|
| 1 | +resource "aws_lambda_function" "granules_cnm_ingester" { |
| 2 | + filename = local.lambda_file_name |
| 3 | + source_code_hash = filebase64sha256(local.lambda_file_name) |
| 4 | + function_name = "${var.prefix}-granules_cnm_ingester" |
| 5 | + role = var.lambda_processing_role_arn |
| 6 | + handler = "cumulus_lambda_functions.granules_cnm_ingester.lambda_function.lambda_handler" |
| 7 | + runtime = "python3.9" |
| 8 | + timeout = 300 |
| 9 | + reserved_concurrent_executions = var.granules_cnm_ingester__lambda_concurrency # TODO |
| 10 | + environment { |
| 11 | + variables = { |
| 12 | + LOG_LEVEL = var.log_level |
| 13 | + SNS_TOPIC_ARN = var.cnm_sns_topic_arn |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + vpc_config { |
| 18 | + subnet_ids = var.cumulus_lambda_subnet_ids |
| 19 | + security_group_ids = local.security_group_ids_set ? var.security_group_ids : [aws_security_group.unity_cumulus_lambda_sg[0].id] |
| 20 | + } |
| 21 | + tags = var.tags |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_sns_topic" "granules_cnm_ingester" { |
| 25 | + name = "${var.prefix}-granules_cnm_ingester" |
| 26 | + tags = var.tags |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_sns_topic_policy" "granules_cnm_ingester_policy" { |
| 30 | + arn = aws_sns_topic.granules_cnm_ingester.arn |
| 31 | + policy = templatefile("${path.module}/sns_policy.json", { |
| 32 | + region: var.aws_region, |
| 33 | + accountId: local.account_id, |
| 34 | + snsName: "${var.prefix}-granules_cnm_ingester", |
| 35 | + s3Glob: var.granules_cnm_ingester__s3_glob |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +resource "aws_sqs_queue" "dead_letter_granules_cnm_ingester" { // https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue |
| 40 | + // TODO how to notify admin for failed ingestion? |
| 41 | + tags = var.tags |
| 42 | + name = "${var.prefix}-dead_letter_granules_cnm_ingester" |
| 43 | + delay_seconds = 0 |
| 44 | + max_message_size = 262144 |
| 45 | + message_retention_seconds = 345600 |
| 46 | + visibility_timeout_seconds = 300 |
| 47 | + receive_wait_time_seconds = 0 |
| 48 | + policy = templatefile("${path.module}/sqs_policy.json", { |
| 49 | + region: var.aws_region, |
| 50 | + roleArn: var.lambda_processing_role_arn, |
| 51 | + accountId: local.account_id, |
| 52 | + sqsName: "${var.prefix}-dead_letter_granules_cnm_ingester", |
| 53 | + }) |
| 54 | +// redrive_policy = jsonencode({ |
| 55 | +// deadLetterTargetArn = aws_sqs_queue.terraform_queue_deadletter.arn |
| 56 | +// maxReceiveCount = 4 |
| 57 | +// }) |
| 58 | +// tags = { |
| 59 | +// Environment = "production" |
| 60 | +// } |
| 61 | +} |
| 62 | + |
| 63 | +resource "aws_sqs_queue" "granules_cnm_ingester" { // https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue |
| 64 | + name = "${var.prefix}-granules_cnm_ingester" |
| 65 | + delay_seconds = 0 |
| 66 | + max_message_size = 262144 |
| 67 | + message_retention_seconds = 345600 |
| 68 | + visibility_timeout_seconds = var.granules_cnm_ingester__sqs_visibility_timeout_seconds // Used as cool off time in seconds. It will wait for 5 min if it fails |
| 69 | + receive_wait_time_seconds = 0 |
| 70 | + policy = templatefile("${path.module}/sqs_policy.json", { |
| 71 | + region: var.aws_region, |
| 72 | + roleArn: var.lambda_processing_role_arn, |
| 73 | + accountId: local.account_id, |
| 74 | + sqsName: "${var.prefix}-granules_cnm_ingester", |
| 75 | + }) |
| 76 | + redrive_policy = jsonencode({ |
| 77 | + deadLetterTargetArn = aws_sqs_queue.dead_letter_granules_cnm_ingester.arn |
| 78 | + maxReceiveCount = var.granules_cnm_ingester__sqs_retried_count // How many times it will be retried. |
| 79 | + }) |
| 80 | + tags = var.tags |
| 81 | +} |
| 82 | + |
| 83 | +resource "aws_sns_topic_subscription" "granules_cnm_ingester_topic_subscription" { // https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription |
| 84 | + topic_arn = aws_sns_topic.granules_cnm_ingester.arn |
| 85 | + protocol = "sqs" |
| 86 | + endpoint = aws_sqs_queue.granules_cnm_ingester.arn |
| 87 | +# filter_policy_scope = "MessageBody" // MessageAttributes. not using attributes |
| 88 | +# filter_policy = templatefile("${path.module}/ideas_api_job_results_filter_policy.json", {}) |
| 89 | +} |
| 90 | + |
| 91 | +resource "aws_lambda_event_source_mapping" "granules_cnm_ingester_queue_lambda_trigger" { // https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping#sqs |
| 92 | + event_source_arn = aws_sqs_queue.granules_cnm_ingester.arn |
| 93 | + function_name = aws_lambda_function.granules_cnm_ingester.arn |
| 94 | + batch_size = 1 |
| 95 | + enabled = true |
| 96 | +} |
0 commit comments