What are AWS Lambda Triggers

What are 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.

aws lambda triggers - cloudfront

aws lambda triggers – CloudFront

CloudFront events for triggering Lambda@Edge functions are listed below:

– Viewer Request

The 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

The 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

The function will be executed when CloudFront gets a response from the origin and prior to caching the object in the response. The function will still execute when an error returns from the origin.

The function may not be executed in the below cases:

  • The requested file is found in the CloudFront cache + not expired.
  • The response gets generated from a function triggered by an origin request event.

– Viewer Response

The function will be executed prior to getting back the requested file to its viewer. The function will execute even if the file is already found in the CloudFront cache.

The function will not be executed in the below cases:

  • Origin returns an HTTP status code being 400 or greater.
  • The custom error page gets returned.
  • The response gets generated from a function triggered using a viewer request event.
  • CloudFront automatically redirects HTTP requests to HTTPS

How to Add Triggers Through the Lambda Console?

  1. Start by signing in to the Management Console and opening the Lambda console using the following link https://console.aws.amazon.com/lambda/.
  2. From the available Region list at the top right of your page, select US East (N. Virginia).
aws lambda triggers - region list

aws lambda triggers – region list

  1. From the Functionspage, select a name of which function you’d like to add triggers for.
aws lambda triggers - function

aws lambda triggers – function

  1. Click on Qualifiers, then select the Versions
aws lambda triggers - qualifiers

aws lambda triggers – qualifiers

  1. 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.

  1. Click on the Triggers
  2. Select Add triggers.
  1. For the Add trigger dialog box, select the dotted box, then click on CloudFront.

Keep Note

In case of previously creating 1 or more triggers, CloudFront is going to be the default service.

  1. Set the following values in order to indicate at the time of Lambda function execution.

Distribution ID

Select the ID of which distribution you’d like to add the trigger to.

Cache behavior

Select which cache behavior specifies the objects you’d like the function to be executed on.

Take Note

Specifying * for the cache behavior enables the Lambda function to deploy to the default cache behavior.

CloudFront event

Select which CloudFront event will be causing your function to get executed.

Include body

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.

  1. Click on Submit.

 

How to Add Triggers Through the CloudFront Console?

1. 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.

Important Note

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:

arn:aws:lambda:us-east-1:123456789012:function:TestFunction:2

The last number stated at the end (2 in the mentioned example) refers to the version number of your chosen function.

2. Go to the CloudFront console through this link https://console.aws.amazon.com/cloudfront/.

3. From under the list of distributions, select the ID of a specific distribution for which you’d like to add triggers.

4. Select the Behaviors

5. Choose the check box to the cache behavior for adding triggers, and later select Edit.

6. 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.

7. Paste the ARN of the Lambda function you’ll like to get executed as soon as the selected event takes place. It will be the value that you have previously copied in step 1.

8. 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.

9. For executing the exact function for additional event types, select +and keep repeating steps 6 + 7.

10. Select Yes, Edit.

11. For adding triggers to additional cache behaviors for this distribution, you will need to go ahead and repeat steps 5 to 9.

See Also

AWS lambda Throttle