amazon-web-servicesaws-lambdaaws-cloudformationserverless-framework

Difference between SAM template and Cloudformation template


I'm finding it hard to understand the difference between SAM template and Cloudformation template. I know that SAM template can be used to define Serverless Applications like Lambda, but how does that make it different from Cloudformation template? Is the syntax different? I can still specify the Lambda definitions in cloudformation template. So, my question is why should I care about SAM? Won't knowing about just cloud formation template be sufficient?


Solution

  • From CloudFormation's perspective, SAM is a transform. Meaning: SAM templates are syntactically equivalent, but they allow you to define your serverless app with more brevity. The SAM template eventually gets expanded into full CFN behind the scenes. If you already know CFN, but want to write less YAML code, SAM may be beneficial to you. The idea is to reduce your effort.

    Furthermore, besides the template definition itself, SAM allows you to test your serverless app locally (i.e., before uploading it to AWS). And can be used to simplify certain operational tasks, for example by automatically uploading artifacts to an S3 bucket or ECR repository, or by automatically creating a basic CI/CD pipeline for you.

    One last thing to take in mind is that SAM CLI tends to be a bit more "high-level". For example, the single sam deploy command is a replacement for both aws cloudformation create-stack and aws cloudformation update-stack, meaning that it will create or update the CloudFormation stack depending on its existence. Similarly, sam logs will let you see the logs of all the resources of your serverless app.