Introduction: Serverless and AWS Lambda

Welcome to the first lesson of the "Building Serverless Applications" course. In this lesson, we will explore the basics of serverless computing and how AWS Lambda helps you build applications without managing servers.

Serverless computing means you do not have to worry about setting up or maintaining servers. Instead, you write your code, and the cloud provider (like AWS) runs it for you only when needed. This makes it easier to scale and can save money, since you only pay for what you use.

AWS Lambda is a popular serverless service. With Lambda, you write small pieces of code called functions. These functions run in response to events, such as a user making a request to your application.

In this lesson, you will learn how to connect AWS Lambda to an API Gateway so you can build an API that responds to user requests. By the end, you will understand how a simple API can trigger a Lambda function, process some data, and return a result.

Recall: APIs and API Gateway

Before we dive in, let's quickly remind ourselves what an API (Application Programming Interface) is and how API Gateway fits in.

An API (Application Programming Interface) is a way for different programs to talk to each other. For example, when you use a weather app, it might call an API to get the latest weather data.

API Gateway is a service from AWS that helps you create and manage APIs. It acts as a "front door" for your application. When someone sends a request to your API, API Gateway receives it and can pass it on to a Lambda function to process.

In this lesson, we will see how API Gateway and Lambda work together to handle a simple calculation request.

Exploring the Lambda Function Code

Let's build up the Lambda function step by step. This function will receive a request with an amount, calculate tax, and return the total.

Step 1: Importing Required Libraries

First, we need to import the json library. This helps us work with JSON data, which is a common format for sending information in APIs.

Step 2: Defining the Lambda Handler

Every Lambda function needs a handler. The handler is the main function that AWS Lambda calls when the function is triggered.

  • event contains information about the request, such as the data sent by the user.
  • context contains information about the runtime and the function itself. For now, we won't use it, but it must be included.
Step 3: Reading the Input Data

We want to get the amount from the request. The data is sent as a JSON string in the body of the event.

  • event.get("body") gets the body of the request.
  • json.loads(...) converts the JSON string into a Python dictionary.
  • body.get("amount", 0) gets the amount value from the dictionary. If it's not there, it uses 0.
  • We use float(...) to make sure the amount is a number.
Step 4: Calculating Tax and Total

Now, let's calculate the tax and the total amount.

  • We multiply the amount by 0.07 to get a 7% tax.
  • round(..., 2) makes sure the result has two decimal places, like money.
Step 5: Returning the Response

Finally, we need to return the result as a JSON response.

  • statusCode: 200 means the request was successful.
  • headers tells the client that the response is in JSON format.
  • body contains the result as a JSON string.

Full Example Output

If you send a request with an amount of 100, the output will look like this:

Understanding the SAM Template

To connect API Gateway to our Lambda function, we use a template file called template.yaml. This file uses AWS SAM (Serverless Application Model) to define our resources.

Let's look at the key parts:

  • Type: AWS::Serverless::Function tells AWS this is a Lambda function.
  • Handler: main.handler means the function to run is called handler in the main.py file.
  • Runtime: python3.13 sets the Python version.
  • Events defines how the function is triggered. Here, it's triggered by an HTTP API at the path /calculate using the POST method.

This template tells AWS to create a Lambda function and set up API Gateway so that when someone sends a POST request to /calculate, our function runs. SAM automatically creates the necessary IAM execution role for the Lambda function with the basic permissions it needs to run and write logs to CloudWatch.

Building and Deploying with SAM

Now that we understand the Lambda function and SAM template, let's learn how to actually build and deploy our serverless application.

AWS SAM provides two key commands to get your application running:

Building the Application

First, we need to build our application:

This command:

  • Reads the template.yaml file
  • Packages your Lambda function code
  • Downloads any dependencies
  • Prepares everything for deployment

You'll see output showing SAM building your function and creating a .aws-sam folder with the packaged code.

Example Output:

Deploying the Application

Next, we deploy the application to AWS:

The --guided flag walks you through the deployment process step by step:

  • It asks for a stack name (like tax-calculator-app)
  • Confirms the AWS region to deploy to
  • Shows you what resources will be created
  • Asks for confirmation before deploying

Example Output:

Putting It All Together

Let's see how everything works as a whole:

  1. A user sends a POST request to /calculate with a JSON body like {"amount": 100}.
  2. API Gateway receives the request and passes it to the Lambda function.
  3. The Lambda function reads the amount, calculates the tax and total, and returns the result.
  4. API Gateway sends the response back to the user.

Real-World Example

Imagine you are building a simple online store. When a customer enters the amount of their purchase, your API calculates the tax and total for them instantly, without you having to manage any servers.

Summary And What's Next

In this lesson, you learned how to build a simple serverless API using AWS Lambda and API Gateway. We broke down the Lambda function code, explained how to process input and return a response, and saw how the SAM template connects everything together.

Next, you will get hands-on practice by working with Lambda functions and API Gateway in the CodeSignal environment. You will write your own code, test it, and see how serverless applications work in real time. Let's get started!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal