Invoking AWS lambda from S3
Learning Objectives
- Setting up the AWS Account
- Overview of AWS services S3, Lambda, IAM, etc
- Creating IAM Role for Lambda
- Creating a Lambda Function
- 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.
- 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.
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.
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.
Use the policy created from the previous task.
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
- Navigate to the AWS S3 console page.
- Navigate into the settings for our input-… bucket.
- Navigate into the Properties of the bucket.
- Scroll to Events for that bucket.
- 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.