Skip to content

Commit 0357d5c

Browse files
mohsanjafferyMohsan Jaffery
andauthored
Apex domain (#30)
* Added apex domain to ACM cert and Cloudfront distribution * Docs updated with optional steps for apex configuration Co-authored-by: Mohsan Jaffery <mjaffery@amazon.com>
1 parent 99500be commit 0357d5c

4 files changed

Lines changed: 80 additions & 3 deletions

File tree

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ To deploy the solution, you use [AWS CloudFormation](https://aws.amazon.com/clou
7070
following fields:
7171

7272
- **SubDomain:** The subdomain for your registered domain name. Viewers use the subdomain to access your website, for example: www.example.com. We recommend using the default value of **www** as the subdomain.
73-
- **DomainName:** Your registered domain name, such as example.com. This domain must be pointed to a Route 53 hosted zone.
73+
- **DomainName:** Your registered domain name, such as example.com. This domain must be pointed to a Route 53 hosted zone.
74+
- **CreateApex:** Optionally create an Alias to the domain apex (example.com) in your CloudFront configuration. Default is [no]
7475

7576
After entering values, choose the **Next** button.
7677
5. On the **Configure stack options** page, you can optionally [add tags and other stack options](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-add-tags.html). When finished, choose the **Next** button.
@@ -129,15 +130,27 @@ https://s3.amazonaws.com/solution-builders-us-east-1/amazon-cloudfront-secure-st
129130
--output-template-file packaged.template
130131
```
131132

132-
7. Run the following command to deploy the packaged CloudFormation template to a CloudFormation stack:
133+
7. Run the following command to deploy the packaged CloudFormation template to a CloudFormation stack. To optionally deploy the stack with a domain apex skip this section and proceed to [Step 8] below.
133134

134135
```shell
135136
aws --region us-east-1 cloudformation deploy \
136137
--stack-name <your CloudFormation stack name> \
137138
--template-file packaged.template \
138139
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
139140
--parameter-overrides DomainName=<your domain name> SubDomain=<your website subdomain>
140-
```
141+
```
142+
143+
8. [Optional] Run the following command to deploy the packaged CloudFormation template to a CloudFormation stack with a domain apex.
144+
145+
```shell
146+
aws --region us-east-1 cloudformation deploy \
147+
--stack-name <your CloudFormation stack name> \
148+
--template-file packaged.template \
149+
--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
150+
--parameter-overrides DomainName=<your domain name> SubDomain=<your website subdomain> CreateApex=yes
151+
```
152+
153+
141154
### Updating the site Content Security Policy
142155

143156
To change the Content Security Policy of the site:

templates/acm-certificate.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Parameters:
1313
Type: String
1414
SubDomain:
1515
Type: String
16+
CreateApex:
17+
Type: String
18+
19+
Conditions:
20+
CreateApexConfig: !Equals
21+
- !Ref CreateApex
22+
- 'yes'
1623

1724
Resources:
1825
CopyCustomResource:
@@ -24,6 +31,11 @@ Resources:
2431
Type: Custom::Certificate
2532
Properties:
2633
DomainName: !Sub '${SubDomain}.${DomainName}'
34+
SubjectAlternativeNames:
35+
Fn::If:
36+
- CreateApexConfig
37+
- - Ref: DomainName
38+
- Ref: AWS::NoValue
2739
Region: !Ref Region
2840
ValidationMethod: DNS
2941
ServiceToken: !Ref 'CFNCustomProvider'
@@ -41,6 +53,14 @@ Resources:
4153
DomainName: !Sub '${SubDomain}.${DomainName}'
4254
ServiceToken: !Ref 'CFNCustomProvider'
4355

56+
apexCertificateDNSRecord:
57+
Type: Custom::CertificateDNSRecord
58+
Condition: CreateApexConfig
59+
Properties:
60+
CertificateArn: !Ref Certificate
61+
DomainName: !Ref DomainName
62+
ServiceToken: !Ref 'CFNCustomProvider'
63+
4464
DomainValidationRecord:
4565
Type: AWS::Route53::RecordSetGroup
4666
Properties:
@@ -54,10 +74,25 @@ Resources:
5474
ResourceRecords:
5575
- !GetAtt CertificateDNSRecord.Value
5676

77+
apexDomainValidationRecord:
78+
Type: AWS::Route53::RecordSetGroup
79+
Condition: CreateApexConfig
80+
Properties:
81+
HostedZoneName: !Sub '${DomainName}.'
82+
RecordSets:
83+
- Name: !GetAtt apexCertificateDNSRecord.Name
84+
Type: !GetAtt apexCertificateDNSRecord.Type
85+
TTL: 60
86+
Weight: 1
87+
SetIdentifier: !Ref Certificate
88+
ResourceRecords:
89+
- !GetAtt apexCertificateDNSRecord.Value
90+
5791
Outputs:
5892
DNSRecord:
5993
Description: DNS record
6094
Value: !Sub '${CertificateDNSRecord.Name} ${CertificateDNSRecord.Type} ${CertificateDNSRecord.Value}'
95+
6196
CertificateArn:
6297
Description: Issued certificate
6398
Value: !Ref Certificate

templates/cloudfront-site.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Parameters:
2727
S3BucketRootArn:
2828
Description: Content Bucket locator
2929
Type: String
30+
CreateApex:
31+
Type: String
32+
33+
Conditions:
34+
CreateApexConfig: !Equals
35+
- !Ref CreateApex
36+
- 'yes'
3037

3138
Resources:
3239
S3BucketPolicy:
@@ -87,6 +94,7 @@ Resources:
8794
DistributionConfig:
8895
Aliases:
8996
- !Sub '${SubDomain}.${DomainName}'
97+
- !If [ CreateApexConfig, !Ref DomainName, !Ref 'AWS::NoValue' ]
9098
DefaultCacheBehavior:
9199
Compress: true
92100
DefaultTTL: 86400
@@ -150,6 +158,20 @@ Resources:
150158
# The following HosteZoneId is always used for alias records pointing to CF.
151159
HostedZoneId: 'Z2FDTNDATAQYW2'
152160

161+
ApexRoute53RecordSetGroup:
162+
Condition: CreateApexConfig
163+
Type: AWS::Route53::RecordSetGroup
164+
Properties:
165+
HostedZoneName: !Sub '${DomainName}.'
166+
RecordSets:
167+
- Name: !Ref 'DomainName'
168+
Type: 'A'
169+
AliasTarget:
170+
DNSName: !GetAtt 'CloudFrontDistribution.DomainName'
171+
EvaluateTargetHealth: false
172+
# The following HosteZoneId is always used for alias records pointing to CF.
173+
HostedZoneId: 'Z2FDTNDATAQYW2'
174+
153175
Outputs:
154176
LambdaEdgeFunctionVersion:
155177
Description: Security Lambda version

templates/main.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Parameters:
2424
DomainName:
2525
Description: The part of a website address after your SubDomain - e.g. example.com
2626
Type: String
27+
CreateApex:
28+
Description: Create an Apex Alias in CloudFront distribution - yes/no
29+
Type: String
30+
Default: 'no'
31+
AllowedValues: ['yes','no']
2732

2833
Resources:
2934
CustomResourceStack:
@@ -43,6 +48,7 @@ Resources:
4348
DomainName: !Ref DomainName
4449
CFNCustomProvider: !GetAtt CustomResourceStack.Outputs.CFNCustomProvider
4550
CopyFunction: !GetAtt CustomResourceStack.Outputs.CopyFunction
51+
CreateApex: !Ref CreateApex
4652
Tags:
4753
- Key: Solution
4854
Value: ACFS3
@@ -55,6 +61,7 @@ Resources:
5561
CertificateArn: !GetAtt AcmCertificateStack.Outputs.CertificateArn
5662
DomainName: !Ref DomainName
5763
SubDomain: !Ref SubDomain
64+
CreateApex: !Ref CreateApex
5865
S3BucketRoot: !GetAtt CustomResourceStack.Outputs.S3BucketRoot
5966
S3BucketRootName: !GetAtt CustomResourceStack.Outputs.S3BucketRootName
6067
S3BucketRootArn: !GetAtt CustomResourceStack.Outputs.S3BucketRootArn

0 commit comments

Comments
 (0)