CloudFormation Concepts

CloudFormation is AWS’s infrastructure-as-code execution engine.
CDK uses it under the hood — understanding CloudFormation helps you debug deployment failures and understand what CDK is actually doing.


1. What Is CloudFormation?

CloudFormation is an AWS service that provisions and manages infrastructure from declarative templates.

You describe what you want (resources and their configuration), and CloudFormation figures out how to create, update, or delete them in the correct order.

Core idea:

Template (JSON/YAML) → CloudFormation Stack → AWS Resources

In the CDK context:

Python CDK Code → cdk synth → CloudFormation Template → cdk deploy → AWS Resources

2. Templates

A CloudFormation template is a JSON or YAML document that declares the resources to create.

Key Sections

SectionRequiredPurpose
AWSTemplateFormatVersionNoTemplate version (always 2010-09-09)
DescriptionNoHuman-readable description
ParametersNoInput values at deploy time
ResourcesYesThe AWS resources to provision
OutputsNoValues to export from the stack

Minimal Example

AWSTemplateFormatVersion: "2010-09-09"
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration:
        Status: Enabled

CDK generates these templates for you in the cdk.out/ directory after cdk synth.


3. Stacks

A stack is the deployed instance of a template. It is CloudFormation’s unit of management.

  • One template → one stack
  • A stack owns all the resources it creates
  • You create, update, and delete stacks as a unit

In this assignment:

  • StorageStack → one CloudFormation stack (S3 buckets + DynamoDB)
  • LambdaStack → one CloudFormation stack (Lambdas + API Gateway + EventBridge)

Stack Naming

  • CDK uses the class name (e.g., StorageStack) as the CloudFormation stack name by default.

4. Stack Lifecycle States

Every stack has a status that reflects where it is in its lifecycle.

Normal States

StateMeaning
CREATE_IN_PROGRESSResources are being created
CREATE_COMPLETEAll resources created successfully
UPDATE_IN_PROGRESSResources are being updated
UPDATE_COMPLETEAll updates applied successfully
DELETE_IN_PROGRESSResources are being deleted
DELETE_COMPLETEStack fully deleted

Failure States

StateMeaning
CREATE_FAILEDStack creation failed; CloudFormation may auto-rollback
ROLLBACK_IN_PROGRESSReversing a failed create
ROLLBACK_COMPLETECreate failed and rolled back; stack exists but is empty
UPDATE_ROLLBACK_IN_PROGRESSReversing a failed update
UPDATE_ROLLBACK_COMPLETEUpdate failed and rolled back to previous state
UPDATE_ROLLBACK_FAILEDRollback itself failed; stack is stuck
DELETE_FAILEDDelete failed; stack is stuck

UPDATE_ROLLBACK_FAILED and DELETE_FAILED are the “stuck” states where normal CDK commands no longer work and you need to intervene directly through the CloudFormation console or CLI.


5. How CloudFormation Updates Work

When you run cdk deploy on an existing stack, CloudFormation computes a changeset — the diff between the current template and the new one.

Three possible outcomes per resource:

ActionMeaning
No changeResource is unchanged; CloudFormation leaves it alone
Modify (in place)Resource can be updated without replacement
ReplaceThe change requires creating a new resource and deleting the old one

Why Replacement Is Dangerous for Stateful Resources

Replacement means:

  1. New resource is created (e.g., new S3 bucket)
  2. Dependencies are re-wired to the new resource
  3. Old resource is deleted

If the old resource holds data (S3 bucket with objects, DynamoDB table with records), that data is gone.

In this project, RemovalPolicy.DESTROY + auto_delete_objects=True makes this especially aggressive — CloudFormation will delete all bucket contents on resource deletion.


6. Logical IDs vs Physical Names

Every resource in a CloudFormation template has two identifiers:

IdentifierWhat It IsExample
Logical IDName in the template; unique within the stackDataBucket
Physical NameActual AWS resource name/ARN; may be auto-generatedlambdastack-databucket12345-abc

CDK generates logical IDs automatically from the construct tree path.

