Invoking AWS lambda from S3

Learning Objectives

Dhiraj Mishra
Analytics Vidhya
5 min readOct 12, 2020

--

  1. Setting up the AWS Account
  2. Overview of AWS services S3, Lambda, IAM, etc
  3. Creating IAM Role for Lambda
  4. Creating a Lambda Function
  5. Creating a Trigger for the Lambda Function

Setting up the AWS Account

Before starting, kindly have your AWS account in place. You can use AWS Free Tier, the AWS Free Tier is automatically activated on each new AWS account. With the AWS Free Tier, you can try out some AWS services free of charge within certain usage limits.

The AWS Free Tier includes three different types of offerings:

  • Trials: These are short-term trial offers that start from the date that you activate a service. You pay standard rates after the trial period expires.
  • 12-Months free: These offers provide limited usage for 12 months after your initial sign-up date. You pay standard rates after your 12 months free usage term expires or if your application use exceeds the free tier limits.
  • Always free: These offers are available to all AWS customers and don’t expire at the end of your 12-month AWS Free Tier term.

However, not all AWS services are free. You can track your Free Tier usage with the AWS Free Tier usage alerts.

Overview of AWS services S3, Lambda, IAM, etc

  • IAM: AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.
  • S3: Amazon Simple Storage Service (Amazon S3) is storage for the internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web.
image source: AWS
  • Lambda: AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.
image source: AWS

Components of AWS Lambda

Functions: A Lambda function consists of code you provide, associated dependencies, and configuration. It’s an independent unit of deployment. A Lambda function will be trigger by an event, so the more events you have, the more Lambda functions will be invoked. Moreover, Lambda functions can trigger other lambda functions synchronously or asynchronously.

Triggers: Triggers are AWS services or resources that invoke your Lambda function. You can invoke Lambda functions directly with the Lambda console, the Lambda API, the AWS SDK, the AWS CLI, and AWS toolkits. You can also configure other AWS services to invoke your function, or you can configure Lambda to read from a stream or queue and invoke your function.

Trigger Type / Invocations

  • Synchronous invocation: You wait for the function to process the event and return a response.
  • Asynchronous invocation: Lambda queues the event for processing and returns a response immediately.

For asynchronous invocation, Lambda handles retries and can send invocation records to a destination.

Destinations: Destinations are AWS resources that receive a record of an invocation after it succeeds or fails. If you want to set up a destination, you should create a role and attach related permissions to authorize Lambda function to access AWS resources.

Run code without thinking about servers. Pay only for the compute time you consume — AWS

Creating IAM Role for Lambda

If a Lambda function needs to call other AWS services, we need to create an IAM role that it can assume at execution time. Create an IAM role for our Lambda function to be able to connect with S3 and CloudWatch Logs.

Navigate to the IAM console page, select Roles from the left-hand menu and create a new role if you don’t have any for lambda.

image source: AWS
image source: AWS

Select Lambda as the trusted entity, add the following managed policies:

  • AmazonS3ReadOnlyAccess
  • CloudWatchLogsFullAccess

Give a Name to the role “S3-lambda-role”.

Creating a Lambda Function

Navigate to the AWS Lambda console page.

Select Create function, create a function from scratch name it“S3-Lambda-Fxn”. Select Python 3.6 you can choose your version.

image source: AWS

Use the policy created from the previous task.

image source: AWS
image source: AWS

Creating a Trigger for the Lambda Function

Now it’s time to configure our Lambda function to be triggered by an S3 event.

Configuring S3 Event for Lambda

  1. Navigate to the AWS S3 console page.
  2. Navigate into the settings for our input-… bucket.
  3. Navigate into the Properties of the bucket.
  4. Scroll to Events for that bucket.
  5. Inside the Events window, select Add notification and set with the following properties:
    Add a meaningful name for the event, check All object create events and in “Send to” select the Lambda function created in the previous task and Save.

Confirming if S3 event trigger was added to our Lambda Function.

image source: AWS

Try triggering an S3 event and check the CloudWatch logs and you would see the message displayed.

Photo by Stepan Unar on Unsplash

--

--