Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jonoirwinrsa/ceb2cba3d104720320f30e06b8c821f8 to your computer and use it in GitHub Desktop.
Save jonoirwinrsa/ceb2cba3d104720320f30e06b8c821f8 to your computer and use it in GitHub Desktop.
Description: >
Script to create a SSL certificate, S3 bucket and Cloudfront distribution.
###############################################################################
Parameters:
###############################################################################
DomainName:
Type: String
Description: The domain name.
Default: webapp.example.com
PriceClass:
Type: String
Description: The CloudFront distribution price class
Default: 'PriceClass_All'
AllowedValues:
- 'PriceClass_100'
- 'PriceClass_200'
- 'PriceClass_All'
###############################################################################
Resources:
###############################################################################
Cert:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref DomainName
DomainValidationOptions:
- DomainName: !Ref DomainName
ValidationDomain: !Ref DomainName
ValidationMethod: DNS
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !Ref DomainName
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: false
TargetOriginId: the-s3-bucket
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCachingMinTTL: 300
ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCachingMinTTL: 300
ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
Enabled: true
HttpVersion: http2
Origins:
- DomainName: !Join ['', [!Ref Bucket, '.s3.amazonaws.com']]
Id: the-s3-bucket
S3OriginConfig:
OriginAccessIdentity:
!Join ['', ['origin-access-identity/cloudfront/', !Ref CloudFrontOriginAccessIdentity]]
PriceClass: !Ref PriceClass
ViewerCertificate:
AcmCertificateArn: !Ref Cert
MinimumProtocolVersion: TLSv1
SslSupportMethod: sni-only
Tags:
- Key: Domain
Value: !Ref DomainName
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub 'CloudFront OAI for ${DomainName}'
Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
Tags:
- Key: Domain
Value: !Ref DomainName
BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref Bucket
PolicyDocument:
Statement:
- Action:
- s3:GetObject
Effect: Allow
Resource: !Join ['', ['arn:aws:s3:::', !Ref Bucket, '/*']]
Principal:
CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment