CDK: how to run and debug locally without SAM? #1352
-
Before asking, I searched a lot but only found this related topic: It seems there are no other methods to run / debug the code locally without using SAM, right? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @kenberkeley, Generally speaking SAM is a good way to run and debug your Lambda functions locally. This is because behind the scenes SAM spins up a docker container and runs your code into an environment that is very close to the one where your function runs on AWS Lambda. This allows you to have confidence that the same packages, file system structure, and environment variables that you'd have on Lambda are also in your local test environment. This however has a cost in terms of feedback loop and complexity that can vary depending on your machine as spinning a container for each iteration is somewhat resource intensive. Another option would be to treat your functions as just that, and test/debug them as pure functions. This can be considered a low fidelity test as most of the environment in which your function runs will be different from the one it'll run once deployed. However this type of setup allows you to iterate much faster by simply invoking your function with Node.js and pass it an event. Both strategies can be valuable and I encourage you to evaluate the trade offs described above and pick the option that is more appropriate for your needs. An additional note here would be that none of these methods should be considered as a real replacement for tests run on a real AWS environment. This is because there are aspects like IAM permissions, trigger patterns/behaviors, or integration with other services that can't be reliably mocked locally. |
Beta Was this translation helpful? Give feedback.
Thanks @dreamorosi for your incredibly detailed and patient reply, I appreciate it.
Indeed SAM is a good way to run and debug Lambda functions locally. However, with the presence of CDK (my beloved wife) in my project, SAM is like a redundant mistress/ex-wife 😂 Additionally, we expect that the API can run locally (hot reload please) with a single endpoint (like
localhost:8080/api
) to facilitate development/debugging/writing E2E tests, etc.Next, the "same packages, file system structure, and environment variables" benefits you mentioned are manageable and provisionable, which I am not too worried about. Finally, in terms of "integration with other AWS services", we have
saml2aws
to log in…