AWS Lambda Triggers
CloudFront Events for Triggering a Lambda Function
For every single cache behavior found in a CloudFront distribution, it’s possible to add up to 4 triggers for a Lambda function to start executing upon the occurrence of specific CloudFront events. CloudFront triggers may be based upon 1 of 4 CloudFront events, just like what is displayed in the below diagram.
CloudFront events for triggering Lambda@Edge functions are listed below:
– Viewer Request
Function will be executed as soon as CloudFront receives a request from any viewer, prior to checking if the requested object is found in the CloudFront cache or not.
– Origin Request
Function will be executed merely as soon as CloudFront forwards a request to your origin. Upon having the requested object found in the CloudFront cache, its function shall not be executed.
– Origin Response
Function will be executed when CloudFront gets a response from the origin and prior to caching the object in the response. Function will still execute when an error returns from the origin.
Function may not be executed in the below cases:
- Requested file is found in the CloudFront cache + not expired.
- Response gets generated from a function triggered by an origin request event.
– Viewer Response
Function will be executed prior to getting back the requested file to its viewer. Function will execute even if the file is already found in the CloudFront cache.
Function will not be executed in the below cases:
- Origin returns an HTTP status code being 400 or greater.
- Custom error page gets returned.
- Response gets generated from a function triggered using a viewer request event.
- CloudFront automatically redirects HTTP request to HTTPS
How to Add Triggers Through the Lambda Console?
- Start by signing in to the Management Console and opening the Lambda console using the following link https://console.aws.amazon.com/lambda/.
- From the available Region list at the top right of your page, select US East (N. Virginia).
- From the Functions page, select a name of which function you’d like to add triggers for.
- Click on Qualifiers, then select the Versions Tab.
- Select which version you’d like to add triggers to.
Upon choosing a version, the button’s name will be changed to Version: $LATEST – Version: the version number.
- Click on the Triggers
- Select Add triggers.
- For the Add trigger dialog box, select the dotted box, then click on CloudFront.
In case of previously creating 1 or more triggers, CloudFront is going to be the default service.
- Set the following values in order to indicate at the time of Lambda function execution.
Select the ID of which distribution you’d like to add the trigger to.
Select which cache behavior specifying the objects you’d like the function to be executed on.
Specifying * for the cache behavior enables the Lambda function to deploy to default cache behavior.
Select which CloudFront event will be causing your function to get executed.
Choose this check box for accessing the request body in the chosen function.
Enable trigger and replicate
Choose this check box to allow Lambda to replicate your function to other Regions globally.
- Click on Submit.
How to Add Triggers Through the CloudFront Console?
- Find the ARN of which Lambda function you’d like triggers to be added triggers for:
– Start by signing in to the Management Console and then opening the Lambda console through this link https://console.aws.amazon.com/lambda/.
– From the list of available Regions at the top right of your page, select US East (N. Virginia).
– From under the list of functions, select the name of which function you’d like to get triggers added to.
– Click on Qualifiers, select the Versions tab, and click on which numbered version you’d like triggers to be added to.
Triggers may be added to a numbered version and not $LATEST.
– Go ahead and copy the ARN which shows at the very top of your page, as an example:
The last number stated at the end (2 in the mentioned example) refers to the version number of your chosen function.
- Go to the CloudFront console through this link https://console.aws.amazon.com/cloudfront/.
- From under the list of distributions, select the ID of a specific distribution for which you’d like to add triggers.
- Select the Behaviors
- Choose the check box to the cache behavior for adding triggers to, and later select Edit.
- For Lambda Function Associations, from the Event Type list, select the time you’d like the function to get executed: for viewer requests, responses, origin requests or responses.
- Paste the ARN of the Lambda function you’ like to get executed as soon as the selected event takes place. It will be the value which you have previously copied in step 1.
- Choose Include Body for accessing the request body in the function.
In case you simply need to replace the request body, you don’t need to select this option is not necessary for being selected.
- For executing the exact function for additional event types, select +and keep repeating steps 6 + 7.
- Select Yes, Edit.
- For adding triggers to additional cache behaviors for this distribution, you will need to go ahead and repeat steps 5 till 9.