/
1-s3-buckets-stack.yaml
98 lines (90 loc) · 4.17 KB
/
1-s3-buckets-stack.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
AWSTemplateFormatVersion: 2010-09-09
## =================== DESCRIPTION =================== ##
Description: >-
AWS CloudFormation sample template
- Create S3 bucket for subdomain (such as www.example.com) and configure it to host a static website
- Create S3 bucket for root domain (such as example.com) and set it up to redirect requests to S3 bucket for subdomain (such as from example.com to www.example.com)
- Note: both buckets are private. A new policy for S3 bucket for subdomain will be created in `3-cloudfront-stack-v1.yaml` template to let CloudFront OAI access S3 bucket content
## ===================== METADATA ===================== ##
Metadata:
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: DNS parameters for S3 buckets
Parameters:
- paramRootDomain
- paramSubdomain
- Label:
default: AWS tag parameters
Parameters:
- paramUniqueTagName
## ==================== PARAMETERS ==================== ##
Parameters:
paramRootDomain:
Description: Specify a root domain for your website (such as example.com)
Type: String
paramSubdomain:
Description: OPTIONAL. Specify a subdomain (such as 'www' or 'apex' for www.example.com or apex.example.com). You can leave it empty to skip.
Type: String
Default: www
paramUniqueTagName:
Description: Specify a unique name for tag
Type: String
Default: static-website-hosting-to-s3
AllowedPattern: "[\\x20-\\x7E]*"
ConstraintDescription: Must contain only ASCII characters
## ==================== MAPPINGS ==================== ##
# Mappings:
## ==================== CONDITIONS ==================== ##
# Conditions:
## =================== RESOURCES =================== ##
Resources:
# create S3 bucket for subdomain (such as www.example.com) and configure it to host a static website
myS3BucketForSubdomain:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain # keep S3 bucket when its stack is deleted
Properties:
BucketName: !Sub ${paramSubdomain}.${paramRootDomain} # use the name of subdomain with domain, such as www.example.com
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
VersioningConfiguration: # turn versioning on in case we need to rollback newly built files to older version
Status: Enabled
AccessControl: BucketOwnerFullControl
Tags:
- Key: mastering-cloudformation
Value: !Ref paramUniqueTagName
# create S3 bucket for root domain (such as example.com) and set it up to redirect requests to S3 bucket for subdomain (such as from example.com to www.example.com)
myS3BucketForRootDomain:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain # keep S3 bucket when its stack is deleted
Properties:
BucketName: !Ref paramRootDomain # use the name of your domain, such as example.com
WebsiteConfiguration:
RedirectAllRequestsTo: # Configure the bucket to route traffic to the subdomain bucket
HostName: !Ref myS3BucketForSubdomain
Protocol: https
AccessControl: BucketOwnerFullControl
Tags:
- Key: mastering-cloudformation
Value: !Ref paramUniqueTagName
## ======================= OUTPUT ====================== ##
Outputs:
outputS3WebsiteURLForRootDomain:
Description: Amazon S3 website endpoint for root domain
Value: !GetAtt myS3BucketForRootDomain.WebsiteURL
outputS3DomainNameForRootDomain:
Description: IPv4 DNS name of S3 bucket for root domain
Value: !GetAtt myS3BucketForRootDomain.DomainName
outputS3RegionalDomainNameForRootDomain:
Description: Regional domain name of S3 bucket for root domain
Value: !GetAtt myS3BucketForRootDomain.RegionalDomainName
outputS3WebsiteURLForSubdomain:
Description: Amazon S3 website endpoint for subdomain
Value: !GetAtt myS3BucketForSubdomain.WebsiteURL
outputS3DomainNameForSubdomain:
Description: IPv4 DNS name of S3 bucket for subdomain
Value: !GetAtt myS3BucketForSubdomain.DomainName
outputS3RegionalDomainNameForSubdomain:
Description: Regional domain name of S3 bucket for subdomain
Value: !GetAtt myS3BucketForSubdomain.RegionalDomainName