Lesson 1 of 5 AWS CloudFormation

Reading and Writing CloudFormation Templates

This lesson explains the anatomy of AWS CloudFormation templates in JSON or YAML, a core skill for the exam's provisioning domain.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the sections of a template.
  • Use intrinsic functions and pseudo parameters.
  • Use cross-stack references and dependencies.

Template sections

A template has an optional AWSTemplateFormatVersion and Description, and the following sections. Parameters accept input at stack creation, with types (including AWS-specific types such as AWS::EC2::KeyPair::KeyName and SSM parameter types), defaults, allowed values and NoEcho for sensitive input. Mappings are static lookup tables, for example instance size by environment. Conditions decide whether resources are created or properties set, for example only creating a read replica in production. Resources, the only required section, declare each resource's logical ID, type and properties. Outputs return values and can be exported for other stacks to import. Transform applies macros such as AWS::Serverless (SAM) or AWS::Include. Rules validate parameter combinations before creation.

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  Env: {Type: String, AllowedValues: [dev, prod]}
  LatestAmi: {Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>',
              Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64}
Mappings:
  Sizes: {dev: {Type: t3.small}, prod: {Type: m7i.large}}
Conditions:
  IsProd: !Equals [!Ref Env, prod]
Resources:
  WebSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: web
      SecurityGroupIngress: [{IpProtocol: tcp, FromPort: 443, ToPort: 443, CidrIp: 0.0.0.0/0}]
  Web:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !Ref LatestAmi
      InstanceType: !FindInMap [Sizes, !Ref Env, Type]
      SecurityGroupIds: [!GetAtt WebSG.GroupId]
      Monitoring: !If [IsProd, true, false]
Outputs:
  WebId:
    Value: !Ref Web
    Export: {Name: !Sub '${AWS::StackName}-WebId'}
Match the pairs

Template sections

Exam skills 3.1.2

Match each template section with what it holds.

Log in to try this activity Interactive activities are free with an account.

Intrinsic functions and pseudo parameters

Intrinsic functions connect resources. !Ref returns a parameter value or a resource's primary identifier; !GetAtt returns an attribute such as an ARN or DNS name; !Sub substitutes variables into strings; !Join, !Select, !Split and !GetAZs manipulate lists; !FindInMap reads mappings; !If, !Equals, !And, !Or and !Not implement conditions; !ImportValue reads another stack's export; and !Cidr calculates subnet CIDR blocks. Pseudo parameters such as AWS::Region, AWS::AccountId, AWS::StackName and AWS::NoValue make templates portable. Dynamic references ({{resolve:ssm:...}}, {{resolve:secretsmanager:...}}) retrieve values at deployment time without putting them in the template.

Scenario questions

Which function?

Exam skills 3.1.2

Choose the intrinsic function or pseudo parameter for each need.

Log in to try this activity Interactive activities are free with an account.

Cross-stack references and dependencies

An exported output can be imported by other stacks in the same account and Region. While another stack imports it, the export cannot be changed or deleted, which prevents breaking dependents but can block updates; nested stacks or SSM parameters are alternatives. CloudFormation infers creation order from references; DependsOn adds explicit ordering, for example making an instance wait for a NAT gateway route so that user data can download packages. The following table summarises when to use each sharing mechanism.

MechanismUse when
Exports and !ImportValueStable values shared between independently managed stacks
Nested stacksReusable components deployed together as one unit
SSM parametersLoose coupling; values that change, or sharing across tools
ParametersValues supplied at deployment time
True or false

Exports and dependencies

Exam skills 3.1.2 · 3.1.3

Check how cross-stack references and dependencies behave.

Log in to try this activity Interactive activities are free with an account.
Check yourself

Which template section is the only required one?

Show answer

Resources.

Exam Tips

Remember for the exam

  • Resources is the only required section; Outputs can be exported for cross-stack use.
  • Use SSM parameter types for the latest AMI and dynamic references for secrets.
  • An export in use by another stack cannot be changed or deleted.
  • DependsOn adds explicit ordering when references do not.