Sub topic 2
Declaring resources
This chapter assumes you can read YAML and know what an S3 bucket and an IAM role are. You should also know the template sections from the previous chapter. Here you will learn how the Resources section works.
What a resource declaration contains
Resources is the only required section of a template. Each entry has a logical ID and a body.
- The logical ID is the name you pick, such as
AppBucket. It must be alphanumeric and unique within the template. Typeis required. It names the AWS resource to create, in the formservice::group::resource, for exampleAWS::S3::Bucket.Propertiesholds the settings for that type. Whether each property is required depends on the type.
The logical ID is not the name of the real resource. CloudFormation creates a physical ID, such as a generated bucket name, when it provisions the resource. Generated names let you deploy the same template several times without name collisions.
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: EnabledFinding the right properties
The resource type reference in the AWS documentation lists every property of a type. For each property it shows whether it is required and its update behavior. It also lists the values that Ref and Fn::GetAtt return. Check that page before you write a resource, because the same idea can have different property names on different services.
Connecting resources
Resources usually need each other. Use !Ref to point to another resource, and !GetAtt to read one of its attributes. This example creates a role and an instance profile that uses it.
Resources:
AppRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
AppProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref AppRoleFor an IAM role, Ref returns the role name. For a bucket, it returns the bucket name. The Ref return value differs by type, so look it up in the reference. These references also tell CloudFormation the order of creation: AppRole is created before AppProfile.
Common mistakes
- Setting a fixed name, such as
BucketName, on a resource. Names must be unique, and many properties cannot change without replacing the resource. Let CloudFormation generate names unless you need a fixed one. - Misspelling a property. CloudFormation rejects unknown properties, and the stack fails to validate or create.
- Assuming
Refalways returns the name. It can return an ID or an ARN, depending on the type. - Renaming a logical ID. CloudFormation treats it as a new resource, so it creates the new one and deletes the old one.
Exercise
You rename the logical ID of an AWS::S3::Bucket resource from AppBucket to DataBucket and update the stack. What does CloudFormation do?
Show answer
It treats DataBucket as a new resource. It creates a new bucket and then deletes the old AppBucket during cleanup, because the logical ID is how CloudFormation tracks a resource. Any data in the old bucket is at risk unless a retention policy protects it.