Sub topic 3
Declaring resources
This chapter assumes you can read YAML, know what S3, SQS and IAM are, and have seen the layout of a template. Resources are the only required section of a template. Every other section exists to support them.
What a resource declaration contains
A resource is one entry under the top-level Resources key. Each entry has a logical ID, a Type, and usually a Properties block.
AWSTemplateFormatVersion: "2010-09-09"
Description: A bucket and a parameter that records its name
Resources:
ArchiveBucket:
Type: AWS::S3::Bucket
Properties:
Tags:
- Key: Purpose
Value: archive
BucketNameParameter:
Type: AWS::SSM::Parameter
Properties:
Name: /demo/archive-bucket
Type: String
Value: !Ref ArchiveBucket- Logical ID (
ArchiveBucket): the name you use inside the template. It must be alphanumeric (A-Z, a-z, 0-9) and unique within the template. - Type (
AWS::S3::Bucket): the resource type, written asservice::product::resource. It tells CloudFormation which API calls to make. - Properties: the settings for that type. Which properties are required, and which are accepted, depends on the type. The resource type reference in the AWS documentation lists them.
Some entries have more attributes next to Type, such as DependsOn, DeletionPolicy, UpdateReplacePolicy, Condition and Metadata. Later chapters cover these in detail. This chapter only covers the basic declaration.
Logical IDs and physical IDs
The logical ID belongs to your template. The physical ID is the identifier of the real resource, for example an S3 bucket name or an EC2 instance ID. CloudFormation records the mapping between the two in the stack.
If you leave out a name property, CloudFormation generates a physical ID. It combines the stack name, the logical ID and a random suffix, so you get something like demo-archivebucket-1a2b3c4d5e6f.
This mapping has a consequence for edits. CloudFormation identifies a resource by its logical ID. If you rename ArchiveBucket to LogBucket and update the stack, CloudFormation treats it as a deletion of one resource and a creation of another. The old resource is removed unless a DeletionPolicy retains it. Choose logical IDs you can live with.
Referring to another resource
The Ref function returns the value that identifies a resource. In the example above, !Ref ArchiveBucket gives the bucket's name. CloudFormation sees the reference and creates the bucket before the SSM parameter. The next chapters cover intrinsic functions and dependencies in full.
What Ref returns depends on the resource type:
| Resource type | `Ref` returns |
|---|---|
| `AWS::S3::Bucket` | Bucket name |
| `AWS::SQS::Queue` | Queue URL |
| `AWS::EC2::Instance` | Instance ID |
| `AWS::IAM::Role` | Role name |
Use Fn::GetAtt when you need another attribute, such as an ARN. The documentation for each type has a Return values section that lists both.
Choosing names on purpose
Properties such as BucketName or QueueName let you fix the physical name. This has costs:
- Some updates require replacement, which means creating a new resource and then deleting the old one. A fixed name makes this fail, because the new resource cannot take the name while the old one still exists.
- A stack with fixed names cannot be deployed twice in one account and Region. Names such as S3 buckets must be unique across all accounts.
Letting CloudFormation generate names avoids both problems. Use fixed names only when something outside the stack needs a predictable name.
Resource types you will meet
Resource types fall into three groups.
- AWS resource types begin with
AWS::, such asAWS::EC2::VPC. - Third-party and private types are published in the CloudFormation registry. Their names use a different namespace, for example
MyOrg::Monitoring::Dashboard. - Special types include
AWS::CloudFormation::Stack(a nested stack) andCustom::types (custom resources). Both appear in later chapters.
A misspelled type is rejected during validation. A type that exists but is not offered in your Region fails when you create or update the stack.
Common errors
- Wrong indentation.
TypeandPropertiesmust be siblings under the logical ID, and property names must be nested underProperties. - Missing required property. The stack operation fails, and the error names the property.
- Invalid property value. CloudFormation accepts the template, but the service rejects the value during creation, and the stack rolls back.
- Duplicate logical IDs. In YAML, a repeated key can silently overwrite the first, so check for copy-paste mistakes.
- Name collision. A fixed physical name that already exists fails the create.
Run aws cloudformation validate-template --template-body file://template.yaml before deploying. It catches syntax and structure errors. It does not check whether property values are valid for the service, so the service can still reject a template that passes validation.
Exercise
You rename the logical ID of a bucket in your template from ArchiveBucket to LogBucket and update the stack. The bucket has no DeletionPolicy. What does CloudFormation do, and how would you avoid it?
Show answer
CloudFormation sees a new resource (LogBucket) and a removed one (ArchiveBucket). It creates a new bucket and deletes the old one, along with the old bucket's configuration. A bucket that still holds objects cannot be deleted, so the delete would fail. To avoid this, keep the original logical ID. If the name must change, set DeletionPolicy: Retain on the old resource first, or move the resource with import.