jueves, 6 de noviembre de 2025

Terraform with lambda

 

 


terraform {
required_version = ">= 1.13.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.16.1"
}
}
}

# Configure the AWS provider
provider "aws" {
region = "us-east-1" # Replace with your desired region
}

# IAM role for Lambda execution
resource "aws_iam_role" "lambda_exec" {
name = "hello_world_lambda_role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}

# Attach basic Lambda execution policy
resource "aws_iam_role_policy_attachment" "lambda_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}

# Create a ZIP archive of the Lambda function
data "archive_file" "lambda_zip" {
type = "zip"
source_file = "${path.module}/lambda_function.py"
output_path = "${path.module}/lambda_function.zip"
}

# Create the Lambda function
resource "aws_lambda_function" "hello_world" {
function_name = "hello_world"
filename = data.archive_file.lambda_zip.output_path
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
role = aws_iam_role.lambda_exec.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.9"
environment {
variables = {
ENVIRONMENT = "production"
}
}
}

# Output the Lambda function ARN
output "lambda_function_arn" {
description = "The ARN of the Lambda function"
value = aws_lambda_function.hello_world.arn
}

# Output the Lambda function name
output "lambda_function_name" {
description = "The name of the Lambda function"
value = aws_lambda_function.hello_world.function_name
}



 

No hay comentarios:

Publicar un comentario