Study guide
Technical reference and lesson notes
AWS CloudFormation Template Deep Dive
Purpose of This Lesson
This lesson explains how AWS CloudFormation templates describe the desired end state of infrastructure and how CloudFormation uses those templates to provision resources. The focus is on template sections, logical and physical IDs, intrinsic functions, and the decisions involved in using parameters, mappings, conditions, outputs, and transforms.
CloudFormation templates use YAML or JSON. A template can be uploaded directly to CloudFormation or stored in Amazon S3 and referenced when creating a stack. CloudFormation reads the template and makes the required AWS API calls. The resulting collection of provisioned resources is a stack.
Key Concepts
Logical IDs and physical IDs
- A logical ID is the identifier assigned to a resource inside the template. Other template resources and functions use it to reference that resource.
- A physical ID identifies the actual deployed resource outside the template after CloudFormation creates it. Depending on the resource, this may be an instance ID, bucket name, VPC ID, or another AWS-generated or configured identifier.
Intrinsic functions
Intrinsic functions are built-in CloudFormation functions that calculate or retrieve values during template processing or deployment. They are useful when a value is not known until runtime or when one resource needs a value from another resource.
#### Ref
Ref returns the value of a parameter or resource identified by its logical name:
- For a parameter, it returns the parameter value.
- For a resource, it returns a value used to refer to that resource, typically its physical ID.
For example, an Elastic IP association can use Ref to obtain the instance ID of an EC2 instance declared elsewhere in the template.
#### Fn::GetAtt
Fn::GetAtt returns a specific attribute from a resource. Use it when the required value is an attribute rather than the resource’s general reference value. The lecture example uses it to retrieve load balancer-related attributes such as the source security group owner alias and source security group name.
The long YAML form is:
Fn::GetAtt:
- ResourceLogicalId
- AttributeName
The short YAML form is commonly written as:
!GetAtt ResourceLogicalId.AttributeName
#### Fn::FindInMap
Fn::FindInMap retrieves a value from a two-level map declared in the Mappings section. It is useful when a template must select a value based on two keys, such as a region and an architecture.
A common pattern is a region map containing AMI IDs for different architectures. The resource then uses Fn::FindInMap to select the appropriate AMI for the deployment region and architecture.
CloudFormation Template Structure
Resources — required
Resources is the only required top-level section. It declares the AWS resources that CloudFormation must create or manage, such as EC2 instances, S3 buckets, and other infrastructure components.
Resources can reference one another using logical IDs and intrinsic functions.
Parameters — optional
Parameters lets users provide custom values when creating or updating a stack. Parameters improve template reuse by allowing the same template to accept different deployment choices.
A parameter can define:
- Its type, such as
String. AllowedValues, which restrict valid inputs.- A
Defaultvalue used when the user does not provide another value.
For example, a parameter can restrict an EC2 instance type to three approved values and select one of them by default.
Mappings — optional
Mappings defines key-value structures that are fixed in the template. A mapping associates a key with a corresponding set of named values.
Mappings are especially useful for region-specific or architecture-specific values. For example, a RegionMap can contain different AMI IDs for different architectures in us-east-1 and us-west-1. This allows one template to deploy EC2 instances across regions while selecting the appropriate AMI.
Outputs — optional
Outputs declares values that CloudFormation should expose after stack creation or update. Outputs can be viewed in the CloudFormation console, returned through stack-related calls, or used for cross-stack referencing.
For example, a stack can output a VPC ID so another stack can import and use it.
Conditions — optional
Conditions defines logic that controls whether resources or configurations are created. A condition can compare a parameter value with a specified value.
For example, a resource can be created only when an EnvType parameter equals prod. If the parameter has another value, the condition fails and the resource is not created.
Conditions are useful for environment-specific infrastructure without requiring separate templates.
Transform — optional
Transform specifies one or more macros that CloudFormation uses to process the template. Transforms can reference additional code or template snippets stored in Amazon S3.
Important transforms covered in the lesson include:
- AWS SAM transform:
AWS::Serverless-2016-10-31. This enables AWS Serverless Application Model syntax, simplifying the definition of serverless resources such as Lambda functions. The transform determines how CloudFormation processes the SAM syntax. - AWS Include transform:
AWS::Include. This works with template snippets stored separately from the primary CloudFormation template.
An AWS SAM example can define a Lambda function and refer to its code through a CodeUri pointing to an S3 location.
Exam- or Assessment-Relevant Takeaways
- Know that
Resourcesis mandatory;Parameters,Mappings,Outputs,Conditions, andTransformare optional. - Distinguish
ReffromFn::GetAtt: useReffor a parameter value or a resource’s general reference value, and useFn::GetAttfor a named resource attribute. - Recognize
Fn::FindInMapas the function used to retrieve a value from a two-level mapping, such as a region-and-architecture AMI lookup. - Use parameters when deployment-time customization and template reuse are required.
- Use mappings for fixed lookup data embedded in the template, especially region-specific values.
- Use conditions when resource creation depends on a deployment choice such as production versus non-production.
- Use outputs when values must be exposed to users or made available for cross-stack referencing.
- Recognize the AWS SAM transform as the mechanism that enables SAM syntax in a CloudFormation template.
- Remember that CloudFormation provisions the declared end state by making AWS API calls; the deployed collection of resources is the stack.
Tool / Feature Decision Guide
| Requirement | Appropriate feature | Reason |
|---|---|---|
| Supply a value when creating or updating a stack | Parameters | Allows deployment-time customization and template reuse |
| Select a fixed value based on region and another key | Mappings with Fn::FindInMap | Performs a two-level lookup within template-defined data |
| Reference a parameter or a resource generally | Ref | Returns the parameter value or the resource’s reference value, typically its physical ID |
| Retrieve a particular resource attribute | Fn::GetAtt | Returns a named attribute from a declared resource |
| Create resources only for selected environments | Conditions | Controls resource creation based on logical tests |
| Publish a VPC ID or another stack value | Outputs | Exposes values for console/API viewing and cross-stack use |
| Simplify serverless resource definitions | AWS SAM transform | Enables SAM syntax and processing in CloudFormation |
| Reuse a separately stored template fragment | AWS Include transform | Incorporates template snippets stored separately from the main template |
Common Traps / Misconceptions
- Confusing logical and physical IDs: A logical ID exists in the template; a physical ID identifies the deployed AWS resource.
- Using
Reffor every lookup:Refdoes not retrieve arbitrary attributes. UseFn::GetAttwhen a specific attribute is required. - Treating parameters and mappings as interchangeable: Parameters are supplied or selected at deployment time; mappings are predefined lookup data in the template.
- Assuming outputs automatically create resources: Outputs only expose values; they do not provision infrastructure.
- Forgetting that conditions can prevent creation: A resource associated with a false condition is not created.
- Assuming transforms are ordinary resources: A transform changes how CloudFormation processes the template or incorporates additional content.
- Overlooking the required section: A template must contain
Resources, even if it also contains parameters, mappings, or transforms.
Real-World Engineer / Analyst Notes
- Design reusable templates by keeping environment-specific choices in parameters and fixed regional lookups in mappings.
- Restrict parameter values with
AllowedValueswhen only approved instance types or deployment options should be accepted. - Use conditions to keep one template suitable for multiple environments while preventing production-only resources from appearing elsewhere.
- Make important identifiers available through outputs, especially when another stack or operational workflow needs a VPC or similar shared resource ID.
- When debugging references, first determine whether the consumer needs a resource’s general identifier (
Ref) or a specific attribute (Fn::GetAtt). - Treat S3-stored templates and snippets as deployment dependencies. The CloudFormation stack must be able to access the referenced template content.
Quick Reference Summary
- Template formats: YAML or JSON.
- Deployment input: Direct upload to CloudFormation or a template stored in S3.
- Result: A CloudFormation stack containing the provisioned resources.
- Required section:
Resources. - Runtime/reference functions:
Ref,Fn::GetAtt, andFn::FindInMap. - Customization:
Parameters. - Static lookup data:
Mappings. - Exposed or shared values:
Outputs. - Conditional creation:
Conditions. - Template processing and extensions:
Transform. - Serverless syntax: AWS SAM transform.
- Separate template snippets: AWS Include transform.
Flashcards
Q: A CloudFormation resource needs the instance ID of an EC2 resource declared elsewhere in the same template. Which intrinsic function should be used?
A: Use Ref with the EC2 resource’s logical ID. For a resource, Ref returns the value used to refer to it, typically its physical ID.
Q: When should Fn::GetAtt be chosen instead of Ref?
A: Choose Fn::GetAtt when the template needs a specific named attribute of a resource rather than its general reference value.
Q: A template must select an AMI based on both AWS Region and CPU architecture. Which feature combination fits this requirement?
A: Use a two-level Mappings section and retrieve the value with Fn::FindInMap. This supports fixed region-and-architecture lookup data.
Q: Which CloudFormation template section is mandatory?
A: Resources is mandatory. It declares the AWS resources that CloudFormation will provision or manage.
Q: When are parameters preferable to mappings?
A: Use parameters when the value should be supplied or selected during stack creation or update. Use mappings when the lookup values are fixed in the template.
Q: What does Ref return when given a parameter logical name?
A: It returns the parameter’s value.
Q: What is the purpose of the Outputs section?
A: It exposes selected stack values for viewing or stack-related responses and can provide values for cross-stack referencing.
Q: A resource should exist only when EnvType equals prod. Which CloudFormation feature expresses this decision?
A: Use a Condition that evaluates the EnvType parameter. The resource is created only when the condition evaluates to true.
Q: What is the operational effect of a false condition attached to a resource?
A: CloudFormation does not create that resource when the condition evaluates to false.
Q: What does the AWS SAM transform enable in a CloudFormation template?
A: It enables AWS Serverless Application Model syntax and tells CloudFormation how to process that syntax, simplifying definitions such as Lambda functions.
Q: When would the AWS Include transform be useful?
A: Use it when template snippets are stored separately from the main CloudFormation template and need to be incorporated during template processing.
Q: What is the difference between a logical ID and a physical ID?
A: A logical ID is the template-local identifier used for references. A physical ID identifies the actual deployed AWS resource outside the template.
Practice Questions
Question 1
A team wants one EC2 CloudFormation template to work in multiple Regions. Each Region requires different AMI IDs, and the AMI also varies by architecture. The values should remain defined in the template rather than entered by the operator. What should the engineer use?
A. Parameters with AllowedValues
B. Outputs with Ref
C. A two-level mapping with Fn::FindInMap
D. Conditions with Fn::GetAtt
Correct answer: C. The decisive clues are fixed template data and two lookup keys: Region and architecture. A mapping stores the values, and Fn::FindInMap retrieves the correct AMI.
Question 2
A stack creates a VPC, and another stack needs the resulting VPC ID. Which design best supports this requirement?
A. Add the VPC ID to AllowedValues
B. Define the VPC ID in Outputs
C. Store the VPC ID in Conditions
D. Retrieve the VPC ID with Fn::FindInMap only
Correct answer: B. The Outputs section exposes stack values and supports cross-stack referencing. The VPC itself remains declared under Resources.
Question 3
A CloudFormation resource requires a specific attribute from a load balancer, such as a security group-related attribute, rather than the load balancer’s general identifier. Which function should be selected?
A. Ref
B. Fn::GetAtt
C. Fn::FindInMap
D. Fn::Include
Correct answer: B. Fn::GetAtt is designed to return a named attribute from a resource. Ref returns the resource’s general reference value instead.
Question 4
An organization uses one template for development and production, but a particular resource must be created only in production. Which approach is most appropriate?
A. Put the resource in Outputs and hide it in development
B. Use a Mappings entry for the production resource
C. Define a condition based on an environment parameter
D. Use Fn::FindInMap to create the resource conditionally
Correct answer: C. A condition can compare an environment parameter with prod and prevent the resource from being created when the comparison is false.
WordPress Metadata
Suggested Slug:
aws-cloudformation-template-sections-intrinsic-functions
Meta Description:
Study AWS CloudFormation template structure, intrinsic functions, parameters, mappings, outputs, conditions, transforms, and key deployment decisions for the SOA-C03 exam.
Tags:
AWS CloudFormation, AWS Certified CloudOps Engineer, SOA-C03, Infrastructure as Code, CloudFormation templates, Intrinsic functions, AWS SAM, Stack automation