INFRASTRUCTURE LEARNINGINFRASTRUCTURE LEARNING
インフラ大全インフラ大全IT INFRASTRUCTURE / LEARNING
Infrastructure learning guide
Find a lesson or lab/

Esc to close · Search terms stay on your device

LEARNING日本語

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.

What you will learn

Count resource declarations, distinguish logical IDs from physical identifiers, and explain dependencies expressed by Ref and DependsOn.

Prerequisite

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.

Three sections to identify first
SectionPurposeDistinction
ResourcesRequired declarations of resources to manage.The declaration count does not enumerate every AWS feature or billing item.
ParametersOptional inputs and constraints for creation or updates.An input declaration is not a new AWS resource.
OutputsOptional stack output values.An output does not prove application readiness, connectivity or security.

Read the names and hierarchy in a short YAML example

Reading-only YAML; this site does not create resourcesScroll horizontally to read long lines. Use arrow keys in the code area.
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
Match each part of the code to its role
CodeMeaning in this example
ReportGroup / ReportNetworkLogical IDs under Resources: names used inside the template.
Type: AWS::EC2::SecurityGroupDeclares ReportGroup as a security group.
PropertiesGroups settings for that resource. VpcId and CidrBlock belong to different resource types.
VpcId: !Ref ReportNetworkReferences ReportNetwork and uses its VPC ID.
Outputs → NetworkId → ValueDeclares 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.

Ref means different things for different targets
ReferenceReturn valueCondition here
!Ref ReportNetworkA VPC ID.Check Ref for AWS::EC2::VPC.
!Ref ReportGroupA security group ID in this example.VpcId is specified. Without it, Ref returns the group name; do not generalize this case.
!GetAtt ReportGroup.GroupIdThe GroupId attribute.Ref and GetAtt are not universally interchangeable.
!Ref Stage, when Stage is a parameterAn input value such as dev.Not a reference that creates a resource named Stage.

Trace creation dependencies through references

References in this example; lines are not a network path
References in this example; lines are not a network path1: ReportNetwork — VPC is created first; use its reference value — ReportGroup.VpcId. 2: ReportNetwork — Reference the value for an output — Outputs.NetworkId12ReportNetworkReportGroup.VpcIdOutputs.NetworkIdReferences in this example; lines are not a network path1: ReportNetwork — VPC is created first; use its reference value — ReportGroup.VpcId. 2: ReportNetwork — Reference the value for an output — Outputs.NetworkId12ReportNetworkReportGroup.VpcIdOutputs.NetworkId
  1. ReportNetwork → ReportGroup.VpcIdVPC is created first; use its reference value
  2. 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

Additional reading fragment: Stage is an input, not a complete standalone templateScroll horizontally to read long lines. Use arrow keys in the code area.
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

  1. Identify logical IDs and Types under Resources; count declarations.
  2. Read each resource’s properties, required/default values and input origins.
  3. Trace Ref/GetAtt/Sub and DependsOn targets; inspect dependencies and cycles.
  4. Distinguish Parameters and Outputs from resources; inspect secret exposure.
  5. 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.

How many resources are declared directly under Resources in the first YAML?
Read the answer and reason

Answer: Two: ReportGroup and ReportNetwork · NetworkId is under Outputs and does not add a Resources declaration.

ReportGroup is written first. Which resource is the creation prerequisite in this example?
Read the answer and reason

Answer: ReportNetwork, referenced by VpcId through Ref · ReportGroup uses the VPC reference value during creation.

Which reading of !Ref ReportGroup is correct for this example?
Read the answer and reason

Answer: With VpcId specified, this type’s Ref returns the group ID · Check this condition in the resource reference; distinguish the group-name result when VpcId is omitted.

A references B and B references A, forming a cycle. What should you do?
Read the answer and reason

Answer: Reconsider references or resource separation · The mutual dependency must be resolved; moving lines does not resolve it.

A password is placed in Outputs while its input has NoEcho. Is the secret protected there?
Read the answer and reason

Answer: No; do not expose secrets through Outputs · Masking an input and protecting an explicitly emitted output are different things.

Check, then explain in your own words

  1. Point out logical IDs, Types and Properties in the first YAML.
  2. Explain ReportNetwork references and creation dependencies using the diagram.
  3. 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.