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 Trigger - CloudFront Triggers

AWS Lambda Trigger – CloudFront Triggers

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?

  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 Trigger - Select Region

AWS Lambda Trigger – Select Region

  1. From the Functions page, select a name of which function you’d like to add triggers for.
AWS Lambda Trigger - Select Function for Adding Triggers

AWS Lambda Trigger – Select Function for Adding Triggers

  1. Click on Qualifiers, then select the Versions Tab.
AWS Lambda Trigger - Select Version for adding triggers

AWS Lambda Trigger – Select Version for adding triggers

  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.
AWS Lambda Trigger - Add Trigger Button

AWS Lambda Trigger – Add Trigger Button

  1. For the Add trigger dialog box, select the dotted box, then click on CloudFront.
AWS Lambda Trigger - Select CloudFront as Trigger Configuration

AWS Lambda Trigger – Select CloudFront as Trigger Configuration

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

  1. Go to the CloudFront console through this link https://console.aws.amazon.com/cloudfront/.
  2. From under the list of distributions, select the ID of a specific distribution for which you’d like to add triggers.
  3. Select the Behaviors
  4. Choose the check box to the cache behavior for adding triggers to, and later select Edit.
  5. 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.
  6. 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.
  7. 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.

  1. For executing the exact function for additional event types, select +and keep repeating steps 6 + 7.
  2. Select Yes, Edit.
  3. For adding triggers to additional cache behaviors for this distribution, you will need to go ahead and repeat steps 5 till 9.

aws lambda rds


AUTHOR