diff --git a/lambda/main.tf b/lambda/main.tf index 339566b..9cfedca 100644 --- a/lambda/main.tf +++ b/lambda/main.tf @@ -2,13 +2,13 @@ locals { sqs_mapping_without_ignore_enabled = { for mapping in var.lambda_sqs_queue_mappings : (mapping.sqs_queue_arn) => mapping.sqs_queue_concurrency if mapping.ignore_enabled_status == false } sqs_mapping_ignore_enabled = { for mapping in var.lambda_sqs_queue_mappings : (mapping.sqs_queue_arn) => mapping.sqs_queue_concurrency if mapping.ignore_enabled_status == true } } -resource "aws_lambda_function" "lambda_function" { +resource "aws_lambda_function" "lambda_function_zip" { + count = var.use_image ? 0 : 1 function_name = var.function_name handler = var.handler role = aws_iam_role.lambda_iam_role.arn - image_uri = var.use_image ? null : var.image_url - runtime = var.use_image ? null : var.runtime - filename = var.use_image ? null : var.filename == "" ? startswith(var.runtime, "java") ? "${path.module}/functions/generic.jar" : "${path.module}/functions/generic.zip" : var.filename + runtime = var.runtime + filename = var.filename == "" ? startswith(var.runtime, "java") ? "${path.module}/functions/generic.jar" : "${path.module}/functions/generic.zip" : var.filename timeout = var.timeout_seconds memory_size = var.memory_size @@ -39,8 +39,46 @@ resource "aws_lambda_function" "lambda_function" { } } +resource "aws_lambda_function" "lambda_function_ecr" { + count = var.use_image ? 1 : 0 + function_name = var.function_name + handler = var.handler + role = aws_iam_role.lambda_iam_role.arn + image_uri = var.image_url + package_type = "Image" + timeout = var.timeout_seconds + memory_size = var.memory_size + + ephemeral_storage { + size = var.storage_size + } + + reserved_concurrent_executions = var.reserved_concurrency + tags = var.tags + environment { + variables = local.all_env_vars + } + + dynamic "file_system_config" { + for_each = var.efs_access_points + content { + arn = file_system_config.value.access_point_arn + local_mount_path = file_system_config.value.mount_path + } + } + vpc_config { + security_group_ids = var.vpc_config.security_group_ids + subnet_ids = var.vpc_config.subnet_ids + } + + lifecycle { + ignore_changes = [filename] + } +} + + resource "aws_cloudwatch_log_group" "lambda_log_group" { - name = "/aws/lambda/${aws_lambda_function.lambda_function.function_name}" + name = var.use_image ? "/aws/lambda/${aws_lambda_function.lambda_function_ecr[0].function_name}" : "/aws/lambda/${aws_lambda_function.lambda_function_zip[0].function_name}" retention_in_days = var.log_retention kms_key_id = var.log_group_kms_key_arn tags = var.tags @@ -62,7 +100,7 @@ resource "aws_kms_ciphertext" "encrypted_environment_variables" { resource "aws_lambda_event_source_mapping" "sqs_queue_mappings" { for_each = local.sqs_mapping_without_ignore_enabled event_source_arn = each.key - function_name = aws_lambda_function.lambda_function.*.arn[0] + function_name = var.use_image ? aws_lambda_function.lambda_function_ecr.*.arn[0] : aws_lambda_function.lambda_function_zip.*.arn[0] batch_size = var.sqs_queue_mapping_batch_size maximum_batching_window_in_seconds = var.sqs_queue_batching_window dynamic "scaling_config" { @@ -76,7 +114,7 @@ resource "aws_lambda_event_source_mapping" "sqs_queue_mappings" { resource "aws_lambda_event_source_mapping" "sqs_queue_mappings_ignore_enabled" { for_each = local.sqs_mapping_ignore_enabled event_source_arn = each.key - function_name = aws_lambda_function.lambda_function.*.arn[0] + function_name = var.use_image ? aws_lambda_function.lambda_function_ecr.*.arn[0] : aws_lambda_function.lambda_function_zip.*.arn[0] batch_size = var.sqs_queue_mapping_batch_size maximum_batching_window_in_seconds = var.sqs_queue_batching_window dynamic "scaling_config" { @@ -94,7 +132,7 @@ resource "aws_lambda_permission" "lambda_permissions" { for_each = var.lambda_invoke_permissions statement_id = "AllowExecutionFrom${title(split(".", each.key)[0])}" action = "lambda:InvokeFunction" - function_name = aws_lambda_function.lambda_function.function_name + function_name = var.use_image ? aws_lambda_function.lambda_function_ecr[0].function_name : aws_lambda_function.lambda_function_zip[0].function_name principal = each.key source_arn = each.value } diff --git a/lambda/outputs.tf b/lambda/outputs.tf index 3bbdb22..87b8a08 100644 --- a/lambda/outputs.tf +++ b/lambda/outputs.tf @@ -1,5 +1,5 @@ output "lambda_arn" { - value = aws_lambda_function.lambda_function.arn + value = var.use_image ? aws_lambda_function.lambda_function_ecr[0].arn : aws_lambda_function.lambda_function_zip[0].arn } output "lambda_role_arn" {