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

Feature/cloudfront function/app handler #67

Merged
merged 5 commits into from
Apr 4, 2023
Merged
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
42 changes: 38 additions & 4 deletions docs/cloudformation/app-geo-ca-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Parameters:
SslCertArn:
Type: AWS::SSM::Parameter::Value<String>
Default: /webpresence/app-geo-ca/ssl-cert-arn
Description: SSM parameter name for app.geo.ca ACM SSL Cert ARN
Description: SSM parameter name for app.geo.ca ACM SSL Cert ARN
WebAclArn:
Type: String
Description: ARN of the WAF web ACL to use for CloudFront
Expand Down Expand Up @@ -100,7 +100,7 @@ Resources:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
Resource: '*'


AppCodeBuildProject:
Expand Down Expand Up @@ -313,7 +313,7 @@ Resources:
- Name: AppBuild
RunOrder: 1
Configuration:
ProjectName: !Ref AppCodeBuildProject
ProjectName: !Ref AppCodeBuildProject
- Name: Deploy
Actions:
- Name: Deploy
Expand Down Expand Up @@ -371,6 +371,9 @@ Resources:
LambdaFunctionAssociations:
- EventType: origin-response
LambdaFunctionARN: !Ref SecHeadersLambdaEdgeArn
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt CloudfrontAppHandler.FunctionMetadata.FunctionARN
DefaultRootObject: index.html
HttpVersion: http2
WebACLId: !Ref WebAclArn
Expand Down Expand Up @@ -398,4 +401,35 @@ Resources:
ResponsePagePath: /index.html
ResponseCode: 200


CloudfrontAppHandler:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: >
function handler(event) {
var request = event.request;
// Redirects requests to the www. url to the non www. url.
if (request.headers && request.headers.host && request.headers.host.value.includes('www.')) {
var newurl = request.headers.host.value.replace('www.', '')
var qs = ''
Object.keys(request.querystring).forEach((e) => {
if (qs === '') {
qs += '?';
} else { qs += '&' };
qs += e + '=' + request.querystring[e].value;
})
var response = {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers:
{ "location": { "value": 'https://' + newurl + request.uri + qs } }
}
return response;
}

return request;
}
FunctionConfig:
Comment: A function run on every request to app.geo.ca
Runtime: cloudfront-js-1.0
Name: app-handler