Skip to content

Latest commit

 

History

History
238 lines (180 loc) · 10.3 KB

README.md

File metadata and controls

238 lines (180 loc) · 10.3 KB

Serverless Framework Python Flask API with DynamoDB on AWS

Verified

This template deploys a Flask API on AWS Lambda that stores users in a DynamoDB table. It exposes POST /users to create a user and GET /users/{userId} to read one back. Use it as the starting point for a Python API that keeps data.

What it deploys

  • An API Gateway REST API that sends every path and method to one function.
  • A Lambda function, api, that runs the Flask application.
  • A DynamoDB table, users-table-<stage>, with provisioned capacity of one read and one write unit.
  • An IAM role that lets the function read and write that table, and a CloudWatch Logs log group for the function's logs.

Prerequisites

  • Python 3.15 on your PATH: the deploy runs it to package the dependencies in requirements.txt. It downloads Linux arm64 wheels, so the package matches the function on macOS, Windows and Linux alike, without Docker.
  • Node.js, to install the Serverless Framework CLI (npm i -g serverless) and the serverless-wsgi plugin. 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

Install the plugin and deploy from this directory:

npm install
serverless deploy

With npm 12 or later, npm install ends with a warning that it blocked the install script of es5-ext, a dependency of the plugin. That script only prints a message, so the example needs nothing from it.

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-flask-dynamodb-api" to stage "dev" (us-east-1)
Using Python specified in "runtime": python3.15
Packaging Python WSGI handler...
✔ Service deployed to stack aws-python-flask-dynamodb-api-dev (62s)
endpoints:
  ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
  ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+}

functions:
  api: aws-python-flask-dynamodb-api-dev-api (660 kB)

The deploy prints the endpoint URLs, which end in the stage name: https://<api-id>.execute-api.<region>.amazonaws.com/dev/ for the default dev stage.

Try it

Create a user with the URL the deploy printed:

curl --request POST 'https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users' \
  --header 'Content-Type: application/json' \
  --data-raw '{"name": "John", "userId": "someUserId"}'

The API stores the user and answers with it:

{"name":"John","userId":"someUserId"}

Read the user back by its userId:

curl https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/users/someUserId

The API answers with the stored user:

{"name":"John","userId":"someUserId"}

A userId that does not exist answers status 404 with {"error":"Could not find user with provided \"userId\""}.

How it works

serverless.yml defines the table as a CloudFormation resource, names it per stage with a parameter, passes the name to the function in an environment variable, and allows the function to use it:

stages:
  default:
    params:
      tableName: "users-table-${sls:stage}"

provider:
  iam:
    role:
      statements:
        - Effect: Allow
          Action:
            - dynamodb:Query
            - dynamodb:Scan
            - dynamodb:GetItem
            - dynamodb:PutItem
            - dynamodb:UpdateItem
            - dynamodb:DeleteItem
          Resource:
            - Fn::GetAtt: [UsersTable, Arn]
  environment:
    USERS_TABLE: ${param:tableName}

resources:
  Resources:
    UsersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${param:tableName}

Two http events, / and /{proxy+} with method ANY, send every request to the api function. serverless-wsgi adds the wsgi_handler.handler entry point, which turns each Lambda event into a WSGI request for the Flask application named in custom.wsgi.app. The Framework's built-in Python requirements packaging, turned on by custom.pythonRequirements, bundles the dependencies in requirements.txt.

The function runs on arm64, and one of Flask's dependencies, MarkupSafe, includes compiled code. pipCmdExtraArgs tells pip to download wheels built for Linux on arm64 and Python 3.15, whatever machine you deploy from, as Linux wheels without Docker describes:

custom:
  pythonRequirements:
    # Download Linux arm64 wheels for Python 3.15 on any machine, so compiled
    # dependencies (such as MarkupSafe) match the Lambda architecture and runtime.
    pipCmdExtraArgs:
      - '--platform=manylinux2014_aarch64'
      - '--platform=manylinux_2_28_aarch64'
      - '--python-version=3.15'
      - '--only-binary=:all:'

With --only-binary=:all:, a dependency you add that publishes no such wheel stops the deploy instead of being built for your machine. To build those inside a Docker container, set the dockerizePip option under custom.pythonRequirements.

app.py reads and writes the table with boto3:

@app.route('/users/<string:user_id>')
def get_user(user_id):
    result = dynamodb_client.get_item(
        TableName=USERS_TABLE, Key={'userId': {'S': user_id}}
    )
    item = result.get('Item')
    if not item:
        return jsonify({'error': 'Could not find user with provided "userId"'}), 404

    return jsonify(
        {'userId': item.get('userId').get('S'), 'name': item.get('name').get('S')}
    )

After the deploy, the API is public and anyone can call it. To restrict access, configure an authorizer, as the REST API event docs describe.

Run it locally

serverless-wsgi can serve the application on your machine, against a local DynamoDB. Install werkzeug, boto3 and the requirements first, in a virtual environment:

python3.15 -m venv ./venv
source ./venv/bin/activate
pip install werkzeug boto3
pip install -r requirements.txt

Emulate DynamoDB with the serverless-dynamodb plugin, the maintained fork of serverless-dynamodb-local. These commands add the plugin to devDependencies in package.json and to plugins in serverless.yml, and install DynamoDB locally:

serverless plugin install -n serverless-dynamodb
serverless dynamodb install

Add its configuration to the custom section of serverless.yml, so it creates the table on start:

custom:
  dynamodb:
    start:
      migrate: true
    stages:
      - dev

The virtual environment and the local DynamoDB files (.dynamodb/) stay out of the deployed package: the package section of serverless.yml excludes them, as Keep local environments out of the package describes.

app.py already connects to the local DynamoDB when serverless-wsgi sets the IS_OFFLINE environment variable:

if os.environ.get('IS_OFFLINE'):
    dynamodb_client = boto3.client(
        'dynamodb', region_name='localhost', endpoint_url='http://localhost:8000'
    )

Start DynamoDB, then serve the application:

serverless dynamodb start
serverless wsgi serve

The serverless-wsgi and serverless-dynamodb repositories describe the other local options.

Cost and cleanup

While the example is deployed, Lambda bills each request and the function's compute time, and API Gateway bills each request. The DynamoDB table uses provisioned capacity, one read and one write unit, which is billed by the hour while the table exists, whether or not you use it. The table also bills for the data it stores, and the users you create stay in it until you remove the stack, and so do the function's logs in CloudWatch Logs, which bills for stored log data. See Lambda pricing, API Gateway pricing, DynamoDB pricing and CloudWatch pricing. serverless remove deletes the table with its data, the function, the API and the log group:

serverless remove

To keep the table when you remove the stack, add DeletionPolicy: Retain to its resource definition.

Next steps