How to force AWS SAM CLI to detect changes?
Asked Answered
B

2

1

Amazon's SAM CLI does not pick up changes without rebuilding. The documentation SOUNDS like I should not have to start/rebuild/restart every time I make a change to a function. In fact, issuing the sam local start-api command provides the following message...

You do not need to restart/reload SAM CLI while working on your 
functions, changes will be reflected instantly/automatically.  
You only need to restart SAM CLI if you update your AWS SAM template.  

However, changes are not detected automatically. So, changing this...

const test = async (event, context) => {
  const body = JSON.parse(event.body || {});
  return toResponse(body);
};

... to this ...

const test = async (event, context) => {
  const body = JSON.parse(event.body || {});
  body.date = new Date();
  return toResponse(body);
};

... should cause the body variable to include a date property. Unfortunately, the only way to force this is to stop (CTRL+C), and then ...

sam build
sam local start-api

This is torture as the sam build takes almost a minute on a 32GB i7 MacBook Pro.

Beshore answered 19/3, 2021 at 18:2 Comment(0)
M
1

To solve the problem of time to build, rather than building the whole template file just build the Lambda that you have made changes to.

sam build <FunctionName>
sam local start-api

The should be the same as the one you specified in the template.yaml.

Maure answered 31/3, 2021 at 13:16 Comment(1)
Thanks, but the goal is to cause the AWS SAM CLI to detect changes and handle them automatically as the CLI announces it will.Beshore
J
1

I was able to get this to work by NOT running sam build and instead just run sam local start-api. If you have run sam build delete the .aws-sam folder, then run sam local start-api again, or sam local invoke.... That should hot-load the latest code each time!

Also note the sam output hints towards this as well: "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. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template" it fails to mention deleting the folder so you don't have to rerun build each time.

Jotunheim answered 13/1, 2023 at 1:46 Comment(1)
I noticed this as well - problem seems to be that any dependencies used by the lambda will then not be available. I get e.g. the error `No module named 'aws_xray_sdk' when trying to invoke my endpoints since I use the AWS Xray tracing.Underclothes

© 2022 - 2024 — McMap. All rights reserved.