AWS Builder Center

Building Your First Serverless API with AWS Lambda and Amazon API Gateway

Learn how to build a simple serverless API using AWS Lambda and Amazon API Gateway. This hands-on guide explains how to create a Lambda function, configure an HTTP endpoint, test API responses, and understand the benefits of serverless architecture.

Introduction

Building and maintaining a traditional server can require time, infrastructure management, and ongoing maintenance. AWS offers serverless services that allow developers to build applications without managing servers directly.
In this article, we will build a simple serverless API using AWS Lambda and Amazon API Gateway. By the end, you will understand how these services work together to process HTTP requests and return responses.

What Is Serverless Computing?

Serverless computing allows developers to run application code without provisioning or managing the underlying servers.
AWS Lambda runs your code in response to events, while Amazon API Gateway provides an endpoint through which applications can communicate with your backend.
Together, these services make it possible to create APIs without managing a traditional web server.

Architecture Overview

Our application will follow this simple flow:
  1. A client sends an HTTP request.
  2. Amazon API Gateway receives the request.
  3. API Gateway invokes an AWS Lambda function.
  4. Lambda executes the application logic.
  5. The function returns a response through API Gateway to the client.
This architecture is useful for lightweight APIs, automation tools, and event-driven applications.

Step 1: Create an AWS Lambda Function

  1. Sign in to the AWS Management Console.
  2. Open AWS Lambda.
  3. Choose Create function.
  4. Select Author from scratch.
  5. Enter MyFirstServerlessAPI as the function name.
  6. Choose a supported Python runtime.
  7. Use the default execution role option to create a basic role with Lambda execution permissions.
  8. Create the function.
AWS Lambda will provide an editor where you can write and test your code.

Step 2: Write the Lambda Code

Replace the sample code with the following Python function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import json

def lambda_handler(event, context):
response =
{
"message": "Hello from AWS Lambda!",
"status": "success"
}

return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response)
}
Choose Deploy to save the updated code.
This function returns a JSON response containing a message and a status field.

Step 3: Create an API Gateway HTTP API

  1. Open Amazon API Gateway in the AWS Management Console.
  2. Choose Create API.
  3. Select the HTTP API option.
  4. Add an integration with your Lambda function.
  5. Configure a route such as GET /hello.
  6. Create a stage, using $default if appropriate.
  7. Review the configuration and create the API.
API Gateway will generate an invoke URL for your API.

Step 4: Test the API

Copy the invoke URL and append /hello if the route is not already included in the URL.
For example, use a request such as:
1
curl https://YOUR_API_ID.execute-api.YOUR_REGION.amazonaws.com/hello
Replace YOUR_API_ID and YOUR_REGION with the values shown in your API Gateway configuration.
A successful request should return a JSON response similar to this:
1
2
3
4
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
If you receive an error, check the API route, Lambda integration, execution permissions, and CloudWatch logs.

Step 5: Understand the Benefits

This serverless architecture offers several advantages:
  • No server management: AWS manages the underlying compute infrastructure.
  • Automatic scaling: Lambda can handle changing request volumes.
  • Pay-per-use pricing: Charges depend on service usage and configuration.
  • Easy integration: These services integrate with other AWS services.
  • Faster development: Developers can focus on application logic rather than server maintenance.
Actual costs depend on request volume, execution duration, configuration, and other service usage.

Security Best Practices

Security should be considered from the beginning of an application.
  • Follow the principle of least privilege for Lambda execution roles.
  • Do not store passwords or API keys directly in source code.
  • Configure authentication and authorization when the API requires protected access.
  • Monitor requests, errors, and execution logs.
  • Set suitable throttling and usage controls where appropriate.
  • Review AWS costs and configure billing alerts.
A publicly accessible API should never be assumed to be protected simply because its backend runs on AWS Lambda.

Conclusion

AWS Lambda and Amazon API Gateway provide a practical way to build serverless APIs without managing traditional servers.
By creating a Lambda function, connecting it to an HTTP API, and testing the endpoint, you can build a foundation for more advanced cloud applications.
You can extend this project by connecting the API to Amazon DynamoDB, adding authentication, or implementing a CRUD application.
For beginners learning AWS, this is an excellent hands-on project that introduces serverless computing and API development.

References

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article