Hey readers! I’m going to show you that how to read file data from S3 on Lambda trigger. Before moving further, I assume that you have a basic intuition about Amazon Web Service and its services. We are going to see few AWS services here, which are IAM, S3 and Lambda. If you aware about the basics of the mentioned service, keep reading or please go through the AWS documentation.

We will start with creating the IAM service. IAM role is “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.

To read the file, we need to assign S3 read access permission to Lambda. Apart from that, we will require the logs to inspect the errors or for various purpose. So, we will assign cloud watch access permission to Lambda for writing logs. Let’s go ahead and create the role.

 

AWS IAM

Please login into the AWS console and follow below steps to create IAM role.

  • Navigate to IAM service
  • Click on Roles from left panel & Create role
  • Choose Lambda under “Choose the service that will use this role” & Next:Permissions
  • Search and select AWSLambdaExecute & Next:Review
  • Define the role name “lambda_s3_access” & Create role. After creation it should look like below screenshot. Here we are done with IAM role creation.

 

AWS: Reading File content from S3 on trigger

IAM Role

 

AWS S3

Now, lets jump to the AWS S3 service. We will create the bucket, in order to set the trigger on the AWS Lambda function. Let’s go ahead and create the S3 bucket. Please follow below steps to create bucket.

  • Navigate to S3 Service
  • Click on Create Bucket
  • Define the bucket name & select the Region
  • Click on Create. Here we are done with bucket creation.

 

AWS Lambda

Now, lets jump to the AWS Lambda service. Once you are there, follow the below steps.

  • Navigate to AWS Lambda service
  • Click on Create function
  • Define the Function Name, Runtime as Python 3.6 and choose the role, that we just created in IAM section (i.e. “lambda_s3_access”) and click on Create function

Once the function is created, we will set the S3 bucket trigger. Please follow below steps.

  • Under Designer section -> Add triggers & select S3
  • Under Configure triggers, select the bucket that we created under S3 section
  • Select Event type based on your requirement. But here we will go with Object Created (All) & click on Add and Save
  • Under the Function code -> lambda_function, use this code from GitHub and paste and Save.

When you are done, go ahead and upload a file (ex. csv) to the S3 bucket and it will trigger the Lambda function. Go ahead and check the AWS Lambda function logs. Please refer the below video for reference.

 

More importantly, make sure that the AWS Lambda function and the S3 bucket are in the same region.

 

 

Keep sharing and stay tuned for more. Follow me in Twitter