AWS Lambda Invocation using Amazon S3

10 / Apr / 2015 by Ankit Giri 3 comments

To start, we create a Lambda function to consume events published by Amazon S3. For any object uploaded to a bucket, S3 will invoke our Lambda function by passing event information in the form of function parameters. AWS Lambda executes the function. As the function executes, it reads the S3 event data, logs some of the event information to Amazon CloudWatch. This is an example of “push” model where Amazon S3 invokes the Lambda function.So, let’s get started with AWS Lambda Amazon S3 Invocation.

For more information on the “push” model, see AWS Lambda: How it Works


In this Demo, we will do the following:

  • Create a Lambda function to process Amazon S3 events and test it by invoking it manually by using sample Amazon S3 event data. The function reads incoming event data and writes logs to Amazon CloudWatch.
  • Configure an S3 bucket notification so that Amazon S3 can publish object-created events to AWS Lambda by invoking your Lambda function.

We can then upload objects to the bucket. Each object upload will cause Amazon S3 to invoke the Lambda function. You can verify AWS Lambda executed your function by reviewing the logs.

Both the Lambda function and the S3 bucket must be in the same AWS region. This exercise assumes the us-east-1 (Virginia) region.

Step 1: Create a Lambda Function and Invoke it Manually (Using the Console)

In this step, we create and invoke a Lambda function:

Go to the AWS Management Console, select “Lambda” service & click on “Create a Lambda Function”.



Select the following options:

Name: Give a appropriate name to Lambda Function ( we have named it S3)

Description: This should ideally tell what the function will do.

Code entry type: Edit Code inline

Code Template: None (from the drop-down menu)

  • We will use the following JavaScript code that console provides to create a Lambda function. The code simply reads the Amazon S3 event data it receives as parameter and logs some of the information to Amazon CloudWatch Logs. The “console.log()” statements in the code generate log events in CloudWatch.

console.log('Loading function');
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));
    // Get the object from the event and show its content type
    var bucket = event.Records[0];
    var key = event.Records[0].s3.object.key;
    s3.getObject({Bucket: bucket, Key: key}, function(err, data) {
        if (err) {
            console.log("Error getting object " + key + " from bucket " + bucket +
                ". Make sure they exist and your bucket is in the same region as this function.");
   ("Error getting file: " + err)      
        } else {
            console.log('CONTENT TYPE:', data.ContentType);

  • Role*: S3 Execution role (Under Create a new role)A new window will open up, give the appropriate name to the Role (we have named it as  ( lambda_S3_exec_role) & click on “allow”.


Click on “Create Lambda Function”.

Step 2: Configure Amazon S3 as the Event Source 

In this step, we add Amazon S3 as the event source for Lambda function. We will be using a already created S3 Bucket “s3lambda”. The service Lambda does the following:

  • Adds a permission, in the access policy associated with Lambda function, to allow S3 to invoke the function.
  • Adds notification configuration on the S3 bucket identifying the event type (object-created events) and Lambda function. When  S3 detects the event of the specified type, it invokes the Lambda function by passing event information as function parameters. 

Step 2.1: Add Amazon S3 as the Event Source

Follow the steps to add Amazon S3 as the event source for our Lambda function. 

  1. In the AWS Lambda console, add an event source for our Lambda function.
    1. On the Lambda: Function List page, click the triangle to the left of the function name to view expanded function details.LL2

    2. b. From the Actions drop-down, select Add Event Source.


    3. In the dialog box that opens, select “S3” from the Event source type drop-down.

    4. Select the bucket “s3lambda” from the Bucket drop-down. (In your case, it has to be the bucket you created)

    5. Select Object Created from the Event type drop-down.

    6. Click Submit.This will add Amazon S3 as the event source for the Lambda function. The console will also do the following to complete the setup:

      1. Add a permission in the function access policy to grant Amazon S3 principal permission to invoke the function.
      2. Add notification configuration on the S3 bucket so that Amazon S3 can publish object-created events to AWS Lambda by invoking the specific function.

You will see the following screen:LL4

Step 2.2: Test the Setup

To test the setup, we will do the following:

  1. Upload an object to the S3 bucket. Amazon S3 will detect the object-created event and invoke our Lambda function.AWS Lambda will then execute our function. As the function executes, it reads Amazon S3 event data it received as parameters, and logs some of the event information to CloudWatch Logs.
  2. You can verify your logs by using the following steps:
    1. Review function logs in the AWS Lambda console. For the specific function, the logs appear under the CloudWatch Metrics at a glance heading.
    2. You can click the logs links to open review the logs in the CloudWatch console.

LL Success

The console will look like this, once a object is uploaded in the bucket.

Invocation count : Number of invocations done by S3 on Lambda Function.
Invocation duration : Duration of the invocation.
Invocation failures : Number of invocations failed by S3 on Lambda Function.
Throttled invocations : Request that gets invoked asynchronously can absorb reasonable bursts of traffic for approximately 15-30 minutes.


comments (3)

  1. Jagadeesh

    I have 7 GB file in S3 bucket. On this object put request, i need to process the object and store the resultant object to another bucket. Is this possible using lambda?

  2. Pingback: Introduction To AWS LAMBDA | TO THE NEW Blog

Leave a Reply

Your email address will not be published. Required fields are marked *