How to run lambda application as local API

I’ve looked all over for some supported library that does this but can’t find anything. I just want to run my lambda as a local api (ie localhost:80000/api/get/1) so I can run both my frontend and backend all on my machine for rapid development.

I’ve hacked together a fastapi “gateway” that I run locally and use that to call the lambda_entry locally, the only problem is it’s quite slow, no doubt the spinning up of an environment for each request is taxing on performance.

I feel like this is something people would use a lot, am I on the right track?


You can use AWS SAM for this.

Local Testing and Debugging

Use SAM CLI to step-through and debug your code. It provides a Lambda-like execution environment locally and helps you catch issues upfront.

You might need to install Docker first as it would be the execution environment used to run the APIs. Setup the sam project first.

$ python3 -m pip install aws-sam-cli
$ sam init  # Just choose the first options e.g. <1 - AWS Quick Start Templates>

Now, you would have a file structure like this:

$ tree
└── sam-app
    ├── events
    │   └── event.json
    ├── hello_world
    │   ├──
    │   ├──
    │   └── requirements.txt
    ├── template.yaml
    └── tests
        ├── integration
        │   ├──
        │   └──
        ├── requirements.txt
        └── unit

6 directories, 13 files

2 files that you would be interested at are:


  • This is where you would put the codes of your Lambda function.
import json

def lambda_handler(event, context):
    """Sample pure Lambda function"""
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",


  • This is where you would configure the API e.g. HTTP Method, URL, the location of the code to run, etc.
    Type: AWS::Serverless::Function # More info about Function Resource:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
          Type: Api # More info about API Event Source:
            Path: /hello
            Method: get

Build the app and then you can run your local API already

$ cd sam-app/
$ sam build
$ sam local start-api

Access your local API

  • Note that running this the first time might take some time as this is when the environment for the API is setup e.g. creating the image and running the container. Any subsequent calls should be fast already.
$ curl
{"message": "hello world"}

Complete reference for your guidance: