This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Email alert notification receipts #46
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a0be1dd
Add email_alert_notifications project
gpeng 523621d
Add s3 bucket notification
gpeng bd1ef1c
Update README
gpeng 2d35845
Get lambda from S3
gpeng d612f2d
Pull object versionid
surminus 48ad577
Update location of templates
surminus 1cfb375
Move templates into project directory
alexmuller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Email Alert Notifications | ||
|
||
This project sets up an S3 bucket and a Lambda that parses files, | ||
extracts `data-govuk-request-id` and renames the file accordingly as | ||
either: | ||
|
||
travel-advice-alerts/<data-govuk-request-id>.msg | ||
|
||
or if the file doesn't contain a `data-govuk-request-id` | ||
|
||
no-request-id/<uuid>.msg | ||
|
||
## Manual Steps | ||
|
||
There is a manual step required for this 'app'. | ||
|
||
* An SES domain with a RuleSet containing an action that writes to the S3 | ||
bucket (`govuk-email-alert-notifications`). An address on this domain will be | ||
subscribed to all of the travel advice country alerts | ||
|
81 changes: 81 additions & 0 deletions
81
projects/email_alert_notifications/resources/email_alert_notifications.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
variable "s3_bucket_name" { | ||
default = "govuk-email-alert-notifications" | ||
} | ||
|
||
variable "environment"{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't required because it's set by default. |
||
} | ||
|
||
variable "LAMBDA_FILENAME"{ | ||
} | ||
|
||
variable "LAMBDA_VERSIONID"{ | ||
} | ||
|
||
variable "lambda_bucket"{ | ||
default = "govuk-lambda-applications" | ||
} | ||
|
||
resource "template_file" "s3_bucket_policy" { | ||
template = "${file("projects/email_alert_notifications/templates/email_alert_s3_bucket_policy.json")}" | ||
vars { | ||
account_id = "${element(split(":", aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.arn), 4)}" | ||
bucket_name = "${var.s3_bucket_name}-${var.environment}" | ||
lambda_role = "${aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.arn}" | ||
} | ||
} | ||
|
||
resource "template_file" "put_and_delete_to_email_alert_bucket_policy" { | ||
template = "${file("projects/email_alert_notifications/templates/put_and_delete_to_s3_policy.json")}" | ||
vars { | ||
resource_arn = "${aws_s3_bucket.email_alert_inbox_bucket.arn}" | ||
} | ||
} | ||
|
||
resource "aws_s3_bucket" "email_alert_inbox_bucket" { | ||
bucket = "${var.s3_bucket_name}-${var.environment}" | ||
acl = "public-read" | ||
policy = "${template_file.s3_bucket_policy.rendered}" | ||
} | ||
|
||
resource "aws_iam_role" "lambda_execute_and_write_to_email_alert_bucket" { | ||
name = "lambda_execute_and_write_to_email_alert_bucket" | ||
assume_role_policy = "${file("projects/email_alert_notifications/templates/lambda_assume_role_policy.json")}" | ||
} | ||
|
||
resource "aws_iam_role_policy" "put_and_delete_to_email_alert_bucket" { | ||
name = "put_and_delete_to_email_alert_bucket" | ||
role = "${aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.id}" | ||
policy = "${template_file.put_and_delete_to_email_alert_bucket_policy.rendered}" | ||
} | ||
|
||
resource "aws_iam_role_policy" "write_to_logs" { | ||
name = "write_to_logs" | ||
role = "${aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.id}" | ||
policy = "${file("projects/email_alert_notifications/templates/write_to_logs_policy.json")}" | ||
} | ||
|
||
resource "aws_lambda_function" "rename_email_files_with_request_id"{ | ||
s3_bucket = "${var.lambda_bucket}-${var.environment}" | ||
s3_key="${var.LAMBDA_FILENAME}" | ||
s3_object_version="${var.LAMBDA_VERSIONID}" | ||
function_name = "rename_email_files_with_request_id" | ||
role = "${aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.arn}" | ||
handler = "rename_email_files_with_request_id.lambda_handler" | ||
runtime = "python2.7" | ||
} | ||
|
||
resource "aws_lambda_permission" "allow_email_alert_inbox_bucket" { | ||
statement_id = "AllowExecutionFromS3Bucket" | ||
action = "lambda:InvokeFunction" | ||
function_name = "${aws_lambda_function.rename_email_files_with_request_id.arn}" | ||
principal = "s3.amazonaws.com" | ||
source_arn = "${aws_s3_bucket.email_alert_inbox_bucket.arn}" | ||
} | ||
|
||
resource "aws_s3_bucket_notification" "email_alert_inbox_bucket_notification" { | ||
bucket = "${aws_s3_bucket.email_alert_inbox_bucket.id}" | ||
lambda_function { | ||
lambda_function_arn = "${aws_lambda_function.rename_email_files_with_request_id.arn}" | ||
events = ["s3:ObjectCreated:Put"] | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
projects/email_alert_notifications/templates/email_alert_s3_bucket_policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "${uuid()}", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "${lambda_role}" | ||
}, | ||
"Action": [ | ||
"s3:PutObject", | ||
"s3:DeleteObject", | ||
"s3:GetObject" | ||
], | ||
"Resource": "arn:aws:s3:::${bucket_name}/*" | ||
}, | ||
{ | ||
"Sid": "GiveSESPermissionToWriteEmail", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "ses.amazonaws.com" | ||
}, | ||
"Action": "s3:PutObject", | ||
"Resource": "arn:aws:s3:::${bucket_name}/*", | ||
"Condition": { | ||
"StringEquals": { | ||
"aws:Referer": "${account_id}" | ||
} | ||
} | ||
} | ||
] | ||
} |
13 changes: 13 additions & 0 deletions
13
projects/email_alert_notifications/templates/lambda_assume_role_policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} |
15 changes: 15 additions & 0 deletions
15
projects/email_alert_notifications/templates/put_and_delete_to_s3_policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"s3:getObject", | ||
"s3:putObject", | ||
"s3:deleteObject", | ||
"s3:putObjectAcl" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "${resource_arn}" | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
projects/email_alert_notifications/templates/write_to_logs_policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
], | ||
"Resource": "arn:aws:logs:*:*:*" | ||
} | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to go in the
Gemfile
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependency is pulled in by
awspec
so I don't think it also needs to be included in the Gemfile.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep sorry, you're right!