Configuration Management and IaC · Topic 3 of 5
CDK, SAM and Service Catalog
Choose between the AWS CDK, SAM and Service Catalog for reusable infrastructure, and understand constructs, bootstrapping, synthesis and governed products.
- 4 min
- CDK and SAM both generate CloudFormation; the stack is still the deployment unit.
cdk synthcreates the template,cdk diffshows changes,cdk deployapplies them.cdk bootstrapis required once per account and Region before deploying.
What it is
CloudFormation templates are plain data, so reuse is limited. Three tools build on CloudFormation to raise the level: the AWS Cloud Development Kit (CDK), the AWS Serverless Application Model (SAM) and AWS Service Catalog.
The CDK lets you write infrastructure in a programming language. SAM is a shorthand for serverless applications. Service Catalog lets an administrator publish approved templates that other people can launch.
How it works
CDK concepts
A CDK app contains stacks, and stacks contain constructs. A construct is a reusable building block, and constructs come in three levels.
| Level | Description |
|---|---|
| L1 | Direct one to one mapping of a CloudFormation resource, named CfnBucket and similar |
| L2 | Curated resources with sensible defaults and helper methods, such as s3.Bucket |
| L3 | Patterns that combine several resources, such as a load balanced Fargate service |
CDK supports TypeScript, JavaScript, Python, Java, C# and Go. Because it is real code, you get loops, functions, classes, unit tests and package versioning.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
class DataStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'Logs', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.RETAIN,
});
}
}
const app = new cdk.App();
new DataStack(app, 'DataStack');
The CDK workflow
cdk bootstrap aws://111122223333/us-east-1
cdk synth
cdk diff
cdk deploy
cdk synth writes the CloudFormation template to the cdk.out folder. cdk diff compares it with the deployed stack. cdk deploy uploads assets and runs the CloudFormation deployment.
Bootstrapping prepares an account and Region once. It creates a stack, usually named CDKToolkit, with an S3 bucket and an ECR repository for assets and IAM roles that the deployment uses. Every target account and Region needs it before a CDK deploy.
CDK Pipelines builds a CodePipeline for your CDK app. The pipeline can update itself when its own definition changes, so adding a stage is a code change.
SAM
SAM is an extension of CloudFormation. A template declares Transform: AWS::Serverless-2016-10-31, and then short resource types expand into full resources.
Transform: AWS::Serverless-2016-10-31
Resources:
ItemsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Events:
Get:
Type: Api
Properties:
Path: /items
Method: get
Common types include AWS::Serverless::Function, Api, HttpApi, SimpleTable and StateMachine. The SAM command line tool adds local testing and packaging.
sam build
sam local invoke ItemsFunction --event event.json
sam deploy --guided
SAM also provides the DeploymentPreference setting that shifts Lambda traffic gradually through CodeDeploy.
Service Catalog
Service Catalog gives teams self service access to approved infrastructure. An administrator builds products from CloudFormation templates, groups them into portfolios and grants access to IAM users, groups or roles.
| Concept | Role |
|---|---|
| Product | A versioned template that users can launch |
| Portfolio | A set of products with access rules |
| Launch constraint | An IAM role Service Catalog uses to create the resources, so users need no direct rights |
| Template constraint | Rules that limit parameter values, for example allowed instance sizes |
| TagOptions | Controlled tag values applied to provisioned products |
Portfolios can be shared with other accounts and across an organization. Users launch a product and the result is a provisioned product, tracked and updatable as a unit.
Choosing
| Need | Choose |
|---|---|
| Programmatic reuse, loops, tests, higher level patterns | CDK |
| Serverless application with simple syntax and local testing | SAM |
| Governed self service for non experts with guardrails | Service Catalog |
| Plain declarative template, no build step | CloudFormation |
What to remember for the exam
- CDK and SAM both generate CloudFormation; the stack is still the deployment unit.
cdk synthcreates the template,cdk diffshows changes,cdk deployapplies them.cdk bootstrapis required once per account and Region before deploying.- L1 constructs map directly to CloudFormation, L2 adds defaults, L3 combines resources.
- SAM uses the
AWS::Serverless-2016-10-31transform. - Service Catalog launch constraints let users create resources they have no IAM permission to create directly.
- Template constraints restrict parameter choices.
- CDK Pipelines gives a self updating pipeline.
If the scenario says developers must launch only approved stacks and never touch the underlying services, Service Catalog with launch constraints is the answer.
Common mistakes
Skipping bootstrap in a new Region. The deploy fails because the asset bucket and roles do not exist.
Treating CDK as a separate engine. It produces a template, so CloudFormation limits and rollback behavior still apply.
Giving users direct permissions. Service Catalog works because users do not need rights to the resources themselves.
Hand editing synthesized templates. The next synth overwrites the change.
Do not commit secrets as plain strings in CDK code, because they end up in the template and in version control.
Check yourself
What does
cdk bootstrapcreate?A toolkit stack with an asset bucket, a container repository and IAM roles that deployments use.
Which construct level maps one to one to CloudFormation resources?
L1 constructs, whose names start with Cfn.
How can users launch infrastructure without having IAM rights to it?
A Service Catalog launch constraint supplies a role that creates the resources.
Topic 3 of 5 in Configuration Management and IaC
Test yourself on Configuration Management and IaC
The full AWS DOP-C02 question bank has 844 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.