Sub topic 1
From console clicks to declarative infrastructure
Prerequisites
This chapter assumes you have:
- Basic familiarity with core AWS services such as S3, IAM and EC2.
- The ability to read YAML or JSON.
- Comfort running commands in a terminal.
- An AWS account and the AWS CLI configured with credentials.
The chapter introduces the model that the rest of the tutorial builds on. Later chapters cover each template section and each stack operation in detail.
Why clicking does not scale
You can build an application in the AWS console by creating a bucket, a role, a queue and an instance by hand. This works for a first experiment. It causes problems once the environment matters:
- No record. The console does not keep a reviewable history of why a setting has its current value.
- Hard to repeat. Building a second copy for testing or for another Region means redoing every click. Small differences creep in.
- Hard to review. A teammate cannot comment on a change before it happens.
- Hard to undo. Removing an environment means remembering every resource you created and in what order to delete them.
Infrastructure as code (IaC) addresses these problems. You describe infrastructure in text files, keep them in version control, and let a tool make AWS match the description.
Declarative versus imperative
An imperative script lists steps: create a bucket, then enable versioning, then add a tag. If a step fails halfway, or you run the script twice, you must handle those cases yourself.
A declarative description states the end result: a bucket exists, versioning is enabled, and it carries this tag. The tool compares the description with reality and decides which actions to take. CloudFormation is declarative.
| Aspect | Imperative script | Declarative template |
|---|---|---|
| You write | Steps to run | The desired end state |
| Running twice | May fail or duplicate resources | Converges on the same state |
| Ordering | You manage it | The service derives it from references |
| Changing a resource | You write the migration steps | You edit the description and redeploy |
| Cleanup | You track what to delete | The service deletes what it created |
CloudFormation's model: templates and stacks
CloudFormation has two core concepts.
A template is a text file in YAML or JSON that describes resources and their properties. It is only a description. Writing a template creates nothing.
A stack is the set of AWS resources that CloudFormation creates from one template and manages as a single unit. When you give CloudFormation a template and a stack name, it creates a stack. Create, update and delete operations apply to the whole stack.
The same template can produce many stacks. Each stack has its own name, its own parameter values, and its own physical resources. A dev stack and a prod stack can share one template and still be independent.
What CloudFormation does for you
When you submit a template, CloudFormation does the following:
- It validates the template syntax and reads the resources it declares.
- It works out an order. If one resource refers to another, the referenced one is created first. Independent resources can be created in parallel.
- It calls the API of each service (S3, IAM, SQS and so on) using your credentials, or using a service role if you supply one.
- It records the result in the stack, including the physical identifier of every resource.
- If a creation fails, it rolls back by deleting what it created, so you do not keep a half-built environment.
Later updates compare the new template with the stack's current state. CloudFormation changes only the resources whose definitions differ. Deleting the stack removes its resources.
A minimal example
This template declares one S3 bucket. It omits the bucket name, so CloudFormation generates a unique one. A fixed name would fail if another account already used it.
AWSTemplateFormatVersion: "2010-09-09"
Description: Minimal stack with one S3 bucket
Resources:
NotesBucket:
Type: AWS::S3::BucketSave it as minimal.yaml and deploy it:
aws cloudformation deploy \
--template-file minimal.yaml \
--stack-name minimal-demo
aws cloudformation describe-stack-resources \
--stack-name minimal-demo \
--query "StackResources[].[LogicalResourceId,PhysicalResourceId,ResourceStatus]" \
--output table
aws cloudformation delete-stack --stack-name minimal-demoThe output shows two names for the same resource. NotesBucket is the logical ID, which you chose and which is stable inside the template. The physical ID is the real bucket name that S3 holds. Template edits refer to logical IDs. Keep this distinction in mind, because renaming a logical ID later is treated as removing one resource and adding another.
Limits of the model
Declarative infrastructure is not magic. Keep these edge cases in mind:
- The stack is the source of truth, but only for what it manages. If someone edits a stack's bucket in the console, the template no longer matches reality. This is called drift, and a later chapter covers how to detect it.
- Not every change is gentle. Some property edits replace a resource with a new one. For a database this can mean data loss unless you plan for it. Later chapters on update behaviors and change sets cover this.
- Templates have size limits. A template passed directly in a request can be up to 51,200 bytes. One stored in S3 can be up to 1 MB. A template can declare at most 500 resources. Large systems are split across several stacks.
- Coverage varies. Most AWS services have CloudFormation resource types, but new features sometimes arrive in the console or API first.
- Failures are normal. Permissions, quotas and invalid property values cause failed operations. The stack events list shows which resource failed and why.
Where this fits in the tutorial
The next chapters take the template apart section by section: the overall anatomy, then resources, parameters, functions and outputs. After that, the tutorial covers the stack lifecycle and the safeguards around it. Keep the model from this chapter in mind: a template describes, a stack realizes, and CloudFormation reconciles the two.
Exercise
You deploy the same template twice with two different stack names, app-dev and app-prod. How many stacks and how many sets of physical resources exist, and what happens to the app-prod resources if you delete app-dev?
Show answer
There are two stacks, and each has its own separate set of physical resources, even though both came from one template. Deleting app-dev removes only the resources in that stack. The app-prod stack and its resources are unaffected.
Exercise
In the minimal example, you rename the logical ID NotesBucket to DocsBucket and redeploy. What does CloudFormation do, and why does this matter for a bucket that holds data?
Show answer
CloudFormation treats the rename as removing the resource NotesBucket and adding a new resource DocsBucket. It creates a new bucket and then deletes the old one. Any objects in the old bucket are lost (and deletion fails if the bucket is not empty) unless you protect it, for example with a retention policy. Logical IDs identify resources in the stack, so renaming one is not a harmless edit.