# Reference existing IAM role data "aws_iam_role" "lambda_exec_role" { name = "lambda-exec-role" } # Reference existing ECR repository data "aws_ecr_repository" "walthamforest_etl_adhoc_ecr" { name = "walthamforest_etl_adhoc_ecr" } # SQS queue resource "aws_sqs_queue" "walthamforest_etl_adhoc_queue" { name = "walthamforest_etl_adhoc-queue" visibility_timeout_seconds = 1800 # 30 minutes (>= 300s and ~6x Lambda timeout) } # Custom IAM policy specific to lambda_example resource "aws_iam_policy" "walthamforest_etl_adhoc_policy" { name = "walthamforest_adhoc_policy_lambda" policy = jsonencode({ Version = "2012-10-17", Statement = [ { Effect = "Allow", Action = [ "sqs:ReceiveMessage", "sqs:DeleteMessage", "sqs:GetQueueAttributes", "sqs:GetQueueUrl", "sqs:ChangeMessageVisibility" ], Resource = aws_sqs_queue.walthamforest_etl_adhoc_queue.arn }, { Effect = "Allow", Action = [ "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability" ], Resource = data.aws_ecr_repository.walthamforest_etl_adhoc_ecr.arn }, { Effect = "Allow", Action = ["ecr:GetAuthorizationToken"], Resource = "*" } ] }) } resource "aws_iam_role_policy_attachment" "walthamforest_etl_adhoc_policy_attach" { role = data.aws_iam_role.lambda_exec_role.name policy_arn = aws_iam_policy.walthamforest_etl_adhoc_policy.arn } # Lambda function resource "aws_lambda_function" "walthamforest_etl_adhoc" { function_name = "walthamforest_etl_adhoc" role = data.aws_iam_role.lambda_exec_role.arn package_type = "Image" image_uri = "${data.aws_ecr_repository.walthamforest_etl_adhoc_ecr.repository_url}:${var.lambda_image_tag}" # Increase timeout (max 900 sec / 15 min) # timeout = 300 # e.g. 5 minutes # Increase memory (default 128 MB) memory_size = 2048 # try 1024 or 2048 MB to start # environment { # variables = { # DATABASE_URL = "postgresql://postgres:makingwarmhomes@terraform-20250331175522503500000002.cdgzupxvdyp0.eu-west-2.rds.amazonaws.com:5432/surveyDB" # } # } } # SQS trigger resource "aws_lambda_event_source_mapping" "walthamforest_etl_adhoc_trigger" { event_source_arn = aws_sqs_queue.walthamforest_etl_adhoc_queue.arn function_name = aws_lambda_function.walthamforest_etl_adhoc.arn batch_size = 1 }