Write your first AWS Lambda Function

Write your first AWS Lambda Function

Welcome to Serverless!

Need to run code, build applications, provide services without worrying about infrastructure, management and scaling? Meets all the serverless criteria!

In this thread, let's talk about AWS Lambda and what it brings to the table with its capabilities and write our very first Python serverless function on AWS Lambda. But before that, let's get some ground-level understanding of Serverless.

What is Serverless?

image.png Confusing as it might sound, Serverless is not 'code running without a server' but more like 'code not running on your server'.

You would need a server to host your code on the internet. Imagine you host a social media application on your local server. As your application becomes more and more popular, you have more users, which means more interactions and more traffic. Naturally, you'd need a beefier machine to handle all operations without causing trouble to the users. Having more users also means that you'd need more storage to store all the data, viz. user information, media, etc.

But what if someone else could automatically handle the issues of resources, speed, etc. without us having to keep manually managing the infrastructure? That's exactly what Serverless brings to the table.

Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on-demand, taking care of the servers on behalf of their customers.

What are Serverless functions?

image.png Serverless functions are programmatic functions we write in our favourite programming languages, except the execution, scaling and management is taken care of by Serverless infrastructure.

Serverless functions are event-driven and scaled on-demand, hence they make for a lot of use-cases like hosting Web applications, IoT, Streams and Batch Processing, Cloud Automation, etc.

What is AWS Lambda?

image.png AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). We can create functions or complete applications written in one of the supported languages and upload them to AWS Lambda. Once deployed, AWS can take care of managing your code on the server-side.

The AWS Free Tier offers 1 Million Free Requests per month for AWS Lambda. Let's hop into the AWS Console and try it for ourselves!


  • An AWS Account (preferably Free Tier so that you are not charged for using cloud infrastructure)

Write your First Lambda Function

image.png Open up the AWS Console and search for Lambda.

image.png Click on Create Function.

image.png With other default settings, choose a function name and runtime environment, I'll go with Python 3.9 for this thread.

image.png The function has successfully been created and now you are at the Dashboard for this function. The Function overview gives an idea of what Lambda is connected to. Lambda Functions can work independently or can be connected with other AWS Services.


You can use the provided code editor to make changes to your function. You can also create new files in the file system and even upload files.

Understanding Events

Serverless functions, as I previously mentioned are event-driven. You can see that the function is associated with an event. The parameter event here is a Python Dictionary, which is a collection of key-value pairs, that provides information about a certain event.

Let's just print this event.

image.png When you click on Test, it asks you to create a test event. Just give it a name and leave everything as-is. Then, click on Deploy to apply these changes and then click on Test.

image.png You can see that we get the same key-value pairs present in the test config under Execution Results.

Using the Event to modify the Response

image.png Let's use the data in the event parameter to modify our response. Click on the dropdown near Test and change the test configuration. Update the key-value pairs, to say use your name.

Similarly, we can update our function to print the name in the response. If there is no name in our event, we'll simply return 'No Name'.

import json

def lambda_handler(event, context):
    # TODO implement
    res = {
        'name': event['name'] if 'name' in event else 'No Name'
    return {
        'statusCode': 200,
        'body': json.dumps(res)

image.png You can deploy and test your function to see the results.

image.png Once you're done exploring Lambda, you can delete your Lambda function so that you're not billed for it in the future. Even though you won't be charged until you exceed the request limits for the function, it's recommended to delete or terminate services you are not using.

Congratulations, you just created your first AWS Lambda function! I hope you enjoyed working in the serverless world. This was just an introduction to the capabilities of Lambda, the possibilities with it in real life are endless.

Thank you for reading :)

Did you find this article valuable?

Support Sreekesh Iyer by becoming a sponsor. Any amount is appreciated!