From 1c4fe4d635a301f6446034b400c65ae32aabc0af Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Mon, 11 Feb 2019 20:25:05 +0100 Subject: [PATCH 1/6] Added middleware_common --- main.tf | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.tf b/main.tf index db17803..8b76e99 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,8 @@ +provider "aws" { + alias = "middleware" + region = "us-east-1" +} + resource "aws_s3_bucket" "assets" { bucket = "${local.name_prefix}-assets" acl = "private" @@ -112,3 +117,14 @@ resource "aws_cloudfront_distribution" "assets" { tags = "${local.tags}" } + +module "middleware_common" { + source = "./middleware_common" + + name_prefix = "${local.name_prefix}" + tags = "${local.tags}" + + providers = { + aws = "aws.middleware" + } +} From dd93b814c1c24b5db6846eb681618ceb18a6fec3 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Mon, 11 Feb 2019 20:41:57 +0100 Subject: [PATCH 2/6] Added pull request router --- example/main.tf | 2 +- main.tf | 22 ++++++++++++++++++++++ templates/pull-request-router.js | 19 +++++++++++++++++++ variables.tf | 5 +++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 templates/pull-request-router.js diff --git a/example/main.tf b/example/main.tf index 7db8eaf..a766bc6 100644 --- a/example/main.tf +++ b/example/main.tf @@ -15,7 +15,7 @@ module "basic" { resource "aws_s3_bucket_object" "basic_index" { bucket = "${module.basic.bucket_name}" - key = "index.html" + key = "PR-1/index.html" content = "

Hello world

" content_type = "text/html" cache_control = "no-cache no-store" diff --git a/main.tf b/main.tf index 8b76e99..96a38a0 100644 --- a/main.tf +++ b/main.tf @@ -128,3 +128,25 @@ module "middleware_common" { aws = "aws.middleware" } } + +data "template_file" "pull_request_router" { + template = "${file("${path.module}/templates/pull-request-router.js")}" + + vars { + path_re = "${var.pull_request_path_re}" + } +} + +module "pull_request_router" { + source = "./middleware" + + name = "${local.name_prefix}-pull-request-router" + code = "${data.template_file.pull_request_router.rendered}" + code_bucket = "${module.middleware_common.source_bucket_name}" + role_arn = "${module.middleware_common.role_arn}" + tags = "${local.tags}" + + providers = { + aws = "aws.middleware" + } +} diff --git a/templates/pull-request-router.js b/templates/pull-request-router.js new file mode 100644 index 0000000..9af2d0b --- /dev/null +++ b/templates/pull-request-router.js @@ -0,0 +1,19 @@ +const path = require("path") + +const r = String.raw +const pathRe = new RegExp(r`${path_re}`) + +exports.handler = (evt, ctx, cb) => { + const { request } = evt.Records[0].cf + console.log("<-", request.uri) + + const match = pathRe.exec(request.uri) + const extension = path.extname(request.uri) + + if (match && !extension) { + request.uri = `/$${match[1]}/index.html` + } + + console.log("->", request.uri) + cb(null, request) +} diff --git a/variables.tf b/variables.tf index 767bcbf..5c300e6 100644 --- a/variables.tf +++ b/variables.tf @@ -41,3 +41,8 @@ variable "cloudfront_price_class" { default = "PriceClass_100" } + +variable "pull_request_path_re" { + description = "Regular expression which extracts the base directory of a PR as it's first match group" + default = "^/(PR-\\d+)($|/)" +} From e05f1fecf9441b83a14237bcd66f6ac6085630ff Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Mon, 11 Feb 2019 21:08:17 +0100 Subject: [PATCH 3/6] Fix: missing pull request router association --- main.tf | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.tf b/main.tf index 96a38a0..50d74ff 100644 --- a/main.tf +++ b/main.tf @@ -100,6 +100,12 @@ resource "aws_cloudfront_distribution" "assets" { forward = "none" } } + + lambda_function_association { + event_type = "origin-request" + lambda_arn = "${module.pull_request_router.arn}" + include_body = false + } } restrictions { From 2da8197ce870446008d25a4a56dec286533be1e3 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Mon, 11 Feb 2019 21:19:56 +0100 Subject: [PATCH 4/6] Added pull request router variables --- README.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d8d56a2..5673930 100644 --- a/README.md +++ b/README.md @@ -28,16 +28,17 @@ The [`master`](https://github.com/codequest-eu/terraform-single-page-app/tree/ma ## Inputs -| Name | Description | Type | Default | Required | -| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | :----: | :----------------: | :------: | -| certificate\_arn | ACM certificate ARN to use instead of the default cloudfront certificate | string | `""` | no | -| cloudfront\_price\_class | CloudFront price class, which specifies where the distribution should be replicated, one of: PriceClass_100, PriceClass_200, PriceClass_All | string | `"PriceClass_100"` | no | -| domains | List of domains which will serve the application. If empty, will use the default cloudfront domain | list | `` | no | -| environment | Kebab-cased name of the environment, eg. production, staging, development, preview. Will be included in resource names | string | n/a | yes | -| project | Kebab-cased name of the project. Will be included in resource names | string | n/a | yes | -| static\_cors\_max\_age\_seconds | How long can CORS OPTIONS request responses be cached | string | `"3600"` | no | -| static\_path | Base path for static assets | string | `"/static"` | no | -| tags | Additional tags to add to each resource that supports them | map | `` | no | +| Name | Description | Type | Default | Required | +| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | :----: | :------------------: | :------: | +| certificate\_arn | ACM certificate ARN to use instead of the default cloudfront certificate | string | `""` | no | +| cloudfront\_price\_class | CloudFront price class, which specifies where the distribution should be replicated, one of: PriceClass_100, PriceClass_200, PriceClass_All | string | `"PriceClass_100"` | no | +| domains | List of domains which will serve the application. If empty, will use the default cloudfront domain | list | `` | no | +| environment | Kebab-cased name of the environment, eg. production, staging, development, preview. Will be included in resource names | string | n/a | yes | +| project | Kebab-cased name of the project. Will be included in resource names | string | n/a | yes | +| pull\_request\_path\_re | Regular expression which extracts the base directory of a PR as it's first match group | string | `"^/(PR-\\d+)($|/)"` | no | +| static\_cors\_max\_age\_seconds | How long can CORS OPTIONS request responses be cached | string | `"3600"` | no | +| static\_path | Base path for static assets | string | `"/static"` | no | +| tags | Additional tags to add to each resource that supports them | map | `` | no | ## Outputs From 6888e3a28338b87d6f277767bd5fa880600b0378 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Fri, 15 Feb 2019 21:45:38 +0100 Subject: [PATCH 5/6] Fixes after code bucket removal --- main.tf | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/main.tf b/main.tf index 8767616..fcfe222 100644 --- a/main.tf +++ b/main.tf @@ -127,7 +127,6 @@ module "middleware_common" { source = "./middleware_common" name_prefix = "${local.name_prefix}" - tags = "${local.tags}" providers = { aws = "aws.middleware" @@ -145,11 +144,10 @@ data "template_file" "pull_request_router" { module "pull_request_router" { source = "./middleware" - name = "${local.name_prefix}-pull-request-router" - code = "${data.template_file.pull_request_router.rendered}" - code_bucket = "${module.middleware_common.source_bucket_name}" - role_arn = "${module.middleware_common.role_arn}" - tags = "${local.tags}" + name = "${local.name_prefix}-pull-request-router" + code = "${data.template_file.pull_request_router.rendered}" + role_arn = "${module.middleware_common.role_arn}" + tags = "${local.tags}" providers = { aws = "aws.middleware" From 9ff688227ccd79ec29e4379425a4575f4eebab2b Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Mon, 18 Feb 2019 12:56:31 +0100 Subject: [PATCH 6/6] Removed custom_error_response block, routing is handled by a lambda --- main.tf | 7 ------- 1 file changed, 7 deletions(-) diff --git a/main.tf b/main.tf index 2ecb60f..fcfe222 100644 --- a/main.tf +++ b/main.tf @@ -107,13 +107,6 @@ resource "aws_cloudfront_distribution" "assets" { } } - custom_error_response { - error_code = 404 - error_caching_min_ttl = 0 - response_code = 200 - response_page_path = "/index.html" - } - restrictions { geo_restriction { restriction_type = "none"