Writing, packaging, and deploying AWS Lambda function written in Python

Mayank Patel

  1. Jan 02, 2025
  2. 4 min read

What is AWS Lambda?

AWS Lambda is a serverless computing service provided by AWS. It is a service that runs your code in response to an event and automatically manages the resources required for running your code. You don't need to worry about any underlying resources which are required.

One example of such an event is a file uploaded to the S3 bucket. You can trigger your lambda function when the file is uploaded to your S3 bucket. You can think of a data processing job where files are being uploaded to your bucket and you want to start processing as soon as files arrive in the bucket. This will help you achieve real-time data processing use cases.

Now that you know what the AWS lambda function is, let's create our first Lambda function.

Create the file named app.py and include the following code in it.


# This is a handler
def handler_name(event, context):
    # Your logic
    return True

You must be wondering what is handler_name function. Basically, the handler function is a method in your code that processes events. When you invoke your lambda function, lambda runs your handler method. For example, if you have configured an event for triggering the lambda function on file upload in an S3 bucket, upon successful upload of the file in a bucket, Lambda will run your method named handler_name .

Now that we know what the handler is, we need to know how we define the handler's name. It depends on 2 things.

  • The filename in which your handler method is written
  • Your handler method name

For example, the filename is app.py and the method name is lambda_handler , your lambda handler name will be app.lambda_handler .Basically filename.handler_method_name

When your Lambda function is invoked, lambda passes 2 arguments to your handler function. event and context (Lambda context)

  • event: The first argument is event which is the JSON document that contains data for your Lambda function to process
  • context: The context object provides methods and properties that provide information about the invocation, the function, and the runtime environment. For example, it contains a unique ID for each lambda invocation.

Let's assume event data is {first_name: "Mayank", last_name: "Patel"} . Now update your lambda_handler with the following code.


def lambda_handler(event, context):
    message = 'Hello {} {}, this is from Lambda Function!'.format(event['first_name'], event['last_name'])  
    return { 
        'message' : message
    }

Now, we need to build the deployment package.

Prepare a deployment package with no dependencies

Open your terminal and run the following commands

  • mkdir my_function
  • cd my_function
  • Copy your app.py file in this folder
  • zip lambda_deployment_package.zip app.py This will generate your deployment package named lambda_deployment_package.zip in your project root directory

Prepare a deployment package with dependencies

Let's update the handler method with the following content which has a dependency on python requests library


import requests
def lambda_handler(event, context):   
    response = requests.get("https://www.example.com/")
    print(response.text)
    return response.text

Now, open your terminal and run the following commands.

  • mkdir my_function
  • cd my_function
  • Install requests library in the venv folder using the following command
    • pip install --target ./venv requests
  • Now create the deployment package with installed library in venv
    • cd venv
    • zip -r ../lambda_deployment_package.zip .
    • The above commands will generate your deployment package named lambda_deployment_package.zip in your project root directory
  • Now add the app.py file to the root of your zip file using below command
    • cd ..
    • zip lambda_deployment_package.zip app.py

After following the above steps, now you have your lambda deployment package ready locally. Now, it's time to deploy this package.

Follow the below steps to deploy your local lambda package.

There are 2 ways you can deploy your lambda function. Using AWS CLI or from the console.

Deploy using AWS CLI

Here, I assume you have configured AWS CLI.

Please create the execution policy. It gives your lambda function access to AWS resources.


aws iam create-role --role-name lambda-ex --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}

Now, you need to add permission to your role


aws iam attach-role-policy --role-name lambda-ex --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Now, run the below command to deploy your package.


aws lambda create-function --function-name my-function --zip-file fileb://lambda_deployment_package.zip --handler app.lambda_handler --runtime python3.9 --role arn:aws:iam::123456789012:role/lambda-ex

Here, replace 123456789012 with your AWS Account ID. You can get it by logging into the AWS console and clicking on your name in the top-right corner.

Deploy using AWS Console

Please follow the below steps to deploy your lambda function

  • Login to your AWS console and search/open the AWS Lambda service
  • Click Create function
  • Give input to Function name and select appropriate runtime. We will choose python3.9 and hit Create function
  • Now, select your newly created function if not selected
  • Under the code tab, click Upload from option. There are 2 ways to upload. Either you can upload the lambda package zip file from the local disk or you can upload your zip file to S3 and give S3 location. We will upload it from the local disk. Upload and hit Save
  • Now, under the Runtime settings section, hit Edit and give your Handler name. In our case, it is app.lambda_handler and hit Save

Now, you can test your lambda function. However, there are some key settings that you may want to set for your function.

  • Memory & Timeout: Under the Configuration tab -> General configuration, hit Edit update Memory and Timeout
  • Concurrency: You can set concurrency for your lambda function under Concurrency sub-tab

Test the Lambda function

To test your newly deployed serverless lambda function, head to the Test sub-tab.

You can either Save your Test event by giving the name and providing input to Event JSON if applicable and hit Save.

If you don't want to Save and just want to Test, hit Test after giving input to Event JSON.

So far, I have covered basic lambda function package preparation, deployment, and setting key configuration.

