Menu
Amazon CloudFront
Developer Guide (API Version 2016-09-29)

Generating HTTP Responses

You can use a Lambda function to generate HTTP responses when the following CloudFront events occur:

  • Viewer request events – When the function is triggered by a viewer request event, CloudFront doesn't check the edge cache or forward a request to your origin.

  • Origin request events – When the function is triggered by an origin request event, CloudFront checks the edge cache first and forwards the request to your origin if the requested object is not in the cache.

The headers and body in the generated response can be based on the content of the HTTP request. For both types of events, using Lambda functions to generate HTTP responses reduces the load on the origin.

For some examples of generating HTTP responses, see Example Functions.

Programming Model

This section provides details about the programming model for using Lambda@Edge to generate HTTP responses.

Errors

The following are possible errors for generated HTTP responses.

HTTP No Content Response Contains Body Field

HTTP 204 status codes (No Content) responses that are generated from CloudFront viewer requests should not include a message body. If the message body is included in the response, CloudFront returns an HTTP 502 status code (Bad Gateway) to the viewer.

This is because Lambda@Edge imposes the optional restriction found in RFC 2616, which states that an HTTP 204 response does not need to contain a message body.

Response Size Limits

The maximum size of a response that is generated by a Lambda function (including the headers and body) depends on the event that triggered the function:

  • Viewer request events – 40 KB

  • Origin request events – 256 KB

If the response is larger than the allowed size, CloudFront returns an HTTP 502 status code (Bad Gateway) to the viewer.

Required Fields

The status field is required.

All other fields are optional.

On this page: