How to do it…

The first (and only) thing we need to do is configure a CloudFront distribution, as shown in the following diagram:

CloudFront Edge servers

Let's see how we can do that:

  1. Create a new CloudFormation template and start with the following code, which can be found in this book's GitHub repository. Name the file 03-02-Caching.yaml:
   AWSTemplateFormatVersion: '2010-09-09'
Parameters:
OriginDomainName:
Description: The hostname of your origin
(i.e. www.example.org.s3-website-ap-southeast-2.amazonaws.com)
Type: String
Aliases:
Description: Comma delimited list of aliases
(i.e. example.org,www.example.org)
Type: CommaDelimitedList

  1. Continue with the resources:
      Resources:
DistributionALogBucket:
Type: AWS::S3::Bucket
DistributionA:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName:
Ref: OriginDomainName
Id: OriginA
CustomOriginConfig:
OriginProtocolPolicy: http-only
Enabled: true
  1. Continue with the following code:
             Logging:
IncludeCookies: false
Bucket:
Fn::GetAtt: DistributionALogBucket.DomainName
Prefix: cf-distribution-a
Aliases:
Ref: Aliases
DefaultCacheBehavior:
TargetOriginId: OriginA
ForwardedValues:
QueryString: false
ViewerProtocolPolicy: allow-all
PriceClass: PriceClass_100
  1. Finally, we need to define the outputs:
Outputs:
DistributionDomainName:
Description: The domain name of the CloudFront Distribution
Value:
Fn::GetAtt: DistributionA.DomainName
LogBucket:
Description: Bucket where CloudFront logs will be stored
Value:
Ref: DistributionALogBucket
  1. Using the template we created, go ahead and create your CloudFront distribution. Expect to wait around 20-25 minutes for this stack to finish being created. It takes a while for your distribution configuration to be pushed out to all the AWS CloudFront locations:
aws cloudformation create-stack  
--stack-name cloudfont-cache-1
--template-body file://03-02-Caching.yaml
--parameters
ParameterKey=OriginDomainName,ParameterValue=<your-domain-name>
ParameterKey=Aliases,ParameterValue='<alias-1>,<alias-2>'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset