Skip to main content

Command Palette

Search for a command to run...

AWS Lambda with Python: A Step-by-Step Guide to Build and Deploy Your First Serverless Function

Updated
7 min readView as Markdown
AWS Lambda with Python: A Step-by-Step Guide to Build and Deploy Your First Serverless Function
A
Software Engineer, Dev-ops, AWS

When you first start building applications, you usually think about buying or renting a server—a computer that sits somewhere in a data center, running 24/7, waiting for someone to use their app.

But what if your app only gets used a few times a day? You are still paying for that server every single second it sits idle.

This is where AWS Lambda comes in.

AWS Lambda is a serverless service. "Serverless" doesn't mean there are no servers; it just means you don't have to manage them. You don't have to set up an operating system, install updates, or worry about hardware. You simply upload your code, and AWS runs it only when it's needed.

In this guide, we will look at the core concepts of Lambda by building, deploying, and running a simple Python function right inside your browser.


The Core Concept: Pay-As-You-Go Code

Think of traditional servers like renting an apartment—you pay rent every month whether you are sleeping there or away on vacation.

AWS Lambda is like a hotel room—you pay only for the exact time you are using it.

Why do software engineers love it?

  • Zero Maintenance: You focus 100% on writing Python code. AWS handles the cloud infrastructure.

  • Massive Cost Savings: If your code isn't running, your bill is exactly $0. AWS charges you only for the milliseconds your code takes to execute.

  • Automatic Growth: If 1 person uses your code, it runs once. If 1,000 people use it at the exact same time, AWS automatically creates 1,000 copies to handle the demand.


Prerequisites

To follow along, you only need two things:

  1. An active AWS Account (The AWS Free Tier includes 1 million free Lambda requests per month!).

  2. Basic knowledge of Python (knowing how to write a simple function).


Step-by-Step: Building Your First Function

Step 1: Find Lambda in the AWS Console

  1. Log into your AWS Management Console.

  2. Type Lambda into the search bar at the top of the screen.

  3. Click on Lambda from the search results to open the dashboard.

  4. Click the orange Create function button.


Step 2: Configure Your Function

AWS will give you a few options. Keep it simple and select Author from scratch.

Fill out the basic settings like this:

Field What to Type / Select Why we choose it
Function Name my-first-lambda This is just the name of your project.
Runtime Python 3.14 (or the latest version) Tell AWS which language you are using. You can select Node.js or other available languages as well.

Scroll to the bottom and click the orange Create function button.


Step 3: Look at the Python Code

Once your function is created, AWS will show you a built-in code editor. Inside, you will see a file containing a basic Python function that looks like this:

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': 'Hello from AWS Lambda!'
    }

Let's break this down into simple terms:

  • def lambda_handler(...): This is the main entry point. Think of it like the main() function or the first line of code AWS looks for when it runs your file.

  • event and context: These are placeholders (variables) that AWS automatically passes into your function. For now, you don't need to use them. Just know they represent information being sent to your function when it runs.

⚠️ Troubleshooting Note: Seeing a "Unable to retrieve source code" error? If the built-in AWS code editor displays an error saying it cannot load or retrieve your source code, don't panic! This is usually caused by an aggressive browser extension (like an ad-blocker or privacy shield) blocking the AWS network requests. How to fix it quickly:

  • The Easy Way: Try turning off your browser's ad-blocker for the AWS Console, or open the AWS Console in an Incognito / Private window.

  • The Professional Way: If you prefer working locally, you can skip the browser editor entirely. You can write your Python code on your computer using VS Code, package it into a standard .zip file, and upload it to Lambda using the Upload from dropdown button on this exact same page.


Step 4: Change and Deploy Your Code

Let's make a slight change to the code so you can see how editing works. Update the text to include a simple Python print statement:

def lambda_handler(event, context):
    print("Testing my first cloud function!")
    
    return {
        'statusCode': 200,
        'body': 'Success! My Python code is running in the cloud.'
    }

💡 Important Step: Just like saving a file on your computer, you must tell AWS to save your changes to the cloud. Click the Deploy button right above the code editor. Your code is now live!


Step 5: Test Your Function

Since we don't have a website or mobile app connected to this yet, AWS provides a way to simulate running it.

  1. Click the Test button next to Deploy.

  2. A window will pop up asking you to create a "Test event". Don't worry about the complex text on the screen; just type MyFirstTest in the Event name box.

  3. Scroll down and click Save.

  4. Now, click that Test button one more time.

Look at the Execution result box that appears! You will see the successful response:

{
  "statusCode": 200,
  "body": "Success! My Python code is running in the cloud."
}

Where do my print statements go? (CloudWatch Logs)

In a normal Python script on your computer, print() outputs text directly to your terminal screen. But where does it go when it runs on a server thousands of miles away?

It goes to Amazon CloudWatch.

CloudWatch is AWS's digital notebook. Every single time your Lambda function runs, it automatically writes down what happened.

If you click on the Monitor tab right below your function name, and then click View CloudWatch Logs, you will see a history of when your function ran, how long it took, and your custom message: Testing my first cloud function!. This is how you will debug your code when things go wrong in the future.


Next Steps: Moving Beyond the Basics

Now that you understand how to write, deploy, and test code on AWS Lambda, you are familiar with the foundation of serverless computing!

As you get more comfortable, you can start exploring how to connect your Lambda function to the rest of the world. For example:

  • Triggers: You can tell Lambda to run automatically whenever someone uploads a picture to an online storage bucket (S3 Bucket), or on a timer (like a daily alarm clock).

  • API Gateway: You can connect Lambda to a web address so that anytime a user visits [yourwebsite.com/api](https://yourwebsite.com/api), your Python code fires up and sends back data.

Conclusion

You don't need to be a cloud architect to start using the cloud. In just a few minutes, you managed to:

  1. Create a managed Python environment.

  2. Write and save live cloud code.

  3. Execute it and view the output.

The best way to learn is by experimenting. Try changing the text inside the body return statement, hitting Deploy, and testing it again!