Skip to content

Instantly share code, notes, and snippets.

@corpulentcoffee
Created September 23, 2018 22:14
Show Gist options
  • Save corpulentcoffee/4ac2b69bedc5e821b81c5bba7f88ad3c to your computer and use it in GitHub Desktop.
Save corpulentcoffee/4ac2b69bedc5e821b81c5bba7f88ad3c to your computer and use it in GitHub Desktop.
Example CloudFormation nested stack for connecting a CloudFront/ACM-powered website in a Route 53 zone
Parameters:
Zone: {Type: String}
Hostname: {Type: String}
Distribution: {Type: String, Default: ''}
ChallengeA: {Type: String, Default: ''}
ChallengeB: {Type: String, Default: ''}
CloudFrontDomain: {Type: String, Default: cloudfront.net.}
CloudFrontHostedZone: {Type: String, Default: Z2FDTNDATAQYW2}
ACMChallengeDomain: {Type: String, Default: acm-validations.aws.}
Conditions:
HasDistribution: !Not [!Equals [!Ref Distribution, '']]
HasChallenge: !Not [!Or [!Equals [!Ref ChallengeA, ''],
!Equals [!Ref ChallengeB, '']]]
Resources:
IPv4:
Type: AWS::Route53::RecordSet
Condition: HasDistribution
Properties:
HostedZoneId: !Ref Zone
Name: !Ref Hostname
AliasTarget:
HostedZoneId: !Ref CloudFrontHostedZone
DNSName: !Sub
- ${Subdomain}.${Domain}
- {Subdomain: !Ref Distribution, Domain: !Ref CloudFrontDomain}
Type: A
IPv6:
Type: AWS::Route53::RecordSet
Condition: HasDistribution
Properties:
HostedZoneId: !Ref Zone
Name: !Ref Hostname
AliasTarget:
HostedZoneId: !Ref CloudFrontHostedZone
DNSName: !Sub
- ${Subdomain}.${Domain}
- {Subdomain: !Ref Distribution, Domain: !Ref CloudFrontDomain}
Type: AAAA
Certificate:
Type: AWS::Route53::RecordSet
Condition: HasChallenge
Properties:
HostedZoneId: !Ref Zone
Name: !Sub
- _${Challenge}.${Hostname}
- {Challenge: !Ref ChallengeA, Hostname: !Ref Hostname}
ResourceRecords:
- !Sub
- _${Challenge}.${Domain}
- {Challenge: !Ref ChallengeB, Domain: !Ref ACMChallengeDomain}
TTL: 3600
Type: CNAME
Resources:
Zone:
Type: AWS::Route53::HostedZone
Properties:
Name: example.com
WWW:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: cloudfront.yaml
Parameters:
Zone: !Ref Zone
Hostname: www.example.com
Distribution: XXXXXXXXXXXXXX
ChallengeA: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ChallengeB: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment