What we're building
Our first version will contain just two AWS services:
- AWS Lambda: Executes the backend code.
- Amazon API Gateway: Provides the HTTP endpoint and routes requests to Lambda.
The request flow will look like this:
Client → API Gateway → Lambda → API Gateway → Client
We'll start with a GET /hello endpoint. Keeping the first version simple makes it easier to understand how the integration works before adding more functionality.
Step 1: Create the Lambda function
Open the AWS Management Console and navigate to AWS Lambda.
Create a function using Author from scratch. Give it a name such as hello-serverless-api , select a supported Python runtime, and configure an appropriate execution role.
For this example, the function needs permission to write logs. It doesn't need access to a database or other AWS resources.
Once the function is created, replace its default code with the following:
import json
def lambda_handler(event, context):
response_body = {
"message": "Hello from AWS Lambda!",
"status": "success"
}
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response_body)
}
Save and deploy the function.
Let's look at what the code does.
The lambda_handler function is the entry point Lambda invokes. It creates a Python dictionary containing the response message and status.
The function then returns an HTTP status code, a content-type header, and a JSON-formatted response body.
The json.dumps() call converts the Python dictionary into a string, which is the expected format for the body in this Lambda proxy response.
At this stage, we've defined the backend logic. Next, we need a way for clients to invoke it through HTTP.
Step 2: Create an HTTP API
Open Amazon API Gateway and choose the option to create an API.
Select HTTP API and configure a Lambda integration that points to the hello-serverless-api function.
Give the API a descriptive name, such as hello-serverless-http-api .
Next, configure a route:
Setting | Value |
|---|
Method | GET |
Path | /hello |
Integration | Our Lambda function |
Associate the route with the Lambda integration and create or deploy the API as required by the console.
Make sure API Gateway has permission to invoke the function. The console can configure the necessary Lambda resource-based permission when creating the integration.
The route tells API Gateway which integration to use when a request matches the specified HTTP method and path.
For example, a GET request to /hello should invoke our function.
A request using a different method or path might not match this route.
Step 3: Find the invoke URL
After configuring the API, locate its invoke URL in the API Gateway console.
It will resemble this example:
https://your-api-id.execute-api.your-region.amazonaws.com/hello
Replace the placeholder values with the URL and region associated with your API.
The /hello path is the route we configured earlier.
You can now send an HTTP request to the endpoint.
Step 4: Test the endpoint
A convenient way to test the endpoint is with curl :
curl -i "https://your-api-id.execute-api.your-region.amazonaws.com/hello"
Replace the example URL with your actual endpoint.
If the configuration is correct, the response should contain HTTP status 200 and a JSON body similar to this:
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
The expected result gives us a simple way to verify the integration.
If you don't receive the expected response, don't immediately recreate the API. Check the route, integration, deployment configuration, and Lambda execution logs first.
Also, remember that a successful request proves only that this basic path works. It doesn't establish that the API has production-ready security, error handling, or monitoring.
What could go wrong?
A few common issues are worth understanding.
The request returns 404. Check that the requested path and HTTP method match the route you created.
The request returns 500. Inspect the Lambda function's CloudWatch Logs for exceptions and verify the response structure.
The request returns 403. Review authorization settings and the relevant API and invocation permissions. The exact cause depends on the configuration.
The function isn't invoked. Check the API integration and confirm that API Gateway has permission to invoke the selected function.
The useful habit here is to investigate the component responsible for the failure instead of changing several settings at once.
What have we established?
This implementation introduces the basic connection between an HTTP endpoint and a Lambda function.
API Gateway handles the incoming HTTP request, while Lambda executes the backend logic. That separation lets us extend the application without putting all its responsibilities into one component.
Our current API is deliberately limited: it returns a fixed message and doesn't store or modify data.
That gives us a clear next step.
What's coming next?
In Week 3, I'll explore what happens when an API receives invalid input or encounters an error.
We'll look at input validation, response status codes, and ways to make the API's behavior more predictable.
The goal is to move beyond an endpoint that works for one simple request and understand how it should behave when things don't go as expected.
This article is part of Beyond the Console: Exploring AWS, One Build at a Time. Read Part 1 for the architecture and problem we're exploring.
Loading article