30
Test your AWS Lambda function locally
What is AWS SAM?
SAM stands for Serverless Application Model and it is a framework that is used for developing and deploying serverless applications. The idea is that you can create a very simple SAM YAML file which would then generate more complex CloudFormation templates.
All the configuration is done in YAML and it supports anything from CloudFormation like Parameters, Resources, Mapping, Outputs, etc...
SAM stands for Serverless Application Model and it is a framework that is used for developing and deploying serverless applications. The idea is that you can create a very simple SAM YAML file which would then generate more complex CloudFormation templates.
All the configuration is done in YAML and it supports anything from CloudFormation like Parameters, Resources, Mapping, Outputs, etc...
SAM gives you the ability to help you run Lambda, API Gateway, DynamoDB locally. Where you can then test your serverless solution before deploying to AWS.
Differences between SAM and CloudFormation:
You must include the Transform Header within your template and it indicates it is a SAM template.
You must include the Transform Header within your template and it indicates it is a SAM template.
Then you have the following types for resources:
Important commands to know:
First create a folder for your application and then you can use VS Code or any editor of your choice to open that folder.




The template.yaml file:

As we can see at the top the Transform: AWS::Serverless-2016-10-31 is included, which has to be included within the SAM template.
Next under Resources we can see that we have a Lambda function and it's type is AWS::Serverless::Function, which you include with your Lambda function in a SAM template.

As we can see at the top the Transform: AWS::Serverless-2016-10-31 is included, which has to be included within the SAM template.
Next under Resources we can see that we have a Lambda function and it's type is AWS::Serverless::Function, which you include with your Lambda function in a SAM template.
Under properties next to CodeUri, we need to specify where our lambda files will be, which in this case is under the folder hello_world. The handler will be the file name where the handler function is located (app.py) and our handler name (lambda_handler), which is why it is added as app.lambda_handler. The runtime is what we selected during the initializing stage so depending on what you choose this might be different for you.
With this Hello World example the also included an event attached to our function, which is an API and it is a get method.
With this Hello World example the also included an event attached to our function, which is an API and it is a get method.
IMPORTANT: To be able to test this template locally you will need to install Docker first as it will use Docker to run your function locally Download Docker here

This is a quick way to get up and running with AWS SAM so you can test out, edit and deploy functions from your local machine. You can add code to your function and test the changes and play around with some other features of SAM. This quick short template once deployed to AWS will generate a more complex CloudFormation template and then deploy the stack to your AWS account.
30