Sub topic 1
From console clicks to declarative infrastructure
Prerequisites
This chapter assumes you know the basics of S3, IAM and EC2, can read YAML, and can run commands in a terminal. You also need an AWS account with the AWS CLI configured. No earlier chapter is required.
From console clicks to declarative infrastructure
Creating resources by hand in the console is fast for a one-off test. It does not scale. Nobody can review the clicks, repeating them in a second account invites mistakes, and the result lives only in someone's memory.
Infrastructure as code (IaC) replaces the clicks with a text file that describes what you want. CloudFormation is the AWS service that reads such a file. Its model has three parts:
- A template is a YAML or JSON file that declares resources and their properties.
- A stack is the set of AWS resources CloudFormation creates from one template, managed as a single unit.
- The service works out the order of operations and calls the underlying service APIs for you.
The template is declarative. You describe the end state ("a bucket with versioning on"), not the steps to reach it. When you change the template and update the stack, CloudFormation compares the old and new descriptions and changes only what differs. Because the file lives in version control, you can review, diff and reuse it.
Anatomy of a template
A template is a set of top-level sections. Only Resources is required. Here is a summary:
| Section | Purpose | Required |
|---|---|---|
| `AWSTemplateFormatVersion` | Template format version. The only valid value is `"2010-09-09"`. | No |
| `Description` | Free text describing the template. | No |
| `Parameters` | Values you supply when you create or update the stack. | No |
| `Mappings` | Fixed lookup tables, such as settings per environment. | No |
| `Conditions` | Boolean tests that switch resources or properties on and off. | No |
| `Resources` | The AWS resources to create. | Yes |
| `Outputs` | Values to show after deployment or export to other stacks. | No |
The following template uses all of them. A parameter selects an environment, a mapping and a condition vary the bucket's settings, and an output exports the bucket ARN.
AWSTemplateFormatVersion: "2010-09-09"
Description: Environment-aware S3 bucket
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Description: Deployment environment
Mappings:
EnvSettings:
dev:
RetentionDays: 7
prod:
RetentionDays: 90
Conditions:
IsProd: !Equals [!Ref EnvName, prod]
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: !If [IsProd, Enabled, Suspended]
LifecycleConfiguration:
Rules:
- Id: ExpireOldObjects
Status: Enabled
ExpirationInDays: !FindInMap [EnvSettings, !Ref EnvName, RetentionDays]
Tags:
- Key: Name
Value: !Sub "${AWS::StackName}-${EnvName}"
Outputs:
BucketArn:
Description: ARN of the application bucket
Value: !GetAtt AppBucket.Arn
Export:
Name: !Sub "${AWS::StackName}-BucketArn"Read it from the top. Each resource has a logical ID (AppBucket), a Type and Properties. The !Ref, !If, !FindInMap, !Sub and !GetAtt entries are intrinsic functions, which later chapters cover in detail. For now, notice that they connect the sections: the parameter feeds the condition and the mapping, and those feed the resource.
Exercise
Which template section is the only one required, and what is the difference between a template and a stack?
Show answer
Resources is the only required section. A template is the file that describes the resources. A stack is the set of real AWS resources CloudFormation creates and manages from that template.