Wednesday, February 24, 2021

Create AWS CloudFront using CloudFormation

 



In the previous post we have created an application load balancer using CloudFormation. In this post we will configure CloudFront CDN using CloudFormation.


To create CDN, we first configure the cache policy, which configures the cache TTL, and which parameters of the request are used to create the key of the item in the cache.



CloudFrontCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
DefaultTTL: 10
MinTTL: 10
MaxTTL: 10
Name: po-cache-policy
ParametersInCacheKeyAndForwardedToOrigin:
EnableAcceptEncodingGzip: false
CookiesConfig:
CookieBehavior: none
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: all


Next we configure the origin request policy, which configures which of the request parameters are sent upstream to the origin web server.


CloudFrontOriginRequestPolicy:
Type: AWS::CloudFront::OriginRequestPolicy
Properties:
OriginRequestPolicyConfig:
Name: po-origin-policy
CookiesConfig:
CookieBehavior: all
HeadersConfig:
HeaderBehavior: allViewer
QueryStringsConfig:
QueryStringBehavior: all



Lastly we configure the CDN itself, and ask it to use the application load balancer that we have created n the previous post as its upstream server.



CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
HttpVersion: http2
IPV6Enabled: false
DefaultCacheBehavior:
CachePolicyId: !Ref CloudFrontCachePolicy
OriginRequestPolicyId: !Ref CloudFrontOriginRequestPolicy
TargetOriginId: !Ref ApplicationLoadBalancer
ViewerProtocolPolicy: allow-all
Origins:
- Id: !Ref ApplicationLoadBalancer
DomainName: !GetAtt ApplicationLoadBalancer.DNSName
CustomOriginConfig:
HTTPPort: 80
OriginProtocolPolicy: match-viewer
OriginSSLProtocols:
- TLSv1



Final Note


In this post we have created a CDN by configuring AWS CloudFront. In the next post, we will configure a Lambda@Edge for the CDN.


No comments:

Post a Comment