Creating Cache Behavior

Dynamic content generation with Lambda@Edge

In the next sections, you use Lambda@Edge to generate a web page dynamically. Instead of printing the raw request headers, the function returns their values in an HTML table. You will grant permissions to Lambda@Edge, write and deploy the function code, test it, and then use the function logs and metrics to troubleshoot it.

Cache Behavior

In this section, you create a new cache behavior in your CloudFront distribution. Later, you attach the Lambda@Edge function to this cache behavior.

  1. Open the CloudFront console, select Distributions, then select the distribution you created in Lab 1.
  • Select the Behaviors tab, then click Create behavior.

CloudFront Lab

  1. In the Settings section, configure:
  • Path pattern: /serverless
  • Origin and origin groups: select the EC2 origin you created in Step 1 CloudFront Lab

The path pattern /serverless has no wildcard, so this behavior matches only requests to exactly /serverless. Requests such as /serverless/abc still fall through to the other behaviors.

  1. Scroll down to the Cache key and origin requests section and keep Cache policy and origin request policy (recommended) selected.
  • Cache policy: select CachingDisabled.
  • Origin request policy - optional: select AllViewer.
  • Leave all other settings at their default values.

CloudFront Lab

CachingDisabled makes CloudFront forward every request to the origin, so each request runs the function and returns fresh content. AllViewer forwards all viewer headers, cookies and query strings, so the function can read every request header.

  1. Scroll to the bottom of the page and click Create behavior.
  • A Successfully created new cache behavior /serverless message appears

CloudFront Lab

The Last modified field shows Deploying while CloudFront pushes the change to all edge locations. Wait until it shows a date and time before you test the /serverless path.