Created
June 15, 2021 08:47
-
-
Save daaru00/d78430d93723fe42e545195a67c38ded to your computer and use it in GitHub Desktop.
A SAM template that describe an Amazon CloudFront distribution that redirect all request to a specific URL using a CloudFront Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: 2010-09-09 | |
Transform: | |
- AWS::Serverless-2016-10-31 | |
# Template Information | |
Description: "Website redirect" | |
# Template Parameters | |
Parameters: | |
RedirectStatusCode: | |
Type: String | |
Description: "The redirect status code" | |
Default: "307" | |
AllowedValues: | |
- '301' | |
- '302' | |
- '307' | |
RedirectStatusDescription: | |
Type: String | |
Description: "The redirect status description" | |
Default: "Temporary Redirect" | |
RedirectUrl: | |
Type: String | |
Description: "The redirect URL" | |
AcmCertificateArn: | |
Type: String | |
Description: "The certificate arn for the domain name provided" | |
CloudFrontAliases: | |
Type: CommaDelimitedList | |
Description: "The endpoint aliases valid for provided certificate" | |
CloudFrontPriceClass: | |
Type: String | |
Description: "The price class for CloudFront distribution" | |
Default: "PriceClass_100" | |
AllowedValues: | |
- PriceClass_100 | |
- PriceClass_200 | |
- PriceClass_All | |
# Template Resources | |
Resources: | |
RedirectFunction: | |
Type: AWS::CloudFront::Function | |
Properties: | |
Name: !Sub "${AWS::StackName}-redirect" | |
AutoPublish: true | |
FunctionCode: !Sub | | |
function handler() { | |
return { | |
statusCode: ${RedirectStatusCode}, | |
statusDescription: '${RedirectStatusDescription}', | |
headers: { location: { value: '${RedirectUrl}' } } | |
} | |
} | |
FunctionConfig: | |
Comment: !Sub "redirect all requests to ${RedirectUrl}" | |
Runtime: cloudfront-js-1.0 | |
Distribution: | |
Type: AWS::CloudFront::Distribution | |
Properties: | |
DistributionConfig: | |
Enabled: true | |
Comment: !Ref AWS::StackName | |
Origins: | |
- DomainName: "fake.com" | |
Id: fakeOrigin | |
CustomOriginConfig: | |
HTTPSPort: 443 | |
OriginProtocolPolicy: "https-only" | |
DefaultCacheBehavior: | |
Compress: 'true' | |
AllowedMethods: | |
- GET | |
- HEAD | |
- OPTIONS | |
TargetOriginId: fakeOrigin | |
ForwardedValues: | |
QueryString: false | |
Cookies: | |
Forward: none | |
ViewerProtocolPolicy: redirect-to-https | |
FunctionAssociations: | |
- EventType: viewer-request | |
FunctionARN: !GetAtt RedirectFunction.FunctionMetadata.FunctionARN | |
PriceClass: !Ref CloudFrontPriceClass | |
Aliases: !Ref CloudFrontAliases | |
ViewerCertificate: | |
AcmCertificateArn: !Ref AcmCertificateArn | |
SslSupportMethod: sni-only | |
# Template Outputs | |
Outputs: | |
CloudFrontDistribution: | |
Description: "The CloudFront distribution in front of the S3 bucket" | |
Value: !Ref Distribution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment