Skip to content

Latest commit

 

History

History
148 lines (103 loc) · 5.62 KB

README.md

File metadata and controls

148 lines (103 loc) · 5.62 KB

Serverless Framework Python HTTP API on AWS

Verified

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.

What it deploys

  • 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.

Prerequisites

  • 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.

Deploy

Run the deploy from this directory:

serverless deploy

Without 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/.

Try it

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!"}

How it works

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: get

handler.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 response

After 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.

Run it locally

Invoke the function on your machine:

serverless invoke local --function hello

The 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 offline

The plugin's GitHub repository describes its options.

Add dependencies

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.

Cost and cleanup

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

Next steps