Get startedGet started for free

AWS SAM and CDK for serverless and IaC

1. AWS SAM and CDK for serverless and IaC

Welcome back! In this video, you'll deploy a serverless app with the SAM CLI, synthesize a CDK application, and pick the right tool between CDK, SAM, and raw CloudFormation. Let's get started.

2. A 2,000-line YAML file

Picture writing a CloudFormation template for a single Lambda function behind API Gateway. By the time you've spelled out the Lambda, the IAM role, the log group, and the permission grants, you're at 2,000 lines of YAML. A typo halfway down a permission policy breaks the deploy. Now imagine writing the same thing in five lines using SAM, or as Python with the AWS CDK. Both compile to CloudFormation, but you write a fraction of the code.

3. AWS SAM at a glance

That pain is exactly why AWS built SAM. The Serverless Application Model is a CloudFormation extension that gives you concise resource types for serverless.. Instead of declaring an AWS Lambda function, an IAM role, log group, and event source separately, you declare an AWS::Serverless::Function and SAM expands that into a full CloudFormation template behind the scenes. The SAM CLI handles building, packaging, and deploying. The output is plain CloudFormation, so anything you know about stacks still applies.

4. The SAM workflow

The SAM workflow is four commands you run in sequence, each feeding the next. sam init scaffolds a project from a template, like "Python with API Gateway," and sam validate then checks it for errors before you waste time building. From there, sam build compiles and packages your code, and sam deploy uploads the artifact and creates or updates the CloudFormation stack. There's also sam local invoke, which runs the function on your laptop using the same event format AWS would send, for fast iteration.

5. AWS CDK at a glance

The AWS Cloud Development Kit or CDK lets you describe infrastructure in a real programming language: TypeScript, JavaScript, Python, Java, C#, or Go. You write classes that represent stacks and resources, then run cdk synth to compile that code into a CloudFormation template. cdk deploy ships the synthesized template to AWS as a regular CloudFormation stack. The CDK gives you loops, conditionals, functions, and your IDE's autocomplete, but the runtime stack is just CloudFormation underneath.

6. CDK construct levels

The CDK organizes constructs into three levels. L1 constructs are one-to-one wrappers around CloudFormation resources, named with a Cfn prefix, every property explicit. L2 constructs are curated, opinionated wrappers with sensible defaults, like an S3 bucket that defaults to encryption on. L3 constructs, sometimes called patterns, bundle multiple resources for a common use case, like a load-balanced ECS Fargate service in one class. Higher levels mean fewer lines but less control. L2 is the sweet spot, and where most CDK code ends up.

7. When to use each

Choosing among CloudFormation, SAM, and the CDK comes down to your team and your workload. If you want maximum control or your ops team already thinks in YAML, stay on raw CloudFormation. If you are building a pure serverless app, Lambda plus API Gateway plus a queue or table, SAM is the fastest path. If the infrastructure is complex or your team already writes TypeScript or Python, reach for the CDK. Whichever you pick, the deployment is a CloudFormation stack underneath, so the same skills carry over.

8. All three deploy as CloudFormation

Whichever toolkit you choose, the stack you end up with is CloudFormation. SAM transforms your template into CloudFormation at deploy time. CDK synthesizes a CloudFormation template from your code. Either way, the stack supports the same events log, drift detection, change sets, and automatic rollback you saw in the first video. That's the most important thing to internalize: under the hood you are always running CloudFormation, and SAM and CDK simply make it faster to author.

9. Let's practice!

Time to compare these Infrastructure as Code tools hands-on. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.