Best practice (and assignment requirement): do not hardcode physical names for deployable resources. Let CloudFormation generate them. This avoids naming conflicts when deploying to multiple accounts or regions.

Acceptable exception: DynamoDB GSI names are part of the table schema and are passed to Lambda code via environment variables.


7. Outputs and Cross-Stack References

Outputs

You can export values from one stack so other stacks or tools can use them.

Outputs:
  BucketName:
    Value: !Ref MyBucket
    Export:
      Name: MyStack-BucketName

Cross-Stack References in CDK

In CDK, passing a resource from one stack to another (e.g., passing the S3 bucket from StorageStack to LambdaStack) automatically creates CloudFormation outputs and imports.

# In app.py
storage = StorageStack(app, "StorageStack")
LambdaStack(app, "LambdaStack", data_bucket=storage.data_bucket)

This creates a hard dependency between the stacks. CloudFormation will:

  • Always deploy StorageStack before LambdaStack
  • Refuse to delete StorageStack while LambdaStack still imports its outputs

8. Custom Resources

Custom resources let CloudFormation invoke a Lambda to handle provisioning steps that CloudFormation doesn’t natively support.

In this project, CDK automatically creates:

  • Custom::S3BucketNotifications — configures S3 event notifications by invoking a CDK-managed Lambda

Why This Matters for Debugging

Custom resources run during the stack update/delete, not just at create time.

Common failure pattern in this project:

  1. You delete or replace an S3 bucket
  2. CloudFormation tries to clean up the old BucketNotifications custom resource
  3. The custom resource Lambda tries to remove notifications from the deleted bucket
  4. AWS returns NoSuchBucket
  5. The custom resource fails → stack gets stuck in DELETE_FAILED

This is why stack cleanup sometimes requires the --retain-resources or --deletion-mode FORCE_DELETE_STACK flags.


9. The Bootstrap Stack

Before deploying CDK apps, you must run cdk bootstrap.

This creates a special CloudFormation stack named CDKToolkit in your account+region. It provisions:

  • An S3 bucket for storing synthesized assets (Lambda zips, etc.)
  • An ECR repository for Docker image assets
  • IAM roles that CloudFormation uses to deploy on CDK’s behalf

You only need to bootstrap once per account+region combination.

cdk bootstrap --app "./.venv/bin/python app.py"

10. Useful CloudFormation CLI Commands

View stack events (to diagnose failures)

aws cloudformation describe-stack-events \
  --stack-name StorageStack \
  --region us-east-1

View all stacks

aws cloudformation list-stacks \
  --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
  --region us-east-1

Force-delete a stuck stack

aws cloudformation delete-stack \
  --stack-name StorageStack \
  --region us-east-1 \
  --retain-resources DataBucketNotifications11EB1C2E \
  --deletion-mode FORCE_DELETE_STACK

Wait for deletion to complete

aws cloudformation wait stack-delete-complete \
  --stack-name StorageStack \
  --region us-east-1

Describe a specific stack

aws cloudformation describe-stacks \
  --stack-name StorageStack \
  --region us-east-1

11. CDK → CloudFormation Mapping

CDK ActionWhat CloudFormation Does
cdk synthGenerates templates; nothing deployed yet
cdk deployUploads assets, creates a changeset, executes it
cdk destroyCalls DeleteStack on each stack
cdk diffCreates a changeset (preview only, does not execute)

Key insight: CDK is just a template generator. CloudFormation is always the execution engine. When something goes wrong during deployment, the root cause is always in CloudFormation events — not in CDK output.


12. Quick Reference: What To Check When Deployment Fails

  1. Run cdk deploy and note the error message
  2. Open the AWS Console → CloudFormation → find the failing stack
  3. Go to the Events tab — find the first FAILED event
  4. Read the Status Reason — it usually names the exact resource and the exact API error
  5. If the stack is stuck in UPDATE_ROLLBACK_FAILED or DELETE_FAILED, use the AWS CLI recovery commands above

CDK error messages are often just Failed resources: with a list of logical IDs. The actual error is always in the CloudFormation events.