How to force AWS SAM CLI to detect changes?

Viewed 1357

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.

1 Answers

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.

Related