Skip to content

Commit

Permalink
fix rewrite function
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbass committed Apr 22, 2024
1 parent 1caf511 commit 9671490
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
16 changes: 8 additions & 8 deletions tofu/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Tooling
# Tooling

Code was written and tested with the following (these versions are enforced in code as minimums):

Expand Down Expand Up @@ -56,7 +56,7 @@ This is deployed individually before any other stacks and generally should not c

1. tofu/environments/\<env>/terraform/tfbackend
1. `cd tofu/environments/<env>/terraform/tfbackend`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
Expand All @@ -67,31 +67,31 @@ These should be deployed in the following order and generally will only be updat

1. tofu/environments/\<env>/network/vpc
1. `cd tofu/environments/<env>/network/vpc`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
2. tofu/environments/\<env>/services/backend-infra
1. `cd tofu/environments/<env>/services/backend-infra`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
3. tofu/environments/\<env>/data-store/cache
1. `cd tofu/environments/<env>/datastore/cache`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
4. tofu/environments/\<env>/data-store/database
1. `cd tofu/environments/<env>/data-store/database`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
5. tofu/environments/\<end>/services/frontend
1. `cd tofu/environments/<env>/services/frontend`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
Expand All @@ -100,7 +100,7 @@ These should be deployed in the following order and generally will only be updat

1. tofu/environments/\<env>/services/backend-service
1. `cd tofu/environments/<env>/services/backend-service`
2. `terragrun init`
2. `terragrunt init`
3. `terragrunt validate`
4. `terragrunt plan -out tfplan`
5. `terragrunt apply tfplan`
36 changes: 13 additions & 23 deletions tofu/modules/services/frontend/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ resource "aws_cloudfront_distribution" "appointment" {

function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.add_index.arn
function_arn = aws_cloudfront_function.rewrite_api.arn
}

viewer_protocol_policy = "redirect-to-https"
Expand Down Expand Up @@ -162,6 +162,11 @@ resource "aws_cloudfront_distribution" "appointment" {
cache_policy_id = data.aws_cloudfront_cache_policy.CachingDisabled.id
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.AllViewer.id

function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.rewrite_api.arn
}

viewer_protocol_policy = "redirect-to-https"

}
Expand Down Expand Up @@ -194,38 +199,23 @@ resource "aws_cloudfront_function" "rewrite_api" {
async function handler(event) {
const request = event.request;
const apiPath = "/api/v1";
const ignorePaths = ['/fxa', '/assets', '/appointment_logo.svg'];
const pathCheckFn = (path) => request.uri.startsWith(path);
// If our api path is the first thing that's found in the uri then remove it from the uri.
if (request.uri.indexOf(apiPath) === 0) {
request.uri = request.uri.replace(apiPath, "");
request.uri = request.uri.replace(apiPath, "");
} else if (!ignorePaths.some(pathCheckFn)) {
// If we're not in one of the ignorePaths then force them to /index.html
request.uri = '/index.html';
}
// else carry on like normal.
return request;
}
EOT
}

resource "aws_cloudfront_function" "add_index" {
name = "${var.name_prefix}-add-index"
runtime = "cloudfront-js-2.0"
code = <<EOT
async function handler(event) {
const request = event.request;
const uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri = '/index.html';
}
return request;
}
EOT
}
resource "aws_s3_bucket" "request_logs" {
bucket = local.log_bucket
force_destroy = true
Expand Down

0 comments on commit 9671490

Please sign in to comment.