Writing and creating a Lambda@Edge function
To use Lambda@Edge, you write the code for your AWS Lambda function. Next, you set up Lambda to run the function based on specific CloudFront events, which are called triggers.
You can use the AWS Management Console to work with Lambda functions and CloudFront triggers, or you can work with Lambda@Edge programmatically by using the API.
Notes:
-
To use the console to create your Lambda function, you must use the Lambda console. You can't use the Amazon CloudFront console.
-
To add triggers for Lambda@Edgefunctions, you can use the Lambda console or CloudFront console.
-
To work with Lambda@Edge programmatically, see Related information.
Write your Lambda@Edge function
To help you write Lambda@Edge functions, see the following resources:
-
Lambda@Edge event structure – Understand the event structure to use with Lambda@Edge.
-
Lambda@Edge example functions – Example functions, such as A/B testing and generating an HTTP redirect.
The programming model for using Node.js or Python with Lambda@Edge is the same as using Lambda in an AWS Region. For more information, see Building Lambda functions with Node.js or Building Lambda functions with Python in the AWS Lambda Developer Guide.
In your Lambda@Edge function, include the callback
parameter and
return the applicable object for request or response events:
-
Request events – Include the
cf.request
object in the response.If you're generating a response, include the
cf.response
object in the response. For more information, see Generating HTTP responses in request triggers. -
Response events – Include the
cf.response
object in the response.