This template deploys an HTTP API on AWS Lambda and API Gateway with Python. One GET / endpoint returns a JSON message. Start here when you want an HTTP endpoint and no database.
- An API Gateway HTTP API with one route,
GET /. - A Lambda function,
hello, that answers the route. - An IAM role for the function and a CloudWatch Logs log group for its logs.
- Python 3.15, to run the function locally and to package dependencies when you add them.
- Node.js, to install the Serverless Framework CLI:
npm i -g serverless. See Getting started. - An AWS account, with credentials configured on your machine. See AWS credentials.
- A Serverless account, or a license key in
SERVERLESS_LICENSE_KEY. The CLI asks you to sign in the first time you run it.
Run the deploy from this directory:
serverless deployWithout options, the deploy uses the stage dev and the Region us-east-1. To change them, pass --stage or --region, and pass the same options to every later command, including serverless remove.
You should see output like this:
Deploying "aws-python-http-api" to stage "dev" (us-east-1)
✔ Service deployed to stack aws-python-http-api-dev (46s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: aws-python-http-api-dev-hello (3.3 kB)
The deploy prints the endpoint URL, https://<api-id>.execute-api.<region>.amazonaws.com/.
Call the endpoint with the URL the deploy printed:
curl https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/The function answers with status 200 and this body:
{"message": "Go Serverless v4.0! Your function executed successfully!"}serverless.yml declares one function and connects it to the HTTP API with an httpApi event:
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /
method: gethandler.py returns the status code and a JSON body:
def hello(event, context):
body = {
"message": "Go Serverless v4.0! Your function executed successfully!",
}
response = {"statusCode": 200, "body": json.dumps(body)}
return responseAfter the deploy, the API is public and anyone can call it. To restrict access, configure an authorizer, as the HTTP API event docs describe. To serve the API on your own domain, uncomment domain in serverless.yml and read the custom domains guide.
Invoke the function on your machine:
serverless invoke local --function helloThe command prints the function's response:
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v4.0! Your function executed successfully!\"}"
}To emulate API Gateway and Lambda locally, install the serverless-offline plugin and start it. The install adds the plugin to devDependencies in package.json and to plugins in serverless.yml:
serverless plugin install -n serverless-offline
serverless offlineThe plugin's GitHub repository describes its options.
The Framework packages Python dependencies itself, with no plugin. List them in a requirements.txt file and add a custom.pythonRequirements block to serverless.yml to turn the packaging on:
custom:
pythonRequirements: {}The deploy then installs the requirements into the Lambda package. Pipfile and pyproject.toml work too, with the configuration the Python requirements guide describes.
While the example is deployed, Lambda bills each request and the function's compute time, and API Gateway bills each request. While nobody calls the API, neither bills anything. Until you remove the stack, the function's logs stay in CloudWatch Logs, which bills for the log data it stores. See Lambda pricing, API Gateway pricing and CloudWatch pricing. Remove the stack, with the function, the API and the log group, when you are done:
serverless remove- Read the HTTP API event docs for routes, authorizers and CORS.
- Route many paths through one function with Flask.
- Store data in DynamoDB with the Flask and DynamoDB example.