AWS ServerLess Example Using Lambda
AWS Lambda an Example
In this post ‘AWS ServerLess Example Using Lambda’ you will learn about creating an AWS Lambda Function, S3 & DynamoDB.
In short when this post covers the below flow, when a file is uploaded into S3 Bucket an AWS Lambda function will be triggered and meta information will be inserted into a DynamoDB.
AWS Lambda Introduction
A serverless compute service. However it allows you to run your code with you provisioning an instance, the resource provision is done automatically according to your running code. This service lets you to run code the without provisioning or managing servers. At the time of writing this post, Lambda can be directly triggered by AWS services such as S3, DynamoDB, Kinesis, SNS, CloudWatch and Step functions. You can make AWS Lambda to trigger the code in response to your actions such as changes in data, shifts in system state, or actions by users.
Steps to Create AWS Lambda Function – Execution
Create a DynamoDB
First of all we will Create a DynamoDB table for storing employee details. Call it as ’emp’. With basic columns emp_id, fname, lname,dob.
S3 – Create a bucket and place the file
Create a S3 Bucket to upload the json inbound files(resource section).
IAM: Create a Policy with below services,
Resources = All Resources (not recommended) , else provide specific S3 Bucket ARN.
CloudWatch Logs –> this will help to read the logs to analysis or debgu the lambda function.
Resources = All Resourcs
Resources = All Resources
IAM: Create a Role
- Select ‘Lamdba’ Service -> As Lambda function will call S3 & Dynamo DB to read and upload the file.
- Attach the above created policy as per Step-3.
Create Lamdba Function -> Compute: Select Lambda
For this post AWS Lambda Examples, we will create below AWS Lambda function
- Author from Scratch.
- Name = <ur lambda fn >
- Runtime = Python 3.6 (or any other code you are comfortable) .In this example i have used Python. Check in resource section for source code.
- Role = Existing role -> select the role selected as per Step-4.
- The resource section below has the sample code used in this post.
- Add Trigger – S3.
- Select the bucket name as created in Step-2.
- Event type = ‘Object Created(All)’ –> this is the triggering event, like when a file is uploaded into above S3 bucket, this event will be triggered.
- Prefix = your bucket/file prefix — > you can leave this empty.
- Suffix = your file extention –> you can leave this empty.
- Save the changes.
Upload the file into S3 bucket as created in Step-2. (sample file is available in below resource section).
Once a file is Attached, you can check for the logs @ CloudWatch -> Log Groups -> your Lambda function name, order by time of trigger.
If all are success, a row should have be inserted into DynamoDb table ’emp’.
In this post ‘AWS ServerLess Example Using Lambda’ you seen simple use case to trigger the Lambda service through a S3 event. AWS Lambda is an very import service and you can refer AWS Serivces for a little more detail description on Lambda.