From 2d35845d8433661e03732c7cc21edb32cde4f1fc Mon Sep 17 00:00:00 2001 From: Graham Pengelly Date: Thu, 19 May 2016 14:37:15 +0100 Subject: [PATCH] Get lambda from S3 This commit moves the lambda source code out of this repo and now pulls it from S3 (`govuk-lambda-applications-`) The lambda zip file version needs to be passed in as an environment variable `TF_VAR_rename_email_files_with_request_id_version` --- .../files/.gitignore | 1 - .../rename_email_files_with_request_id.zip | Bin 773 -> 0 bytes .../resources/email_alert_notifications.tf | 21 +++++++-- .../resources/files/.gitignore | 1 - .../rename_email_files_with_request_id.py | 44 ------------------ .../rename_email_files_with_request_id.zip | Bin 773 -> 0 bytes 6 files changed, 16 insertions(+), 51 deletions(-) delete mode 100644 projects/email_alert_notifications/files/.gitignore delete mode 100644 projects/email_alert_notifications/files/rename_email_files_with_request_id.zip delete mode 100644 projects/email_alert_notifications/resources/files/.gitignore delete mode 100644 projects/email_alert_notifications/resources/files/rename_email_files_with_request_id.py delete mode 100644 projects/email_alert_notifications/resources/rename_email_files_with_request_id.zip diff --git a/projects/email_alert_notifications/files/.gitignore b/projects/email_alert_notifications/files/.gitignore deleted file mode 100644 index c4c4ffc..0000000 --- a/projects/email_alert_notifications/files/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.zip diff --git a/projects/email_alert_notifications/files/rename_email_files_with_request_id.zip b/projects/email_alert_notifications/files/rename_email_files_with_request_id.zip deleted file mode 100644 index 54e7df4ac70092c01942f77a0352298cc988fc07..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 773 zcmWIWW@Zs#U|`^2&|NmoW6j6id8$kd47#ig45|z=3`MDViMgrqskw=nIq_+kIjP0* z<(VZJ@kObHrK!aw@tG-l1(l&8oD9rv4N~C`I;6r&E4UdLS-vtdFo2Dn8WP(*+d-uE zdw7R{Oy-u|OZamYM9R#kTw1QCE3!#xaq(Hd#eLTDiVNS=zrT7hW0qO&XN8%sEuYW( z{qVykv7p^6FC;xHs5aQ7QE_X&%A7}S#_H1-u4m!EX?)8S9jhXQnkQ_pfMTuKkQT%i3eP28)( zY(dG1n@xM>pOOOa4?MRBW|eZC&sB@!^xR!~Dze^SVX$Evr#&3K-dS?cz5%Ed6Q;K{eooid%dkAKxM z{RKCdf7Q9Abm*GLyQn{pdcJY?bzgQW(X4LlEpN}7=^5v)h&y^y)UnmQI-DqC4 z>Z5s|-}`qHH{_}B{hI2*r0^{QB6^o-x3i xkx7mjS1ObMrcMS1V1i;;(g5wX%a0x26(fwfec{;!fYVj0!-`-3;@sJLB9Y1 diff --git a/projects/email_alert_notifications/resources/email_alert_notifications.tf b/projects/email_alert_notifications/resources/email_alert_notifications.tf index edd3446..a792d45 100644 --- a/projects/email_alert_notifications/resources/email_alert_notifications.tf +++ b/projects/email_alert_notifications/resources/email_alert_notifications.tf @@ -2,11 +2,21 @@ variable "s3_bucket_name" { default = "govuk-email-alert-notifications" } +variable "environment"{ +} + +variable "rename_email_files_with_request_id_version"{ +} + +variable "lambda_bucket"{ + default = "govuk-lambda-applications" +} + resource "template_file" "s3_bucket_policy" { template = "${file("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}" + bucket_name = "${var.s3_bucket_name}-${var.environment}" lambda_role = "${aws_iam_role.lambda_execute_and_write_to_email_alert_bucket.arn}" } } @@ -19,13 +29,13 @@ resource "template_file" "put_and_delete_to_email_alert_bucket_policy" { } resource "aws_s3_bucket" "email_alert_inbox_bucket" { - bucket = "${var.s3_bucket_name}" + 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" + name = "lambda_execute_and_write_to_email_alert_bucket" assume_role_policy = "${file("templates/lambda_assume_role_policy.json")}" } @@ -42,12 +52,13 @@ resource "aws_iam_role_policy" "write_to_logs" { } resource "aws_lambda_function" "rename_email_files_with_request_id"{ - filename = "rename_email_files_with_request_id.zip" + s3_bucket = "${var.lambda_bucket}-${var.environment}" + s3_key="rename_email_files_with_request_id.zip" + s3_object_version="${var.rename_email_files_with_request_id_version}" 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" - source_code_hash = "${base64sha256(file("rename_email_files_with_request_id.zip"))}" } resource "aws_lambda_permission" "allow_email_alert_inbox_bucket" { diff --git a/projects/email_alert_notifications/resources/files/.gitignore b/projects/email_alert_notifications/resources/files/.gitignore deleted file mode 100644 index c4c4ffc..0000000 --- a/projects/email_alert_notifications/resources/files/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.zip diff --git a/projects/email_alert_notifications/resources/files/rename_email_files_with_request_id.py b/projects/email_alert_notifications/resources/files/rename_email_files_with_request_id.py deleted file mode 100644 index fe1669e..0000000 --- a/projects/email_alert_notifications/resources/files/rename_email_files_with_request_id.py +++ /dev/null @@ -1,44 +0,0 @@ -import boto3 -import urllib -import re -import uuid -from botocore.exceptions import ClientError - -S3 = boto3.client('s3') - -REQUEST_ID_REGEX = re.compile(r'data-govuk-request-id=(?:3D){,1}"([0-9\-\.]+)"') - -def lambda_handler(event, context): - bucket_name = source_bucket_name(event) - key = source_key(event) - request_id = parse_request_id(bucket_name, key) - prefix = file_prefix(event, request_id) - move_file(bucket_name, key, request_id, prefix) - -def source_bucket_name(event): - return event['Records'][0]['s3']['bucket']['name'] - -def source_key(event): - return urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') - -def parse_request_id(bucket_name, key): - m = REQUEST_ID_REGEX.search(email_body(bucket_name, key)) - if m: - return m.group(1) - -def email_body(bucket_name, key): - response = S3.get_object(Bucket=bucket_name, Key=key) - return response["Body"].read() - -def move_file(bucket_name, key, request_id, prefix): - S3.copy_object( - Bucket=bucket_name, - CopySource='%s/%s' % (bucket_name, key), - Key='%s/%s.msg' % (prefix, request_id or uuid.uuid4())) - S3.delete_object(Bucket=bucket_name, Key=key) - -def file_prefix(event, request_id): - if request_id: - return "travel-advice-alerts" - else: - return "no-request-id" diff --git a/projects/email_alert_notifications/resources/rename_email_files_with_request_id.zip b/projects/email_alert_notifications/resources/rename_email_files_with_request_id.zip deleted file mode 100644 index d371ba800e6c16fa86cfd249492193393a460085..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 773 zcmWIWW@Zs#U|`^2n6rAS$C{73^HiA_7<5?~7*rW#7>ZK!5_41IQ*#qDbK=u7b5e`r z%QH(d;)_xXOH+$W;xkk93MxZGI2o8ZEF{Cjtt7)sE4UdLS-vtdFo2Dn8WP(*+d-uE zdw7R{Oy-u|OZamYM9R#kTw1QCE3!#xaq(Hd#eLTDiVNS=zrT7hW0qO&XN8%sEuYW( z{qVykv7p^6FC;xHs5aQ7QE_X&%A7}S#_H1-u4m!EX?)8S9jhXQnkQ_pfMTuKkQT%i3eP28)( zY(dG1n@xM>pOOOa4?MRBW|eZC&sB@!^xR!~Dze^SVX$Evr#&3K-dS?cz5%Ed6Q;K{eooid%dkAKxM z{RKCdf7Q9Abm*GLyQn{pdcJY?bzgQW(X4LlEpN}7=^5v)h&y^y)UnmQI-DqC4 z>Z5s|-}`qHH{_}B{hI2*r0^{QB6^o-x3i xkx7mjS1ObMrcMS1V1i;;(g5wX%a0x26(fwfec{;!fYVj0!-`-3;>9aK)(P0