Sub topic 5
Intrinsic functions: Ref, GetAtt, Sub, Join and Select
This chapter assumes you can read YAML, know the template sections (Parameters, Resources, Outputs), and have created a stack from the AWS CLI. Intrinsic functions are built-in functions that CloudFormation evaluates while it processes a template. They let a value depend on a parameter, on another resource, or on the stack itself.
Ref
!Ref is the short form of Ref. It returns different things depending on what it points to:
- A parameter: the parameter's value.
- A resource: usually the physical ID, such as a bucket name or an instance ID. Some resource types return an ARN or another identifier instead, so check the "Return values" section of the resource's documentation.
- A pseudo parameter such as
AWS::Region: its value.
Fn::GetAtt
!GetAtt reads a named attribute of a resource, such as an ARN or a DNS name. Write it as !GetAtt LogicalId.Attribute. Each resource type documents the attributes it supports.
Fn::Sub, Fn::Join and Fn::Select
!Sub replaces ${Name} placeholders in a string. A placeholder can name a parameter, a resource (same as Ref), Resource.Attribute (same as GetAtt), or a pseudo parameter. !Join glues a list of values together with a delimiter. !Select picks one item from a list by zero-based index.
AWSTemplateFormatVersion: "2010-09-09"
Description: Intrinsic function examples
Parameters:
EnvName:
Type: String
Default: dev
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${AWS::StackName}-${AWS::AccountId}-${EnvName}"
Outputs:
BucketName:
Description: Physical name of the bucket
Value: !Ref AppBucket
BucketArn:
Description: ARN of the bucket
Value: !GetAtt AppBucket.Arn
ObjectsArn:
Description: ARN pattern that matches every object
Value: !Join ["", [!GetAtt AppBucket.Arn, "/*"]]
FirstZone:
Description: First Availability Zone in the Region
Value: !Select [0, !GetAZs ""]
ObjectsArnSub:
Description: Same pattern written with Sub
Value: !Sub "${AppBucket.Arn}/*"ObjectsArn and ObjectsArnSub produce the same result. !Sub is usually easier to read, because the string looks like the final value.
How the functions connect resources
When one resource uses !Ref or !GetAtt on another, CloudFormation creates the referenced resource first. That is an implicit dependency, covered in a later chapter.
Common errors
- Using
!GetAttwith an attribute the resource does not support. The stack fails validation or creation. - Putting a
!Refinside a!Substring with the wrong syntax. Write${AppBucket}, not${!Ref AppBucket}. - Writing a literal
${...}in a!Substring. Escape it as${!Literal}, which outputs${Literal}. - Nesting short-form functions that cannot be nested.
!GetAttshort form can't contain another function in its attribute name, so use the long formFn::GetAtt: [Id, !Ref Name]when the attribute name is dynamic.
Exercise
A bucket has the logical ID LogsBucket. Write a single !Sub expression that produces the ARN pattern for every object in it, and say which function ${LogsBucket.Arn} corresponds to.
Show answer
!Sub "${LogsBucket.Arn}/*". The placeholder ${LogsBucket.Arn} works like !GetAtt LogsBucket.Arn, while ${LogsBucket} alone works like !Ref LogsBucket.