Creating your first AWS Lambda function

Kirthi J
2 min readMar 12, 2023

--

What is it?

A serverless, event-driven compute service that lets you run any service or even an entire application virtually without managing servers.

Why do you need it?

It lets you go serverless. Well, the servers are still there. You just don’t need to manage them anymore.

How to start using it?

For starters, you can move a specific function/module of your code to lambda. For example, your authentication/callback module can be moved to lambda. You just need to trigger on when the lambda function should be called.

Now let’s see how we can create an AWS lambda.

There are three ways to create a lambda function — from scratch, using a blueprint or a container image.

For code having size upto 50MB, use from scratch option and directly upload the code as a zip or to S3 and use it. However, if your code exceeds 250MB you can opt for container image and upload your code to Amazon ECR and use the container URI. Containers support upto 10GB. Use blueprint option to create a lambda function with sample code.

1. To start off, create a function using a blueprint, and define the function name and the runtime architecture.

First lambda function

4. Add a new IAM role and add it as the default execution role. You can also use an existing IAM role with trust relationship for lambda like below,

{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}

5. Click on create function to complete the lambda creation.

6. Add a simple method to print hello on your lambda and test the function.

import json

def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Sample lambda function

Voila! You just executed your first lambda function.

Applications of lambda:

  1. Lambda can be used to handle all your callback functions asynchronously.
  2. It can also be used along with other AWS services like authenticating the user for the SFTP server or process an event stored in SQS.

Drawbacks:

  1. The lambda becomes stale if they are not used for a while which is called a cold function. This results in a delay for upto 3 seconds to just initialize a function. So make sure you don’t use it in a user-facing functionality.
  2. Lambdas are meant for short functions that execute in a shorter duration, so the maximum timeout you can set is 15 mins.
  3. Inline editing is not supported for code larger than 5MB, this means you need to upload the edited code manually.

--

--