Topic lessonTopic: IaC and CloudFormation: configuration, changes and drift
Reading CloudFormation templates: logical IDs, Ref and dependencies
A CloudFormation template declares AWS resource types, settings and references. It is not a procedure executed from top to bottom. Read one short YAML example through logical IDs, resource types, property values and references; distinguish creation dependencies from working applications.
Count resource declarations, distinguish logical IDs from physical identifiers, and explain dependencies expressed by Ref and DependsOn.
A VPC scopes a network configuration; security groups control traffic for associated resources. No AWS account or template execution is needed. Indentation represents structure.
Separate the template, stack and resources
The template is the declaration file; a stack is the unit CloudFormation manages; resources are individual objects such as a VPC. Saving a file in Git does not create AWS resources. Creation and updates depend on the account, Region, permissions, inputs and other conditions.
| Section | Purpose | Distinction |
|---|---|---|
| Resources | Required declarations of resources to manage. | The declaration count does not enumerate every AWS feature or billing item. |
| Parameters | Optional inputs and constraints for creation or updates. | An input declaration is not a new AWS resource. |
| Outputs | Optional stack output values. | An output does not prove application readiness, connectivity or security. |
Read the names and hierarchy in a short YAML example
Resources:
ReportGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Reading example
VpcId: !Ref ReportNetwork
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 10.20.0.0/24
ReportNetwork:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.20.0.0/24
Outputs:
NetworkId:
Value: !Ref ReportNetwork| Code | Meaning in this example |
|---|---|
| ReportGroup / ReportNetwork | Logical IDs under Resources: names used inside the template. |
| Type: AWS::EC2::SecurityGroup | Declares ReportGroup as a security group. |
| Properties | Groups settings for that resource. VpcId and CidrBlock belong to different resource types. |
| VpcId: !Ref ReportNetwork | References ReportNetwork and uses its VPC ID. |
| Outputs → NetworkId → Value | Declares a stack output of the VPC ID; it does not create another resource. |
There are two declarations under Resources. That is not an inventory of every default component associated with VPC creation. The example declares no application, subnet or internet route. It has no ingress rule and an illustrative TCP443 egress rule for 10.20.0.0/24. This is neither a universal rule recommendation nor a validation of real connectivity or costs.
Distinguish logical IDs from Ref return values
ReportNetwork is a logical ID inside the template, not the created VPC ID. Inspect physical identifiers from creation results. Do not assume changing a logical ID is only a cosmetic rename; review how CloudFormation will treat the resource.
| Reference | Return value | Condition here |
|---|---|---|
| !Ref ReportNetwork | A VPC ID. | Check Ref for AWS::EC2::VPC. |
| !Ref ReportGroup | A security group ID in this example. | VpcId is specified. Without it, Ref returns the group name; do not generalize this case. |
| !GetAtt ReportGroup.GroupId | The GroupId attribute. | Ref and GetAtt are not universally interchangeable. |
| !Ref Stage, when Stage is a parameter | An input value such as dev. | Not a reference that creates a resource named Stage. |
Trace creation dependencies through references
- ReportNetwork → ReportGroup.VpcIdVPC is created first; use its reference value
- ReportNetwork → Outputs.NetworkIdReference the value for an output
- ReportNetwork
- VPC declaration; Ref returns its VPC ID
- ReportGroup.VpcId
- Uses the value of !Ref ReportNetwork
- Outputs.NetworkId
- Declares an output of the VPC ID
ReportGroup appears first in the file, but its VpcId references ReportNetwork, so the VPC is created first. NetworkId is an output, not another AWS resource. The arrows represent references, not packets travelling through a VPC, group and output.
Resource references through Ref, GetAtt or Sub can create implicit dependencies. DependsOn explicitly declares a required predecessor. It is not needed merely to serialize every resource, and adding it does not resolve a cycle. If A waits for B and B waits for A, reconsider the references or resource separation.
Keep inputs and outputs distinct from resource declarations
Parameters:
Stage:
Type: String
AllowedValues: [dev, prod]If Stage is supplied as dev, !Ref Stage references that value. AllowedValues constrains this input; it does not validate every property, permission or combination. Correct indentation and field names alone do not establish successful creation in the target environment.
Outputs can expose referenced identifiers, but they are not a secret-hiding store. Do not output passwords. NoEcho on a parameter does not protect values placed in Outputs or other excluded locations. Available stack outputs also do not prove every application operation succeeds.
Turn the reading sequence into a pre-change review
- Identify logical IDs and Types under Resources; count declarations.
- Read each resource’s properties, required/default values and input origins.
- Trace Ref/GetAtt/Sub and DependsOn targets; inspect dependencies and cycles.
- Distinguish Parameters and Outputs from resources; inspect secret exposure.
- Review the target environment, permissions, changes, replacement and deletion before deciding to apply.
Check logical IDs, references and dependencies
Choose an option to see the result and explanation.
Check, then explain in your own words
- Point out logical IDs, Types and Properties in the first YAML.
- Explain ReportNetwork references and creation dependencies using the diagram.
- Identify suitable outputs and the checks needed before real application.
Check your understanding: If ReportGroup.VpcId is !Ref ReportNetwork, what is established and what remains unknown?
It establishes a creation dependency on the VPC and a property using its VPC ID. The declaration alone does not prove successful creation, application readiness, connectivity or costs.
References, CloudFormation operation results and actual service behavior require different evidence.
Sources and conditions checked
The official references used in this lesson are collected here with their scope, conditions and corresponding sections. Independent expert review and live AWS comparison have not been performed.
- CloudFormation template formatChecked:
Scope and conditionsRequired Resources versus optional input/output sections; declarations do not prove execution. Documentation review; not live AWS validation.
Used in this lessonSeparate the template, stack and resources
- CloudFormation Resources syntaxChecked:
Scope and conditionsLogical IDs, resource types, properties and physical identifiers; review logical-ID changes. Documentation review; not live AWS validation.
Used in this lessonSeparate the template, stack and resources Read the names and hierarchy in a short YAML example Distinguish logical IDs from Ref return values Keep inputs and outputs distinct from resource declarations Turn the reading sequence into a pre-change review Check logical IDs, references and dependencies
- CloudFormation Parameters syntaxChecked:
Scope and conditionsInputs and AllowedValues scope; NoEcho exclusions including Outputs. Do not expose secrets. Documentation review; not live AWS validation.
Used in this lessonSeparate the template, stack and resources Distinguish logical IDs from Ref return values Keep inputs and outputs distinct from resource declarations Turn the reading sequence into a pre-change review Check logical IDs, references and dependencies
- CloudFormation Outputs syntaxChecked:
Scope and conditionsOutputs differ from resources; outputs are not a secret-hiding store or proof of application success. Documentation review; not live AWS validation.
Used in this lessonSeparate the template, stack and resources Read the names and hierarchy in a short YAML example Trace creation dependencies through references Keep inputs and outputs distinct from resource declarations Turn the reading sequence into a pre-change review Check logical IDs, references and dependencies
- CloudFormation AWS::EC2::VPCChecked:
Scope and conditionsCidrBlock and the VPC-ID Ref result; fixed declarations are not live-environment or cost validation. Documentation review; not live AWS validation.
Used in this lessonRead the names and hierarchy in a short YAML example Distinguish logical IDs from Ref return values Trace creation dependencies through references Check logical IDs, references and dependencies
Scope and conditionsRef returns an ID with VpcId, a name without it; distinguish default/explicit traffic rules and cross-reference cycles. Documentation review; not live AWS validation.
Used in this lessonRead the names and hierarchy in a short YAML example Distinguish logical IDs from Ref return values Trace creation dependencies through references Check logical IDs, references and dependencies
- CloudFormation RefChecked:
Scope and conditionsRef return values depend on the entity; distinguish parameter values from resource return values. Documentation review; not live AWS validation.
Used in this lessonRead the names and hierarchy in a short YAML example Distinguish logical IDs from Ref return values Trace creation dependencies through references Check logical IDs, references and dependencies
- CloudFormation DependsOnChecked:
Scope and conditionsExplicit and implicit dependencies; not YAML file order or application readiness. Documentation review; not live AWS validation.
Used in this lessonTrace creation dependencies through references Turn the reading sequence into a pre-change review Check logical IDs, references and dependencies