Using AWS SAM-CLI requires rebuild every time I update the code
Asked Answered
B

5

12

I am using SAM CLI to develop an API Gateway Lambda proxy integration. According to the docs, I should be able to use sam local start-api to test my endpoint locally. The start-api command allows for "hot reloading" as described in the AWS SAM documentation. However, I am not seeing this behavior.

My template.yaml file looks like:

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./hello_world
      DockerTag: python3.9-v1

When I run:

> sam build && sam local start-api

I can see that the endpoint is working:

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically.

However, when I make changes to the lambda_handler function inside of ./hello_world/app.py, the response to curl http://localhost:3000 remains the same. No hot reloading occurs.

The only solution I have found was to run sam build for each code change. This dramatically slows down development time -- because of some dependencies inside of requirements.txt, I have to wait 1-2 minutes for the build for each code change. I could just work on the file in the .aws-sam/build directory -- as suggested here -- but this seems like a bad solution because I would have to maintain two copies of the handler simultaneously.

Backlog answered 15/11, 2021 at 16:12 Comment(2)
Maybe you can have a look at this new beta release that allows for faster deployment cycles. It does talk about cloud deployments though and not local deployments. aws.amazon.com/blogs/compute/…Thrips
Check out the answer here https://mcmap.net/q/1008470/-how-to-force-aws-sam-cli-to-detect-changesAntoniaantonie
I
15

A great solution to this problem is to use the skip-pull-image flag so that Docker will reuse the Lambda runtime. Essentially run:

sam local start-api -t template.yaml --skip-pull-image

This got hot reloading working for me.

Infeasible answered 22/2, 2022 at 11:45 Comment(1)
The reason it works: AWS documentation: If you specify the --template option, AWS SAM CLI's default behavior is overridden, and will load just that AWS SAM template and the local resources it points to. docs.aws.amazon.com/serverless-application-model/latest/…Gemagemara
V
2

If a build exists, then sam local start-api uses built files to host the local version. Simply delete the ".aws-sam" folder and run yoursam local start-api again. If ".aws-sam" folder does not exist, all your changes will be applied automaticaly.

Voltmeter answered 22/7, 2023 at 17:11 Comment(0)
G
1

According to https://github.com/aws/aws-sam-cli/issues/920 & https://github.com/aws/aws-sam-cli/issues/901, you are expected to have two consoles/terminals open. One where you run sam local start-api and leave it alone. And another where you run sam build repeatedly (whenever you need to update).

If you find this > slightly annoying,

Getupandgo answered 27/11, 2021 at 0:21 Comment(0)
M
1

I used to find it quite annoying to be running two terminals and having to repeatedly build. Hence I ended up writing a boilerplate that bundles my source code and dependencies into a dist directory, runs sam local start-api in parallel, and watches for changes to update the dist directory.

Here's the repo: https://github.com/zishanneno/aws-typescript-lambda-boilerplate

Developing and debugging with this is much faster compared to building manually on every little code update.

Mcrae answered 3/3, 2022 at 18:58 Comment(0)
B
0

If you want to test with automatic reloading you can start your testing with:

$ uvicorn src.main:app --reload
Ballman answered 21/7 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.