|
| 1 | +module "report_sender" { |
| 2 | + source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip" |
| 3 | + |
| 4 | + function_name = "report-sender" |
| 5 | + description = "A lambda function for sending reports to Trusts via MESH messages" |
| 6 | + aws_account_id = var.aws_account_id |
| 7 | + component = local.component |
| 8 | + environment = var.environment |
| 9 | + project = var.project |
| 10 | + region = var.region |
| 11 | + group = var.group |
| 12 | + |
| 13 | + log_retention_in_days = var.log_retention_in_days |
| 14 | + kms_key_arn = module.kms.key_arn |
| 15 | + |
| 16 | + iam_policy_document = { |
| 17 | + body = data.aws_iam_policy_document.report_sender_lambda.json |
| 18 | + } |
| 19 | + |
| 20 | + function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] |
| 21 | + function_code_base_path = local.aws_lambda_functions_dir_path |
| 22 | + function_code_dir = "report-sender/target/dist" |
| 23 | + function_include_common = true |
| 24 | + function_module_name = "report_sender" |
| 25 | + handler_function_name = "handler.handler" |
| 26 | + runtime = "python3.14" |
| 27 | + memory = 128 |
| 28 | + timeout = 5 |
| 29 | + log_level = var.log_level |
| 30 | + |
| 31 | + force_lambda_code_deploy = var.force_lambda_code_deploy |
| 32 | + enable_lambda_insights = false |
| 33 | + |
| 34 | + log_destination_arn = local.log_destination_arn |
| 35 | + log_subscription_role_arn = local.acct.log_subscription_role_arn |
| 36 | + |
| 37 | + lambda_env_vars = { |
| 38 | + REPORT_SENDER_METRIC_NAME = "report-sender-successful-sends" |
| 39 | + REPORT_SENDER_METRIC_NAMESPACE = "dl-report-sender" |
| 40 | + DLQ_URL = module.sqs_report_sender.sqs_dlq_url |
| 41 | + ENVIRONMENT = var.environment |
| 42 | + EVENT_PUBLISHER_DLQ_URL = module.sqs_event_publisher_errors.sqs_queue_url |
| 43 | + EVENT_PUBLISHER_EVENT_BUS_ARN = aws_cloudwatch_event_bus.main.arn |
| 44 | + MOCK_MESH_BUCKET = module.s3bucket_non_pii_data.bucket |
| 45 | + SSM_MESH_PREFIX = "${local.ssm_mesh_prefix}" |
| 46 | + SSM_SENDERS_PREFIX = "${local.ssm_senders_prefix}" |
| 47 | + USE_MESH_MOCK = var.enable_mock_mesh ? "true" : "false" |
| 48 | + } |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +data "aws_iam_policy_document" "report_sender_lambda" { |
| 53 | + statement { |
| 54 | + sid = "KMSPermissions" |
| 55 | + effect = "Allow" |
| 56 | + |
| 57 | + actions = [ |
| 58 | + "kms:Decrypt", |
| 59 | + "kms:GenerateDataKey", |
| 60 | + ] |
| 61 | + |
| 62 | + resources = [ |
| 63 | + module.kms.key_arn, |
| 64 | + ] |
| 65 | + } |
| 66 | + |
| 67 | + statement { |
| 68 | + sid = "SQSPermissions" |
| 69 | + effect = "Allow" |
| 70 | + |
| 71 | + actions = [ |
| 72 | + "sqs:ReceiveMessage", |
| 73 | + "sqs:DeleteMessage", |
| 74 | + "sqs:GetQueueAttributes", |
| 75 | + ] |
| 76 | + |
| 77 | + resources = [ |
| 78 | + module.sqs_report_sender.sqs_queue_arn, |
| 79 | + ] |
| 80 | + } |
| 81 | + |
| 82 | + statement { |
| 83 | + sid = "SQSDLQPermissions" |
| 84 | + effect = "Allow" |
| 85 | + |
| 86 | + actions = [ |
| 87 | + "sqs:SendMessage", |
| 88 | + ] |
| 89 | + |
| 90 | + resources = [ |
| 91 | + module.sqs_report_sender.sqs_dlq_arn, |
| 92 | + ] |
| 93 | + } |
| 94 | + |
| 95 | + statement { |
| 96 | + sid = "EventBridgePermissions" |
| 97 | + effect = "Allow" |
| 98 | + |
| 99 | + actions = [ |
| 100 | + "events:PutEvents", |
| 101 | + ] |
| 102 | + |
| 103 | + resources = [ |
| 104 | + aws_cloudwatch_event_bus.main.arn, |
| 105 | + ] |
| 106 | + } |
| 107 | + |
| 108 | + statement { |
| 109 | + sid = "DLQPermissions" |
| 110 | + effect = "Allow" |
| 111 | + |
| 112 | + actions = [ |
| 113 | + "sqs:SendMessage", |
| 114 | + "sqs:SendMessageBatch", |
| 115 | + ] |
| 116 | + |
| 117 | + resources = [ |
| 118 | + module.sqs_event_publisher_errors.sqs_queue_arn, |
| 119 | + ] |
| 120 | + } |
| 121 | + |
| 122 | + statement { |
| 123 | + sid = "SSMPermissions" |
| 124 | + effect = "Allow" |
| 125 | + |
| 126 | + actions = [ |
| 127 | + "ssm:GetParameter", |
| 128 | + "ssm:GetParametersByPath", |
| 129 | + ] |
| 130 | + |
| 131 | + resources = [ |
| 132 | + "arn:aws:ssm:${var.region}:${var.aws_account_id}:parameter${local.ssm_prefix}/*" |
| 133 | + ] |
| 134 | + } |
| 135 | + |
| 136 | + statement { |
| 137 | + sid = "S3BucketPermissions" |
| 138 | + effect = "Allow" |
| 139 | + |
| 140 | + actions = [ |
| 141 | + "s3:GetObject", |
| 142 | + ] |
| 143 | + |
| 144 | + resources = [ |
| 145 | + "${module.s3bucket_reporting.arn}/*", |
| 146 | + ] |
| 147 | + } |
| 148 | + |
| 149 | + # Grant S3 PutObject permissions for the mock-mesh directory only when the mock is enabled |
| 150 | + dynamic "statement" { |
| 151 | + for_each = var.enable_mock_mesh ? [1] : [] |
| 152 | + content { |
| 153 | + sid = "MockMeshPutObject" |
| 154 | + effect = "Allow" |
| 155 | + |
| 156 | + actions = [ |
| 157 | + "s3:PutObject", |
| 158 | + ] |
| 159 | + |
| 160 | + resources = [ |
| 161 | + "${module.s3bucket_non_pii_data.arn}/mock-mesh/*" |
| 162 | + ] |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments