Deployment and Automation · Topic 1 of 7
CloudFormation Templates and Stacks
Provision resources as code with CloudFormation: template sections, parameters, intrinsic functions, nested and cross stack references, and signals.
- 4 min
- A template needs a Resources section, and everything else is optional.
- A stack is the deployed instance of a template, and deleting it deletes its resources by default.
Ref,Fn::GetAtt,Fn::Sub,Fn::ImportValueandFn::FindInMapare the common intrinsic functions.
What it is
AWS CloudFormation lets you describe infrastructure in a text template, written in JSON or YAML, and then creates the resources for you. The set of resources built from one template is called a stack.
This is infrastructure as code (IaC). It makes environments repeatable, reviewable in version control, and easy to delete, because removing the stack removes its resources.
How it works
Template sections
Only the Resources section is required. The others make the template reusable and clear.
| Section | Purpose |
|---|---|
| Parameters | Values supplied when the stack is created or updated |
| Mappings | Fixed lookup tables, such as an AMI per Region |
| Conditions | Switches that decide whether a resource is created |
| Resources | The AWS resources to create (required) |
| Outputs | Values to show or export, such as a load balancer DNS name |
| Transform | Macros and the SAM (Serverless Application Model) transform |
A short example:
Parameters:
InstanceType:
Type: String
Default: t3.micro
Resources:
WebServer:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: "{{resolve:ssm:/golden/web/ami}}"
Outputs:
WebId:
Value: !Ref WebServer
Intrinsic functions
Intrinsic functions compute values inside the template.
Refreturns a parameter value or the ID of a resource.Fn::GetAttreturns an attribute, such as a bucket ARN (Amazon Resource Name).Fn::Subfills variables into a string.Fn::FindInMapreads a mapping, andFn::Ifreads a condition.Fn::ImportValuereads an output exported by another stack.
Pseudo parameters such as AWS::Region, AWS::AccountId and AWS::StackName are filled in by CloudFormation. A parameter of the SSM type, or a dynamic reference like the one in the example, reads a value from Systems Manager Parameter Store.
Ordering and dependencies
CloudFormation works out the order from references. When there is no reference, you can add DependsOn to force one resource to wait for another.
Splitting large designs
- Nested stacks make a stack a resource inside a parent stack, which helps with reuse of common parts.
- Cross stack references export an output in one stack and import it in another. A stack cannot be deleted while another stack imports its export.
Waiting for software
CloudFormation reports an EC2 resource as created once the instance is launched, not when the software on it is ready. To wait for a real signal, use a CreationPolicy on the resource, and have the instance run cfn-signal at the end of its setup. The helper script cfn-init reads the metadata of the resource to install packages and write files.
If the signal does not arrive before the timeout, the resource fails and the stack rolls back.
Deploying from the CLI
# AWS CLI
aws cloudformation validate-template --template-body file://web.yaml
aws cloudformation deploy --template-file web.yaml \
--stack-name web-stack \
--parameter-overrides InstanceType=t3.small \
--capabilities CAPABILITY_IAM
The deploy command creates the stack or updates it through a change set. A template that creates IAM resources needs an acknowledgement through --capabilities, such as CAPABILITY_IAM, or CAPABILITY_NAMED_IAM when the IAM resources have custom names.
When an exam scenario says "repeatable, version controlled, same environment in several Regions or accounts", the answer is CloudFormation (and StackSets when many accounts are involved).
What to remember for the exam
- A template needs a Resources section, and everything else is optional.
- A stack is the deployed instance of a template, and deleting it deletes its resources by default.
Ref,Fn::GetAtt,Fn::Sub,Fn::ImportValueandFn::FindInMapare the common intrinsic functions.- Exported outputs let other stacks import values, but they block deletion of the exporting stack while in use.
- Nested stacks reuse template parts, and cross stack references link independent stacks.
- CreationPolicy with
cfn-signalmakes CloudFormation wait for real readiness. - IAM changes need the
CAPABILITY_IAMorCAPABILITY_NAMED_IAMacknowledgement. aws cloudformation deploycreates or updates a stack, andvalidate-templatechecks syntax.
Common mistakes
Hard coding AMI IDs. AMI IDs differ per Region. Use a mapping or an SSM parameter instead.
Assuming created means ready. Without a signal, the stack may report success before your software has finished.
Editing resources by hand. Console changes make the real resource differ from the template. The next update can undo them, or fail.
Forgetting capabilities. A create call fails with an insufficient capabilities error when IAM resources are present and no acknowledgement is given.
Deleting an exporting stack first. Delete or update the importing stacks before the exporting one.
Changing some properties makes CloudFormation replace the resource, which creates a new physical resource and deletes the old one. Check the documentation for update behaviour before changing a database or a named resource.
Check yourself
Answer these three questions.
Which template section is required?
Resources, because a template must define at least one resource.
How do you make CloudFormation wait until software on an instance is ready?
Add a CreationPolicy and have the instance send
cfn-signalwhen setup completes.How does one stack use a value created by another?
The first stack exports an output and the second uses
Fn::ImportValue.
Topic 1 of 7 in Deployment and Automation
Test yourself on Deployment and Automation
The full AWS SOA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.