Search code examples
javascripttypescriptamazon-web-servicesaws-lambdaaws-sam-cli

AWS Lambda - SAM CLI - Faster way to develop & debug locally


Project Info:

  • Service: AWS Lambda
  • CLI Tool: SAM CLI
  • Language: TypeScript

I was setting up a lambda project & while setting up the environment for the team, I realized that in order to do local debugging, I do need to use sam build and sam local start-api command for every change to be reflected on localhost. The question is that is there any other way of doing it using SAM CLI as it is getting super slow :(

Thanks for your time :)


Solution

  • For a SAM project I worked on - I was embedding express inside a lambda - and deploying this into SAM.

    For local testing and debugging, I just ran the express server, instead of the lambda handler.

    If it's other lambdas, you can just invoke them them directly instead of hosting inside SAM (or SAM local).

    Once my local tests passed, I could them deploy once to SAM local and run my test cases. Then deploy to AWS - and again run my tests against the hosted SAM.

    This worked, but it was still quite annoying - SAM local did not behave the same as SAM AWS, and it was slow.

    I ended up ditching SAM local, and just executed all my tests against my local express server, and then created a development stack in AWS - and ran my tests against that.

    Depending on your project, and the nature of the lambdas, testing against the lambdas directly might give you a much better development flow.

    Also, you might find there are some workflow improvements using LocalStack, although I have not used this.