Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
AWS · DVA-C03

Infrastructure as code

DVA-C03 · Lesson 18 of 24 · 2:28:53

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the DVA-C03 exam (AWS Certified Developer – Associate). In this lesson: CloudFormation templates and stacks, parameters, mappings, conditions, and outputs, intrinsic functions, dynamic references, change sets, rollback and drift detection, IAM capabilities, exports and nested stacks, AWS SAM, and the AWS CDK. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Built by hand

At AnyCompany Tickets, the first resources were created by hand in the console. Test and production no longer matched, a quick fix made in production was lost, and nobody could rebuild the app in another Region. This lesson covers CloudFormation templates and stacks, safe changes, AWS SAM, and the AWS CDK.

Templates and stacks

A CloudFormation template, in YAML or JSON, describes resources, and a stack creates and manages them as one unit. Resources is the only section that every template needs. Parameters take values at deployment, like the name of the environment, Mappings and Conditions adapt the template to each environment, and Outputs return values, like the URL of an API. Intrinsic functions connect resources: Ref returns a name or an ID, GetAtt an attribute such as an ARN, and Sub builds a string. And secrets never go in a template: a dynamic reference reads them from Parameter Store or Secrets Manager during the deployment.

Safe changes

Before an update, a change set shows what will change, including the resources that will be replaced, and nothing happens until you execute it. When an update fails, CloudFormation rolls the stack back to its last stable state, and rollback triggers also roll it back when a CloudWatch alarm goes off during a monitoring time. Drift detection finds resources that someone changed outside CloudFormation. And a template that creates IAM resources must be acknowledged with an IAM capability.

Several stacks

Large apps split into several stacks. An Output with an Export can be read by another stack with ImportValue, like the ID of a shared VPC, and an export cannot be removed while a stack imports it. Nested stacks reuse a template as a resource of a parent stack, and StackSets deploy one template to many accounts and Regions. And a DeletionPolicy of Retain or Snapshot keeps the data of a resource, like a table, when its stack is deleted.

AWS SAM

AWS SAM extends CloudFormation for serverless apps: its Transform line turns a shorter syntax into regular resources during the deployment. A serverless function resource declares the code, the runtime, its events, like an API route or a queue, and its permissions, with policy templates, like DynamoDB CRUD policy on one table. The Globals section sets shared values, like the runtime and the timeout, for every function. The command sam build prepares the code and its dependencies. And sam deploy, with the guided option the first time, saves your answers in samconfig dot toml for the next deployments.

The AWS CDK

The CDK defines infrastructure in a programming language, like Python or TypeScript. Its building blocks are constructs: low-level ones, called L1, match CloudFormation resources one to one, while higher-level ones add safe defaults and helpers, like a grant method that writes the IAM policy for you. The command cdk synth produces the CloudFormation template, cdk diff compares it with the deployed stack, and cdk deploy deploys it, after a cdk bootstrap, once per account and Region. And aspects apply a rule to every construct in a scope, like checking that each bucket blocks public access.

Recap

Let's recap. A template describes resources, a stack manages them, and only the Resources section is required. Change sets preview updates, failed updates roll back, and drift detection finds manual changes. Exports, nested stacks, and StackSets share and reuse templates, and a DeletionPolicy keeps the data. SAM shortens serverless resources, with policy templates, sam build, and sam deploy. And the CDK writes infrastructure as code in a programming language, with constructs, and synth, diff, and deploy.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A developer must update the production stack, which holds the orders table, and wants to know whether any resource will be replaced, before anything changes. What should the developer do?

    • Run drift detection on the production stack
    • Turn off rollback for the production stack
    • Create a change set, then review it
    • Deploy to a new stack, then delete the old one
    Show the answer

    Create a change set, then review it

    A change set lists every change, with the replacements, and waits for you to execute it. “Run drift detection on the production stack” would find changes made outside CloudFormation, not the changes to come. “Turn off rollback for the production stack” would remove a safety net, without showing anything. And “Deploy to a new stack, then delete the old one” would create the table again, empty, in a new stack.

  2. A team wants to declare a function, its API route, and read and write access to one DynamoDB table in a few lines, still deployed by CloudFormation. What should the team write?

    • A SAM template with a policy template
    • A CloudFormation template with nested stacks
    • A StackSet for the function and the table
    • An Output exported to the other stacks
    Show the answer

    A SAM template with a policy template

    A SAM template declares the function and its route in a few lines, and a policy template grants access to that one table. “A CloudFormation template with nested stacks” would reuse a template, without making it shorter. “A StackSet for the function and the table” would deploy the same stack to many accounts, which is not the need. And “An Output exported to the other stacks” would share a value between stacks, without declaring anything.

  3. A team writes its infrastructure in Python with the CDK. Before a deployment, it wants to see how the stack would change, compared with what is deployed now. Which command should it run?

    • cdk bootstrap
    • cdk synth
    • sam sync --watch
    • cdk diff
    Show the answer

    cdk diff

    Cdk diff compares the synthesized template with the deployed stack. “cdk bootstrap” would prepare an account and a Region for the CDK, once. “cdk synth” would produce the template, without comparing it with anything. And “sam sync --watch” would send code changes to a development stack, without any preview.