AWS Lambda -Serverless Compute Service

Kalpana Sharma
3 min readApr 13, 2021

Learn how to create a lambda function that will trigger when we upload any files into the S3 bucket.

Here we will print the transaction type that we have added in the document in the lambda function.

LAMBDA Function:

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic.

Benefits:

  1. No Servers to manage
  2. Continues scaling
  3. Cost-optimized with milliseconds metering
  4. Consistent performance at any scale

Steps on AWS console:

We will do this in three steps

  1. Create an IAM role and policy through which we will connect to S3
  2. Create an S3 bucket in which you will upload your document or file and set an event that will trigger the lambda
  3. Create a lambda function that will print the document content.

1) AWS IAM Role:

  1. Go to AWS IAM service and got to the Roles section
  2. Click on Create Role.
  3. Chose a use case as Lambda and click on Next
  4. We need to create a policy so first we will check the existing policy for the service and see if anything fits our requirement. We need S3 full access policy, select that.
  5. Also, select the AWSLambdaBasicExecutionRule policy as well to see the cloud watch log
  6. Name the role as “AWSLambdaDemoRole”

2) Create a Lambda function:

  1. Got to AWS Lambda service and create a lambda function click on author from scratch option
  2. Choose a name for a function and select the language you want to write your function. I chose python 3.8
  3. Click on Change execution role and choose to create a new role from AWS existing template Create a Role with existing S3 policy (Amazon S3 object read-only permissions)
  4. Rest settings keep default and click on create the function
  5. You will see the function code option put your code in there and deploy the function.
  6. In the lambda function, you will see the trigger as S3.

Here is the python code:

3) Create an S3 Bucket:

  1. Go to AWS S3 Service and create a bucket
  2. Create a bucket with default settings
  3. Got to S3 Bucket ->Properties->Event Notifications->click on Event Notification
  4. Give the Event name and event type in this case it will be “putObject”
  5. Choose Destination as Lambda function and choose the lambda function you want to trigger on event.

4) Upload Document to S3 and output:

  1. Upload the JSON document in the S3 bucket for which event has been setup
  2. Got to lambda function ->monitoring ->View logs in cloudwatch
  3. Go to Log groups ->Select the lambda function ->check in the log stream the output you should see the document content.

Here is the JSON document code

--

--