Published on

AWS Lambda: How to Create Your First Serverless Function

AWS Lambda is a serverless computing service that lets you run code without managing servers, allowing you to reduce infrastructure costs by up to 80% through its "pay-as-you-go" model. You simply upload your code, and the service handles all the scaling, patching, and administration automatically. This approach means your code only runs when triggered, and you never pay for idle time or unused capacity.

Why should you choose serverless computing?

Serverless computing (a cloud model where the provider manages the server infrastructure) removes the headache of maintaining physical or virtual machines. In a traditional setup, you have to worry about updating the operating system and monitoring CPU usage. With serverless, those tasks are handled by AWS so you can focus purely on your application logic.

This model is incredibly cost-effective for new projects. Since you are only billed for the milliseconds your code is actually executing, small tasks often fall within the AWS Free Tier. We've found that this lowers the barrier to entry for beginners who want to experiment without a large financial commitment.

Scaling is also handled automatically. If your function (the piece of code you write) suddenly receives thousands of requests, AWS Lambda creates more instances of that function to handle the load. You don't have to write any extra code to make this happen.

How does Lambda handle scaling and triggers?

Lambda operates on an event-driven model. This means your code stays dormant until a specific "trigger" (an event that tells the function to start) occurs. Common triggers include someone uploading a file to a storage bucket or a user clicking a button on a website.

When the trigger fires, Lambda "spins up" a container (a lightweight, isolated environment) to run your code. Once the task is finished, the container is eventually destroyed. This cycle ensures you aren't wasting resources on code that isn't doing anything.

Because each execution is independent, Lambda is "stateless." This means the function doesn't remember what happened the last time it ran. If you need to save data between runs, you'll usually connect your function to a database.

What do you need before starting?

To follow this tutorial, you will need a few basic tools and accounts. Don't worry if you haven't used these before; the setup is straightforward.

  • An AWS Account: You will need to sign up at aws.amazon.com. You'll need a credit card for verification, but the steps we're doing today will fit into the Free Tier.
  • A Web Browser: We will use the AWS Management Console (the web interface for managing AWS services).
  • Basic Python Knowledge: We will use Python 3.15, which is the current stable standard for serverless functions in 2026.

Step 1: Create your first function

Once you are logged into your AWS account, look for the search bar at the top of the screen. Type "Lambda" and select the first result to open the Lambda Dashboard.

  1. Click the button labeled Create function near the top right of the dashboard.
  2. Choose the Author from scratch option, which is usually selected by default.
  3. Under Basic information, give your function a name like my-first-lambda-function.
  4. In the Runtime dropdown, select Python 3.15.
  5. Leave all other settings (like Architecture and Permissions) at their default values.
  6. Click the Create function button at the bottom of the page.

What you should see: After a few seconds, a green banner will appear at the top of the screen saying "Successfully created the function." You will be taken to the function configuration page where you can see a file explorer and a code editor.

Step 2: Write and deploy your code

The code editor shows a default file named lambda_function.py. This file contains a basic "Hello World" template that we are going to modify slightly.

  1. In the code editor, delete the existing code and paste the following snippet:
import json

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 {
        'statusCode': 200,
        'body': json.dumps('Hello from SignalThirty! Your first Lambda is working.')
    }
  1. Click the Deploy button located just above the code editor.
  2. Wait for the status message to say "Changes deployed."

What you should see: The "Deploy" button will briefly turn gray and then become active again. This process packages your code and sends it to the AWS cloud servers.

Step 3: Test your function

Now that your code is live, you need to trigger it to make sure it works as expected. We will use a manual test event to simulate a real-world trigger.

  1. Click the Test tab located in the middle of the page (next to Code and Monitor).
  2. In the Test event section, give your event a name like MyTestEvent.
  3. Keep the "hello-world" template and click the Save button.
  4. Now, click the Test button (it's often located near the top right of the code area).

What you should see: An "Execution result" section will appear with a green background. If you click "Details," you will see the message: "Hello from SignalThirty! Your first Lambda is working." You will also see the "Log output" which shows the print statement we added to the code.

How to avoid common beginner mistakes?

It is normal to feel overwhelmed by the number of settings in AWS. One common mistake is forgetting to click "Deploy" after changing your code; if you don't click it, your "Test" will run the old version of the code.

Another common issue is "Timeout" errors. By default, Lambda functions are set to stop running after 3 seconds. If your code is performing a complex task, like processing a large image, you might need to increase this limit in the Configuration tab.

Lastly, pay attention to permissions. If your Lambda needs to talk to another service, like a database, it needs an "IAM Role" (Identity and Access Management - a set of rules defining what a service can do). For this basic tutorial, the default role created for you is enough to run the code and record logs.

Next steps

Now that you've successfully run your first function, you can start exploring more advanced integrations. You might try connecting your Lambda to an API Gateway (a service that creates a URL for your function) so you can trigger it from a browser.

You could also explore using Claude Sonnet 4 or GPT-5 to help you write more complex Python logic for your functions. These models are excellent at generating AWS-specific code patterns that follow current security practices.

For more detailed guides and technical references, visit the official AWS documentation.


Read the Lambda Documentation