Example Use Cases

  • Realtime data processing: For example, if you want to process the data that you are receiving in the form of files, you can choose to upload files in the S3 bucket and trigger your lambda function as soon as the file arrives in the S3 bucket
  • If you are developing the website and you want to host the back-end service on AWS Lambda function
  • If you want to perform some sort of data analytics on top of the data you have in your warehouse
  • Log analysis
  • Document conversion

Benefits of Lambda

  • No servers to manage
  • Continuous scaling
  • Increases innovation
  • Pay-for-use billing model
  • High availability

So far I have covered the basics of AWS lambda which include writing Lambda handler method, preparing lambda package with and without dependencies, and deploying lambda function using AWS CLI and from AWS Console.

About Author
Mayank Patel

See What Our Clients Say

Mindgap

Incentius has been a fantastic partner for us. Their strong expertise in technology helped deliver some complex solutions for our customers within challenging timelines. Specific call out to Sujeet and his team who developed custom sales analytics dashboards in SFDC for a SoCal based healthcare diagnostics client of ours. Their professionalism, expertise, and flexibility to adjust to client needs were greatly appreciated. MindGap is excited to continue to work with Incentius and add value to our customers.

Samik Banerjee

Founder & CEO

World at Work

Having worked so closely for half a year on our website project, I wanted to thank Incentius for all your fantastic work and efforts that helped us deliver a truly valuable experience to our WorldatWork members. I am in awe of the skills, passion, patience, and above all, the ownership that you brought to this project every day! I do not say this lightly, but we would not have been able to deliver a flawless product, but for you. I am sure you'll help many organizations and projects as your skills and professionalism are truly amazing.

Shantanu Bayaskar

Senior Project Manager

Gogla

It was a pleasure working with Incentius to build a data collection platform for the off-grid solar sector in India. It is rare to find a team with a combination of good understanding of business as well as great technological know-how. Incentius team has this perfect combination, especially their technical expertise is much appreciated. We had a fantastic time working with their expert team, especially with Amit.

Viraj gada

Gogla

Humblx

Choosing Incentius to work with is one of the decisions we are extremely happy with. It's been a pleasure working with their team. They have been tremendously helpful and efficient through the intense development cycle that we went through recently. The team at Incentius is truly agile and open to a discussion in regards to making tweaks and adding features that may add value to the overall solution. We found them willing to go the extra mile for us and it felt like working with someone who rooted for us to win.

Samir Dayal Singh

CEO Humblx

Transportation & Logistics Consulting Organization

Incentius is very flexible and accommodating to our specific needs as an organization. In a world where approaches and strategies are constantly changing, it is invaluable to have an outsourcer who is able to adjust quickly to shifts in the business environment.

Transportation & Logistics Consulting Organization

Consultant

Mudraksh & McShaw

Incentius was instrumental in bringing the visualization aspect into our investment and trading business. They helped us organize our trading algorithms processing framework, review our backtests and analyze results in an efficient, visual manner.

Priyank Dutt Dwivedi

Mudraksh & McShaw Advisory

Leading Healthcare Consulting Organization

The Incentius resource was highly motivated and developed a complex forecasting model with minimal supervision. He was thorough with quality checks and kept on top of multiple changes.

Leading Healthcare Consulting Organization

Sr. Principal

US Fortune 100 Telecommunications Company

The Incentius resource was highly motivated and developed a complex forecasting model with minimal supervision. He was thorough with quality checks and kept on top of multiple changes.

Incentive Compensation

Sr. Director

Most Read
Scaling Data Analytics with ClickHouse

In the modern data-driven world, businesses are generating vast amounts of data every second, ranging from web traffic, IoT device telemetry, to transaction logs. Handling this data efficiently and extracting meaningful insights from it is crucial. Traditional databases, often designed for transactional workloads, struggle to manage this sheer volume and complexity of analytical queries.

Kartik Puri

  1. Nov 07, 2024
  2. 4 min read
From Pandas to ClickHouse: The Evolution of Our Data Analytics Journey

At Incentius, data has always been at the heart of what we do. We’ve built our business around providing insightful, data-driven solutions to our clients. Over the years, as we scaled our operations, our reliance on tools like Pandas helped us manage and analyze data effectively—until it didn’t.

The turning point came when our data grew faster than our infrastructure could handle. What was once a seamless process started showing cracks. It became clear that the tool we had relied on so heavily for data manipulation—Pandas—was struggling to keep pace. And that’s when the idea of shifting to ClickHouse began to take root.

But this wasn’t just about switching from one tool to another; it was the story of a fundamental transformation in how we approached data analytics at scale.

Chetan Patel

  1. Oct 28, 2024
  2. 4 min read
Designing Beyond Aesthetics: How UI Shapes the User Experience in Enterprise Solutions

UI design in enterprise solutions goes beyond aesthetics, focusing on enhancing usability and user satisfaction. By emphasizing clarity, visual hierarchy, feedback, and consistency, UI improves efficiency and productivity, allowing users to navigate complex tasks seamlessly.

Mandeep Kaur

  1. Oct 23, 2024
  2. 4 min read
How We Transformed the B2B Marketplace: From Struggle to Success

We recently undertook a comprehensive transformation of the B2B marketplace to address some pressing challenges

Mayank Patel

  1. Jul 29, 2024
  2. 4 min read