diff --git a/terraform/environments/electronic-monitoring-data/lambda_triggers.tf b/terraform/environments/electronic-monitoring-data/lambda_triggers.tf index 73ae3a3a3f8..f773523c9a1 100644 --- a/terraform/environments/electronic-monitoring-data/lambda_triggers.tf +++ b/terraform/environments/electronic-monitoring-data/lambda_triggers.tf @@ -1,34 +1,59 @@ # --------------------------------------- # live fms data json trigger # --------------------------------------- -resource "aws_sns_topic_subscription" "live_serco_fms_sns_subscription" { - topic_arn = aws_sns_topic.live_serco_fms_s3_events.arn - protocol = "lambda" - endpoint = module.format_json_fms_data.lambda_function_arn +resource "aws_s3_bucket_notification" "historic_data_store" { + depends_on = [aws_lambda_permission.historic, aws_lambda_permission.live_serco_fms] + bucket = module.s3-data-bucket.bucket.id + + lambda_function { + lambda_function_arn = module.calculate_checksum.lambda_function_arn + events = [ + "s3:ObjectCreated:*" + ] + filter_suffix = ".bak" + } + lambda_function { + lambda_function_arn = module.calculate_checksum.lambda_function_arn + events = [ + "s3:ObjectCreated:*", + ] + filter_suffix = ".zip" + } + lambda_function { + lambda_function_arn = module.calculate_checksum.lambda_function_arn + events = [ + "s3:ObjectCreated:*", + ] + filter_suffix = ".bacpac" + } + lambda_function { + lambda_function_arn = module.format_json_fms_data.lambda_function_arn + events = [ + "s3:ObjectCreated:*", + ] + filter_suffix = ".JSON" + filter_prefix = "serco/fms/" + } } -resource "aws_lambda_permission" "live_serco_fms_with_sns" { - statement_id = "LiveServcoFMSLambdaAllowExecutionFromSNS" + +resource "aws_lambda_permission" "live_serco_fms" { + statement_id = "LiveSercoFMSLambdaAllowExecutionFromS3Bucket" action = "lambda:InvokeFunction" function_name = module.format_json_fms_data.lambda_function_name - principal = "sns.amazonaws.com" - source_arn = aws_sns_topic.live_serco_fms_s3_events.arn + principal = "s3.amazonaws.com" + source_arn = module.s3-data-bucket.bucket.arn } # --------------------------------------- # historic data json trigger # --------------------------------------- -resource "aws_sns_topic_subscription" "historic_sns_subscription" { - topic_arn = aws_sns_topic.historic_s3_events.arn - protocol = "lambda" - endpoint = module.calculate_checksum.lambda_function_arn -} -resource "aws_lambda_permission" "historic_with_sns" { - statement_id = "ChecksumLambdaAllowExecutionFromHistoricDataSNS" +resource "aws_lambda_permission" "historic" { + statement_id = "ChecksumLambdaAllowExecutionFromHistoricData" action = "lambda:InvokeFunction" function_name = module.calculate_checksum.lambda_function_name - principal = "sns.amazonaws.com" - source_arn = aws_sns_topic.historic_s3_events.arn + principal = "s3.amazonaws.com" + source_arn = module.s3-data-bucket.bucket.arn } diff --git a/terraform/environments/electronic-monitoring-data/s3_sns.tf b/terraform/environments/electronic-monitoring-data/s3_sns.tf deleted file mode 100644 index b821c1d8722..00000000000 --- a/terraform/environments/electronic-monitoring-data/s3_sns.tf +++ /dev/null @@ -1,119 +0,0 @@ - -# bucket notification for data store -resource "aws_s3_bucket_notification" "historic_data_store" { - depends_on = [aws_sns_topic_policy.historic_s3_events_policy] - bucket = module.s3-data-bucket.bucket.id - - # Only for copy events as those are events triggered by data being copied - #  from landing bucket. - topic { - topic_arn = aws_sns_topic.historic_s3_events.arn - events = [ - "s3:ObjectCreated:*" - ] - filter_suffix = ".bak" - } - topic { - topic_arn = aws_sns_topic.historic_s3_events.arn - events = [ - "s3:ObjectCreated:*", - ] - filter_suffix = ".zip" - } - topic { - topic_arn = aws_sns_topic.historic_s3_events.arn - events = [ - "s3:ObjectCreated:*", - ] - filter_suffix = ".bacpac" - } -} - -# sns topic to allow multiple lambdas to be triggered off of it -#trivy:ignore:AVD-AWS-0136 -resource "aws_sns_topic" "historic_s3_events" { - name = "${module.s3-data-bucket.bucket.id}-historic-object-created-topic" - kms_master_key_id = "alias/aws/sns" -} - -# IAM policy document for the SNS topic policy -data "aws_iam_policy_document" "historic_sns_policy" { - statement { - effect = "Allow" - - principals { - type = "Service" - identifiers = ["s3.amazonaws.com"] - } - - actions = ["SNS:Publish"] - resources = [aws_sns_topic.historic_s3_events.arn] - - condition { - test = "ArnLike" - variable = "aws:SourceArn" - values = [module.s3-data-bucket.bucket.arn] - } - } -} - -# Apply policy to the SNS topic -resource "aws_sns_topic_policy" "historic_s3_events_policy" { - arn = aws_sns_topic.historic_s3_events.arn - policy = data.aws_iam_policy_document.historic_sns_policy.json -} - -# ----------------------------------------------- -# Live data sns notification -# ----------------------------------------------- - - -# bucket notification for data store -resource "aws_s3_bucket_notification" "live_serco_fms_data_store" { - depends_on = [aws_sns_topic_policy.live_serco_fms_s3_events_policy] - bucket = module.s3-data-bucket.bucket.id - - # Only for copy events as those are events triggered by data being copied - #  from landing bucket. - topic { - topic_arn = aws_sns_topic.live_serco_fms_s3_events.arn - events = [ - "s3:ObjectCreated:*" - ] - filter_suffix = ".JSON" - } -} - -# sns topic to allow multiple lambdas to be triggered off of it -#trivy:ignore:AVD-AWS-0136 -resource "aws_sns_topic" "live_serco_fms_s3_events" { - name = "${module.s3-data-bucket.bucket.id}-live-object-created-topic" - kms_master_key_id = "alias/aws/sns" -} - -# IAM policy document for the SNS topic policy -data "aws_iam_policy_document" "live_serco_fms_sns_policy" { - statement { - effect = "Allow" - - principals { - type = "Service" - identifiers = ["s3.amazonaws.com"] - } - - actions = ["SNS:Publish"] - resources = [aws_sns_topic.live_serco_fms_s3_events.arn] - - condition { - test = "ArnLike" - variable = "aws:SourceArn" - values = [module.s3-data-bucket.bucket.arn] - } - } -} - -# Apply policy to the SNS topic -resource "aws_sns_topic_policy" "live_serco_fms_s3_events_policy" { - arn = aws_sns_topic.live_serco_fms_s3_events.arn - policy = data.aws_iam_policy_document.live_serco_fms_sns_policy.json -}