|
| 1 | +module "lambda_event_publisher" { |
| 2 | + source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda?ref=v2.0.4" |
| 3 | + |
| 4 | + project = var.project |
| 5 | + environment = var.environment |
| 6 | + component = var.component |
| 7 | + aws_account_id = var.aws_account_id |
| 8 | + region = var.region |
| 9 | + |
| 10 | + kms_key_arn = var.kms_key_arn |
| 11 | + |
| 12 | + function_name = "event-publisher" |
| 13 | + |
| 14 | + function_module_name = "event-publisher" |
| 15 | + handler_function_name = "handler" |
| 16 | + description = "Lambda that accepts events from the dynamodb stream and publishes them to SNS" |
| 17 | + |
| 18 | + memory = 512 |
| 19 | + timeout = 20 |
| 20 | + runtime = "nodejs20.x" |
| 21 | + |
| 22 | + log_retention_in_days = var.log_retention_in_days |
| 23 | + iam_policy_document = { |
| 24 | + body = data.aws_iam_policy_document.event_publisher.json |
| 25 | + } |
| 26 | + |
| 27 | + lambda_env_vars = { |
| 28 | + SNS_TOPIC_ARN = coalesce(var.sns_topic_arn, aws_sns_topic.main.arn) |
| 29 | + TEMPLATES_TABLE_NAME = aws_dynamodb_table.templates.name |
| 30 | + EVENT_SOURCE = "//notify.nhs.uk/${var.component}/${var.group}/${var.environment}" |
| 31 | + } |
| 32 | + |
| 33 | + function_s3_bucket = var.function_s3_bucket |
| 34 | + function_code_base_path = local.lambdas_dir |
| 35 | + function_code_dir = "event-publisher/dist" |
| 36 | + |
| 37 | + send_to_firehose = var.send_to_firehose |
| 38 | + log_destination_arn = var.log_destination_arn |
| 39 | + log_subscription_role_arn = var.log_subscription_role_arn |
| 40 | +} |
| 41 | + |
| 42 | +resource "aws_lambda_event_source_mapping" "event_publisher" { |
| 43 | + event_source_arn = module.sqs_template_mgmt_events.sqs_queue_arn |
| 44 | + function_name = module.lambda_event_publisher.function_name |
| 45 | + batch_size = 5 |
| 46 | + maximum_batching_window_in_seconds = 0 |
| 47 | + function_response_types = [ |
| 48 | + "ReportBatchItemFailures" |
| 49 | + ] |
| 50 | + |
| 51 | + scaling_config { |
| 52 | + maximum_concurrency = 5 |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +data "aws_iam_policy_document" "event_publisher" { |
| 57 | + statement { |
| 58 | + sid = "AllowSNS" |
| 59 | + effect = "Allow" |
| 60 | + |
| 61 | + actions = [ |
| 62 | + "sns:Publish", |
| 63 | + ] |
| 64 | + |
| 65 | + resources = [ |
| 66 | + coalesce(var.sns_topic_arn, aws_sns_topic.main.arn) |
| 67 | + ] |
| 68 | + } |
| 69 | + |
| 70 | + statement { |
| 71 | + sid = "AllowSQSDLQ" |
| 72 | + effect = "Allow" |
| 73 | + |
| 74 | + actions = [ |
| 75 | + "sqs:SendMessage", |
| 76 | + ] |
| 77 | + |
| 78 | + resources = [ |
| 79 | + module.sqs_template_mgmt_events.sqs_dlq_arn, |
| 80 | + ] |
| 81 | + } |
| 82 | + |
| 83 | + statement { |
| 84 | + sid = "AllowSQS" |
| 85 | + effect = "Allow" |
| 86 | + |
| 87 | + actions = [ |
| 88 | + "sqs:ReceiveMessage", |
| 89 | + "sqs:DeleteMessage", |
| 90 | + "sqs:GetQueueAttributes", |
| 91 | + "sqs:ChangeMessageVisibility", |
| 92 | + ] |
| 93 | + |
| 94 | + resources = [ |
| 95 | + module.sqs_template_mgmt_events.sqs_queue_arn, |
| 96 | + ] |
| 97 | + } |
| 98 | + |
| 99 | + statement { |
| 100 | + sid = "AllowKMS" |
| 101 | + effect = "Allow" |
| 102 | + |
| 103 | + actions = [ |
| 104 | + "kms:Decrypt", |
| 105 | + "kms:DescribeKey", |
| 106 | + "kms:Encrypt", |
| 107 | + "kms:GenerateDataKey*", |
| 108 | + "kms:ReEncrypt*", |
| 109 | + ] |
| 110 | + |
| 111 | + resources = [ |
| 112 | + var.kms_key_arn, |
| 113 | + ] |
| 114 | + } |
| 115 | +} |
0 commit comments