Skip to content

Instantly share code, notes, and snippets.

@cat5inthecradle
Last active January 20, 2022 17:03
Show Gist options
  • Select an option

  • Save cat5inthecradle/9a1186bf07ed07de07b063f1b0eae04c to your computer and use it in GitHub Desktop.

Select an option

Save cat5inthecradle/9a1186bf07ed07de07b063f1b0eae04c to your computer and use it in GitHub Desktop.
Cloudfront function redirect
AWSTemplateFormatVersion: 2010-09-09
Description: Cloudfront redirect lambda demo
Parameters:
FromDomain:
Type: String
Default: darin.dev-code.org
Description: The domain you wish to redirect traffic from
ToDomain:
Type: String
Default: code.org
Description: The domain you wish to redirect traffic to
HostedZoneId:
Type: String
Description: The HostedZoneId for the To/From Domains
RedirectType:
Type: String
Default: 302
AllowedValues:
- 301
- 302
Description: The redirect type, permanent or temporary
Resources:
PrimaryRecord:
Type: "AWS::Route53::RecordSet"
Properties:
HostedZoneId: !Ref HostedZoneId
Name: !Ref FromDomain
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt CFDistribution.DomainName
CFDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: "true"
Comment: "Redirect to root domain"
PriceClass: PriceClass_100
Aliases:
- !Ref FromDomain
DefaultCacheBehavior:
TargetOriginId: PrimaryOrigin
CachePolicyId: !Ref RedirectCachePolicy
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !Ref RedirectFunction
ViewerProtocolPolicy: allow-all
ViewerCertificate:
AcmCertificateArn: !Ref Certificate
SslSupportMethod: sni-only
# The lambda prevents traffic from hitting this origin, but we need it regardless
Origins:
- Id: PrimaryOrigin
DomainName: !Ref FromDomain
CustomOriginConfig:
OriginProtocolPolicy: http-only
HTTPPort: 80
RedirectCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: RedirectCachePolicy
MinTTL: 3600
MaxTTL: 31536000
DefaultTTL: 86400
ParametersInCacheKeyAndForwardedToOrigin:
EnableAcceptEncodingGzip: false
CookiesConfig:
CookieBehavior: none
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- Origin
QueryStringsConfig:
QueryStringBehavior: all
Certificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref FromDomain
ValidationMethod: DNS
DomainValidationOptions:
- DomainName: !Ref FromDomain
HostedZoneId: !Ref HostedZoneId
RedirectFunction:
Type: AWS::CloudFront::Function
Properties:
Name: www_redirect
FunctionConfig:
Comment: Redirects a subdomain to the root domain
Runtime: cloudfront-js-1.0
AutoPublish: true
FunctionCode: !Sub |
function handler(event) {
var newUrl = 'https://${ToDomain}';
// Append path
newUrl += event.request.uri;
// Append query string
// - skipping because it's annoying to parse.
return {
statusCode: 302,
statusDescription: '302 Redirect to root domain',
headers:{
"location": { "value": newUrl }
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment