Introducing SAM Local

Testing and Debugging present a unique set of challenges in serverless applications. Firstly, serverless means that you don’t have to manage any servers. The underlying infrastructure is abstracted away from you and is managed by AWS. So how do you make sure that the code that runs successfully in the dev environment will actually run successfully in Lambda as well. Ideally, we need to test in an environment that resembles Lambda in terms of the OS, libraries, and runtime. Additionally, it would be great if the limits (memory, timeout) could be enforced locally. Also, mimic responses and logs locally. Lastly, it would be great to generate test events that are syntactically accurate and different for each trigger. This is challenging because Lambda has over fifteen different event sources, currently and each source emits its own specific event pattern to Lambda. For example, for an S3 event source, we would have to retrieve the event, save it, and then customize it for our testing needs. This process can be time consuming, especially if you have to do this for multiple event sources. The tool that helps you do all this is SAM Local.

SAM Local is an open source CLI tool for local testing of serverless apps that ensures that your application runs as expected in the production Lambda environment. It leverages Docker images to mimic Lambda’s execution environment and enforces configuration limits (same memory and timeout limits).

If you are using the API Gateway, then you can access your API on the local machine. SAM Local emulates the Lambda functions and APIs. Additionally, the event generator helps you generate event payloads for common Lambda triggers. The responses and logs are also as it would be in the Lambda environment. You can pipe the output to stdout and stderr and you can also persist the logs for further inspection. Additionally, SAM Local exposes a debugger port so you can debug your applications using any IDE.

Some of the other useful SAM Local commands help you validate the SAM template against the specification on your local machine. This provides quick feedback locally instead of discovering issues during deployment. The start API command, starts up a local http server and makes all your APIs available to you on your local machine. The invoke command allows you to locally execute your function.

All of this SAM Local functionality is available out of the box in AWS Cloud9. It provides an easy-to-use UI to strip away the complexity of SAM Local commands.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.240.75