How to Configure the Cache Behavior for a CloudFront Download Distribution
AWS CloudFront is a content distribution service offered by AWS to serve low latency content with high data speed. CloudFront caches the object to its edge location. The user can configure how long an object can stay in the cache. When creating a CloudFront download distribution, it allows the user to configure the caching behavior for the different path patterns: E.g. one cache behavior is for all .css files while the other is for all .jpg files. When a new distribution is created, CloudFront forwards all requests to the origin specified during the creation of the distribution. The user can add another caching behavior for a different path pattern.
The present guide demonstrates how to configure the cache behavior for a download distribution.
1. Go to the AWS CloudFront console. The console lists all the existing streaming as well download distributions. Create a new download distribution. Go to the CloudFront configuration settings by clicking on the [i] button of any download distribution.
2. In the distribution configuration, select the “Behaviors” tab.
3. The Behaviors tab lists the existing cache behavior of the distribution. Select any existing behavior and click on “Edit”.
4. The cache behavior will show multiple fields, whereby the user can update the following information:
a. Path pattern: It is default (*) for a new distribution, but the user can configure it to apply to a particular file type, such as *.jpg, *.css. When CloudFront receives the request matching path pattern, it will apply the caching behavior to all the matching objects. The path applies to all the directories and sub-directories beneath the distribution origin.
b. Origin: This value lets the user distinguish multiple origins from one another in the same distribution. The description for each origin must be unique within the distribution.
c. Viewer Protocol Policy: It allows the end user to access objects through HTTP and HTTPS.
d. Object Caching: Object caching allows the user to specify the minimum time an object should stay in the cache. The value is specified in seconds in TTL filed. The image given below shows that the object will stay in the cache for 30 days. If the user is configuring headers with AWS S3, it is recommended to specify the “Use Origin Caching Headers”. If the user specifies the headers for S3 and still specifies a value for TTL, then AWS will make a decision.
5. The cache behavior also takes the following parameters:
a. Forward Cookie allows CloudFront to send cookies to the origin server. The user can configure to send all the cookies or only the selected cookies. To send the selected cookies, select the option “Whitelist” in forward cookies. CloudFront sends the cookie name-value pair but not the attributes, such as the cookie expiration time, and path while forwarding request. CloudFront will cache all the cookies and the attributes returned from the origin, which will be included in the viewer request. Further, if there are three parameters for an object, CloudFront will cache the same object thrice for each cookie key-value pair. If there is a cookie set for the streaming distribution, CloudFront will remove the cookie before sending it to the origin as it does not support cookies for the streaming distribution.
b. The Forward Query string is used to receive different versions of the object based on a query string in the URL. The option allows CloudFront to include the query string in the requested URL.
c. Restrict Viewer Access allows the user to configure security for the CloudFront distribution. Add the trusted signer if the user wants to access the distribution with the signed URLs.
6. The image given below shows the two cache behaviors. The user can configure multiple cache behaviors for different paths, as explained in step# 4.a.
Keywords: Amazon Web Services, AWS, Amazon AWS Console, AWS S3, Amazon CloudFront, AWS CloudFront, CloudFront, AWS EC2, AWS S3, Amazon S3, Download Distribution, AWS IAM, CloudFront Key Pairs, Trusted Signers, CDN, Content Distribution Network, Origin Caching Headers