InfrastructureS3ToDynamoDBL.../modules/lambda/main.tf

61 lines
1.9 KiB
Terraform
Raw Permalink Normal View History

2023-07-11 15:45:59 +00:00
resource "aws_lambda_function" "lambda_function" {
filename = "${path.module}/files/${var.filename}"
function_name = var.lambda_function_name
role = var.lambda_function_role
handler = var.lambda_function_handler
runtime = var.lambda_function_runtime
timeout = var.lambda_function_timeout
memory_size = var.lambda_function_memory_size
environment {
variables = var.lambda_function_environment_variables
}
vpc_config {
subnet_ids = var.lambda_function_subnet_ids
security_group_ids = var.lambda_function_security_group_ids
}
tags = merge(local.default_tags, var.tags, { "Project" = var.project }, { "Environment" = var.environment })
}
resource "aws_cloudwatch_log_group" "lambda_function_log_group" {
name = "/aws/lambda/${var.lambda_function_name}"
retention_in_days = 14
tags = merge(local.default_tags, var.tags, { "Project" = var.project }, { "Environment" = var.environment })
}
resource "aws_lambda_permission" "s3_trigger" {
statement_id = "AllowExecutionFromS3"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.arn
principal = "s3.amazonaws.com"
source_arn = var.s3_arn
}
resource "aws_lambda_permission" "cloudwatch_logs" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.lambda_function.arn
principal = "logs.amazonaws.com"
source_arn = aws_cloudwatch_log_group.lambda_function_log_group.arn
}
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = var.s3_bucket
lambda_function {
lambda_function_arn = aws_lambda_function.lambda_function.arn
events = ["s3:ObjectCreated:*"]
filter_prefix = var.s3_bucket_prefix
}
}
output "lambda_function_arn" {
value = aws_lambda_function.lambda_function.arn
description = "ARN of the Lambda function"
}