Published on

How to Deploy a Python App to AWS Lambda in 15 Minutes

Deploying a Python application to AWS Lambda takes less than 15 minutes and allows you to run code without managing servers. By using the AWS Management Console or the AWS CLI (Command Line Interface), you can scale your application from zero to thousands of simultaneous users for a cost that is often pennies per month.

How does AWS Lambda work for Python?

AWS Lambda is a "serverless" computing service, which means you provide the code and AWS handles the hardware, operating system, and scaling. When an event occurs—like a web request or a file upload—Lambda creates a temporary container to run your Python script.

The core of every Lambda function is the "handler" function. This is a specific part of your code that AWS calls whenever the function starts. We've found that keeping this handler simple makes it much easier to debug your logic as your project grows.

Lambda supports modern versions of Python, including Python 3.12. It also integrates with other services, allowing your code to talk to databases or send emails automatically.

What do you need before starting?

Before you begin, you will need a few basic tools and accounts. Most of these are free for beginners to use.

  • An AWS Account: You can sign up for the AWS Free Tier, which includes one million free Lambda requests per month.
  • Python 3.12+ Installed: Ensure you have Python running on your local computer to test your script.
  • A Code Editor: Tools like VS Code or PyCharm are great for writing your Python logic.

How do you write a Lambda-ready Python script?

A standard Python script usually runs from top to bottom, but a Lambda script waits for an "event" (a dictionary containing data about the trigger). You must structure your code so that AWS knows exactly which function to execute.

Create a file named lambda_function.py and add the following code:

import json

# The handler function is the entry point AWS uses
def lambda_handler(event, context):
    # 'event' contains data sent to the function
    # 'context' provides info about the runtime environment
    
    print("Received event: " + json.dumps(event))
    
    # Return a response that Lambda understands
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from SignalThirty and AWS Lambda!')
    }

In this example, the statusCode tells the requester that the code ran successfully. The body is the actual message or data you want to send back.

How do you create the function in the AWS Console?

Once your code is ready, you need to set up a place for it to live in the cloud. We'll use the AWS Management Console, which is the web-based dashboard for managing your services.

Step 1: Open the Lambda Dashboard Log into your AWS account and search for "Lambda" in the top search bar. Click the orange "Create function" button to start the setup wizard.

Select "Author from scratch" since you are building a custom app. This gives you full control over the settings from the beginning.

Step 2: Configure Basic Settings Give your function a name, such as my-first-python-app. Under the "Runtime" dropdown, select "Python 3.12" to ensure you are using a modern, secure version of the language.

You can leave the "Architecture" as x86_64 for now. Click "Create function" at the bottom of the page to finalize the container.

Step 3: Upload Your Code Scroll down to the "Code source" section of your new function. You will see an online editor where you can paste the code we wrote earlier.

Delete the default code in the editor and replace it with your lambda_handler script. Click the "Deploy" button to save your changes to the AWS servers.

Step 4: Test the Function Click the "Test" button to create a simulated event. You can give this test a name like MyTestEvent and leave the default JSON (JavaScript Object Notation - a format for sharing data) as it is.

Click "Save" and then click "Test" again to run your code. You should see a green box indicating the execution succeeded, along with the "Hello" message in the output.

How do you handle external libraries?

If your Python app needs libraries like requests or pandas, you cannot just paste the code into the browser. AWS Lambda does not automatically install packages from a requirements.txt file like some other platforms do.

To include these, you must create a "Deployment Package" (a .zip file containing your code and all its dependencies). You install the libraries into a local folder, zip them up with your script, and upload that zip file to the Lambda console.

Alternatively, you can use "Lambda Layers." These are separate files that contain your libraries, allowing you to keep your main code file small and easy to edit in the browser.

What are common Lambda mistakes to avoid?

New developers often run into "Timeout" errors. By default, AWS Lambda stops your code if it takes longer than 3 seconds to run, but you can increase this limit in the "Configuration" tab.

Another common issue is "Permissions (IAM) errors." IAM (Identity and Access Management) is the system AWS uses to decide what your code is allowed to touch. If your code tries to save a file to an S3 bucket (a storage folder in the cloud) but doesn't have the right permission, it will fail.

Finally, remember that Lambda is "stateless." This means any files you save or variables you create disappear as soon as the function finishes running. If you need to save data permanently, you should send it to a database like DynamoDB.

Next Steps

Now that you have deployed a basic function, you can try connecting it to a URL so people can visit your app in a browser. This is usually done by adding an "API Gateway" (a service that turns Lambda functions into web addresses) as a trigger.

You might also explore using Claude Sonnet 4 or GPT-5 to help you write more complex Python logic for your handlers. These models are excellent at formatting data specifically for AWS services.

For detailed guides, visit the official AWS Lambda documentation.


Read the Deploy Documentation