Get started with CloudFront - Part 3: Response Headers and Cache Behavior
Published: 2023-03-01 20:17:21

#### Table of Content

1. [Response Headers]()
2. [Cache Behavior]()


#### 1. Response Headers

In this section, you will configure the security headers in the S3 response returned by your Distribution CloudFront. You don't need to configure your origin or use Lambda@Edge or CloudFront custom functions to insert these headers. Amazon CloudFront now supports configurable response headers.

Go to the AWS Management Console and select the CloudFront service.

- Select the Distributions tab and select your Distributions ID.
- In the Behaviors tab, select Default(*) behavior and click the Edit button.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zywngkyda6l6amrkszax.png)

In the "Cache key and origin requests" section, under "Response headers policy - optional", open the drop-down list to see a list of options. Select "Security headers" and click the "Save changes" button.
- This step will set up a response policy that matches the security header you added in the previous step. When requests come to CloudFront, security headers are inserted into the S3 response from your Distribution. This helps to protect your website from various attacks.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ahvvncs1x06fq6n5j5w.png)

Update the Inbound rule of the security group, change **MyIP** to **Anywhere**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v55ulhvbauojlxvabpdy.png)

This Management Response Policy will add a set of security headers to all responses that CloudFront sends to viewers. For more information about these security headers, see Mozilla's web security guide.

- With this response policy, CloudFront will add X-Content-Type-Options: nosniff to all responses, regardless of whether the response CloudFront receives from Origin contains this header. For all other headers in this policy, if the response CloudFront receives from Origin contains that header, CloudFront uses the header received (and its value) in its response to its viewers. , instead of using the header in this policy.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4v98wbdnhnrrvktoyrb.png)

Return to your site with your CloudFront URL (eg http://dxxxx.cloudfront.net), open your browser's developer tools and check the response headers sent by CloudFront. Security headers added using the response policy that you have configured will be listed in the "Response Headers" section of the response.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhy76bugae47oa0rqfor.png)

- You learned how to create a Distribution with multiple origins and multiple behaviors to serve static content and forward dynamic content API to your origin.

- You learned how to set up an origin group to provide rerouting in the event of a problem.

- You also learned how to set up a custom error page and disable content on the fly.

- Finally, you learned about some important CloudFront headers for debugging and how to add a response policy to the response.


#### Cache Behavior


In this section, you will create a new Cache Behavior in the CloudFront distribution you created. You will use this Cache Behavior to connect the Lambda@Edge function that you will write.

- Go to the CloudFront dashboard and select the distribution you created in Part 1.

- Go to "Cache Behaviors" tab and click "Create Behavior"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lueubolftkhyjlumslf8.png)

Make the following configuration:

   - Path : "/serverless"

   - Origin: <Select the EC2 origin created in Lab 1>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vajod0rl2qe62y6oetew.png)

Cache Policy: “Managed-CachingDisabled”

   - Origin Request Policy: “Managed-AllViewer”

   - Leave all other settings at default values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgu1b6vs8j0n4bbnduw3.png)

Click “Create”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/031csm3jca3y2qhb29ox.png)

Finish creating the new Cache Behavior.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgcwd65ub8znszwiie7g.png)

### The following sections

[Get started with CloudFront - Part 1: CloudFront Distribution](https://dev.to/aws-builders/get-started-with-cloudfront-cloudfront-distribution-2n1m)

[Get started with CloudFront - Part 2: Distribution Invalidations](https://dev.to/aws-builders/get-started-with-cloudfront-part-2-distribution-invalidations-2d4)

[Get started with CloudFront - Part 3: Response Headers and Cache Behavior](https://dev.to/aws-builders/get-started-with-cloudfront-part-3-response-headersand-cache-behavior-2n5e)

[Get started with CloudFront - Part 4: Lambda@Edge function](https://dev.to/aws-builders/get-started-with-cloudfront-part-4-lambdaedge-function-4cbh)