Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spa-s3-cloudfront] Fix preview enviornments #995

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading