|
| 1 | +resource "aws_s3_bucket_notification" "bucket_notification" { |
| 2 | + bucket = data.aws_s3_bucket.archive.id |
| 3 | + |
| 4 | + lambda_function { |
| 5 | + lambda_function_arn = module.landing_s3_trigger_lambda.lambda_function_arn |
| 6 | + events = ["s3:ObjectCreated:*"] |
| 7 | + filter_prefix = "${local.newssite_economy_alias}/" |
| 8 | + filter_suffix = "landing.html" |
| 9 | + } |
| 10 | + |
| 11 | + lambda_function { |
| 12 | + lambda_function_arn = module.landing_metadata_s3_trigger_lambda.lambda_function_arn |
| 13 | + events = ["s3:ObjectCreated:*"] |
| 14 | + filter_prefix = "${local.newssite_economy_alias}/" |
| 15 | + filter_suffix = "/metadata.json" |
| 16 | + } |
| 17 | + |
| 18 | + depends_on = [ |
| 19 | + aws_lambda_permission.allow_bucket_trigger_by_landing, |
| 20 | + aws_lambda_permission.allow_bucket_trigger_by_landing_metadata |
| 21 | + ] |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_lambda_permission" "allow_bucket_trigger_by_landing" { |
| 25 | + statement_id = "AllowExecutionFromS3Bucket" |
| 26 | + action = "lambda:InvokeFunction" |
| 27 | + function_name = module.landing_s3_trigger_lambda.lambda_function_arn |
| 28 | + principal = "s3.amazonaws.com" |
| 29 | + source_arn = data.aws_s3_bucket.archive.arn |
| 30 | +} |
| 31 | + |
| 32 | +resource "aws_lambda_permission" "allow_bucket_trigger_by_landing_metadata" { |
| 33 | + statement_id = "AllowExecutionFromS3Bucket" |
| 34 | + action = "lambda:InvokeFunction" |
| 35 | + function_name = module.landing_metadata_s3_trigger_lambda.lambda_function_arn |
| 36 | + principal = "s3.amazonaws.com" |
| 37 | + source_arn = data.aws_s3_bucket.archive.arn |
| 38 | +} |
| 39 | + |
| 40 | +module "landing_s3_trigger_lambda" { |
| 41 | + source = "terraform-aws-modules/lambda/aws" |
| 42 | + create_function = true |
| 43 | + function_name = "${local.project_name}-landing-s3-trigger-lambda" |
| 44 | + description = "Put a landing page in db" |
| 45 | + handler = "landing_s3_trigger" |
| 46 | + runtime = "go1.x" |
| 47 | + |
| 48 | + source_path = [{ |
| 49 | + path = "${var.repo_dir}/lambda_golang/" |
| 50 | + commands = ["${local.go_build_flags} go build ./cmd/landing_s3_trigger", ":zip"] |
| 51 | + patterns = ["landing_s3_trigger"] |
| 52 | + }] |
| 53 | + |
| 54 | + timeout = 900 |
| 55 | + cloudwatch_logs_retention_in_days = 7 |
| 56 | + publish = true |
| 57 | + |
| 58 | + attach_policy_statements = true |
| 59 | + policy_statements = { |
| 60 | + allow_db_put = { |
| 61 | + effect = "Allow", |
| 62 | + actions = [ |
| 63 | + "dynamodb:PutItem", |
| 64 | + ], |
| 65 | + resources = [media_table_arn] |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + environment_variables = { |
| 70 | + SLACK_WEBHOOK_URL = var.slack_post_webhook_url |
| 71 | + LOG_LEVEL = "DEBUG" |
| 72 | + DEBUG = "true" |
| 73 | + DYNAMODB_TABLE_ID = media_table_id |
| 74 | + } |
| 75 | + |
| 76 | + tags = { |
| 77 | + Project = local.project_name |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module "landing_metadata_s3_trigger_lambda" { |
| 82 | + source = "terraform-aws-modules/lambda/aws" |
| 83 | + |
| 84 | + create_function = true |
| 85 | + function_name = "${local.project_name}-fetch-stories" |
| 86 | + description = "Fetch ${local.project_name} stories; triggered by metadata.json creation" |
| 87 | + handler = "stories" |
| 88 | + runtime = "go1.x" |
| 89 | + source_path = [{ |
| 90 | + path = "${var.repo_dir}/lambda_golang/" |
| 91 | + commands = ["${local.go_build_flags} go build ./cmd/stories", ":zip"] |
| 92 | + patterns = ["stories"] |
| 93 | + }] |
| 94 | + publish = true |
| 95 | + |
| 96 | + timeout = 900 |
| 97 | + cloudwatch_logs_retention_in_days = 7 |
| 98 | + |
| 99 | + reserved_concurrent_executions = -1 |
| 100 | + |
| 101 | + # allow lambda to invoke step function |
| 102 | + attach_policy_json = true |
| 103 | + policy_json = <<EOF |
| 104 | +{ |
| 105 | + "Version": "2012-10-17", |
| 106 | + "Statement": [ |
| 107 | + { |
| 108 | + "Effect": "Allow", |
| 109 | + "Action": [ |
| 110 | + "states:StartExecution" |
| 111 | + ], |
| 112 | + "Resource": ["${module.batch_stories_sfn.state_machine_arn}"] |
| 113 | + } |
| 114 | + ] |
| 115 | +} |
| 116 | +EOF |
| 117 | + |
| 118 | + attach_policy_statements = true |
| 119 | + policy_statements = { |
| 120 | + s3_archive_bucket = { |
| 121 | + effect = "Allow", |
| 122 | + actions = [ |
| 123 | + "s3:GetObject" |
| 124 | + ], |
| 125 | + resources = [ |
| 126 | + "${data.aws_s3_bucket.archive.arn}/*", |
| 127 | + ] |
| 128 | + } |
| 129 | + s3_archive_bucket_check_404 = { |
| 130 | + effect = "Allow", |
| 131 | + actions = [ |
| 132 | + "s3:ListBucket", |
| 133 | + ], |
| 134 | + resources = [ |
| 135 | + "${data.aws_s3_bucket.archive.arn}", |
| 136 | + ] |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + environment_variables = { |
| 141 | + SLACK_WEBHOOK_URL = var.slack_post_webhook_url |
| 142 | + LOGLEVEL = "DEBUG" |
| 143 | + ENV = local.environment |
| 144 | + |
| 145 | + S3_ARCHIVE_BUCKET = data.aws_s3_bucket.archive.id |
| 146 | + SFN_ARN = module.batch_stories_sfn.state_machine_arn |
| 147 | + } |
| 148 | + |
| 149 | + tags = { |
| 150 | + Project = local.project_name |
| 151 | + } |
| 152 | +} |
0 commit comments