Skip to content

Commit

Permalink
[spa-s3-cloudfront] Fix preview enviornments (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
goruha authored Feb 28, 2024
1 parent 056a5cd commit 3727f96
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 3 additions & 3 deletions modules/spa-s3-cloudfront/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Component PR [#991]()
## Component PRs [#991](https://github.com/cloudposse/terraform-aws-components/pull/991) and [#995](https://github.com/cloudposse/terraform-aws-components/pull/995)

### Drop `lambda_edge_redirect_404`
### Drop `lambda_edge_redirect_404`

This PR removes the `lambda_edge_redirect_404` functionality because it leads to significat costs.
This PRs removes the `lambda_edge_redirect_404` functionality because it leads to significat costs.
Use native CloudFront error pages configs instead.

```yaml
Expand Down
32 changes: 31 additions & 1 deletion modules/spa-s3-cloudfront/lambda_edge.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ module "lambda_edge_functions" {
const default_prefix = "";
console.log('request:' + JSON.stringify(request));
const host = request.headers['x-forwarded-host'][0].value;
let host = null;
if (request.headers.hasOwnProperty('x-forwarded-host')) {
host = request.headers['x-forwarded-host'][0].value;
} else {
host = site_fqdn;
}
if (host == site_fqdn) {
request.origin.custom.path = default_prefix; // use default prefix if there is no subdomain
} else {
Expand All @@ -54,6 +61,29 @@ module "lambda_edge_functions" {
handler = var.lambda_edge_handler
event_type = "origin-request"
include_body = false
},
viewer_request = {
source = [{
content = <<-EOT
exports.handler = (event, context, callback) => {
const { request } = event.Records[0].cf;
if ('host' in request.headers) {
request.headers['x-forwarded-host'] = [
{
key: 'X-Forwarded-Host',
value: request.headers.host[0].value
}
];
}
return callback(null, request);
};
EOT
filename = "index.js"
}]
runtime = var.lambda_edge_runtime
handler = var.lambda_edge_handler
event_type = "viewer-request"
include_body = false
}
} : {},
var.lambda_edge_functions,
Expand Down

0 comments on commit 3727f96

Please sign in to comment.