Sub topic 5
Pseudo parameters: AWS::Region, AWS::StackName and friends
This chapter assumes you can read a CloudFormation YAML template and know how Ref and Fn::Sub work in basic form. You should also know that a stack is the set of resources created from one template.
What pseudo parameters are
A pseudo parameter is a value that CloudFormation predefines for every stack. You don't declare it in the Parameters section. It exists so a template can learn facts about the deployment, such as the account, the Region, or the stack's name, without a person typing them in.
You read a pseudo parameter the same way you read a regular parameter: with Ref, or with ${...} inside Fn::Sub. The names always start with AWS::.
| Pseudo parameter | Returns | Typical use |
|---|---|---|
| `AWS::AccountId` | The 12-digit account ID | Globally unique names, ARNs |
| `AWS::Region` | The Region code, such as `eu-west-1` | Names, ARNs, endpoints, conditions |
| `AWS::StackName` | The name of the stack | Tags, name prefixes, export names |
| `AWS::StackId` | The stack's full ARN | Unique tags, tracing |
| `AWS::Partition` | `aws`, `aws-cn` or `aws-us-gov` | Building ARNs that work in every partition |
| `AWS::URLSuffix` | The domain suffix, usually `amazonaws.com` | Building service endpoints |
| `AWS::NotificationARNs` | The list of SNS topic ARNs for stack events | Passing them to a resource |
| `AWS::NoValue` | Nothing | Removing a property conditionally |
A worked example
The template below uses several of them. It builds a bucket name that is unique per account and Region, builds an ARN that works in any partition, and drops a property when a condition is false.
AWSTemplateFormatVersion: "2010-09-09"
Description: Pseudo parameter demo
Parameters:
EnvName:
Type: String
Default: dev
AllowedValues:
- dev
- prod
Conditions:
IsUsEast1: !Equals [!Ref AWS::Region, us-east-1]
Resources:
LogBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "logs-${AWS::AccountId}-${AWS::Region}-${EnvName}"
AlertTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: !If [IsUsEast1, "Primary alerts", !Ref AWS::NoValue]
Tags:
- Key: StackName
Value: !Ref AWS::StackName
ReadLogsPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: !Sub "arn:${AWS::Partition}:s3:::${LogBucket}/*"
Outputs:
BucketUrl:
Description: Regional URL of the log bucket
Value: !Sub "https://${LogBucket}.s3.${AWS::Region}.${AWS::URLSuffix}"
StackArn:
Value: !Ref AWS::StackIdFour patterns appear here, and you will use them constantly.
- Unique names. S3 bucket names are global across all accounts. Adding the account ID and Region avoids collisions, and it lets the same template deploy to several Regions without edits.
- Partition-safe ARNs. Hard-coding
arn:aws:breaks in China and GovCloud.${AWS::Partition}keeps the template portable. - Endpoints from the suffix.
${AWS::URLSuffix}avoids hard-codingamazonaws.com. - Conditional properties. When
IsUsEast1is false,AWS::NoValuemakes CloudFormation behave as ifDisplayNamewere never written.
Note that ${LogBucket} inside Fn::Sub is the same as Ref on the bucket, which returns the bucket name.
How the pieces are resolved
CloudFormation fills in the values when it processes the template for a given stack in a given Region. The same template file therefore produces different resolved values in different deployments. That is the main reason to prefer pseudo parameters over typed-in parameters for facts that CloudFormation already knows. A person cannot enter a wrong Region by mistake if the template reads it directly.
Common errors and edge cases
- You can't declare or override them. Defining a parameter named
AWS::Regionis not valid, and--parameter-overridescan't change one. If you need a value that differs from the real Region, add a normal parameter. Mappingscannot contain them. TheMappingssection holds literal values only. UseAWS::Regionas a lookup key instead:!FindInMap [RegionMap, !Ref AWS::Region, ImageId]. If the Region has no entry in the map, the operation fails with a mapping error, so list every Region you deploy to.AWS::NoValueonly works throughRef. It is meant forFn::Ifbranches and similar places. Writing it as a plain string does not remove anything.AWS::StackNamecan lengthen names beyond limits. Many resources cap names, and S3 buckets allow 63 characters. A long stack name plus account ID and Region can exceed the cap, so check the length of the result.- Names must stay valid. Stack names may contain uppercase letters, but S3 bucket names may not. Avoid
${AWS::StackName}in a bucket name unless you control the stack name's case. AWS::StackNameis fixed for the life of a stack. Treat it as stable. Embedding it in a name that is a replacement-triggering property is safe only because it never changes.AWS::NotificationARNsreturns a list. Use it where a list is expected, and don't interpolate it into a string withFn::Sub.
Exercises
Exercise
You want one template to deploy to us-east-1 and eu-west-1 and to give an S3 bucket a unique, valid name in each. Write the BucketName value, and explain why you would not just use a parameter for the Region.
Show answer
BucketName: !Sub "data-${AWS::AccountId}-${AWS::Region}". CloudFormation already knows the Region, so reading AWS::Region means no one can enter a value that disagrees with where the stack actually runs, and the account ID keeps the global bucket name from colliding with other accounts.
Exercise
A property should be present only in us-east-1 and omitted everywhere else. Which pseudo parameter and which intrinsic functions do you use?
Show answer
Define a condition such as IsUsEast1: !Equals [!Ref AWS::Region, us-east-1]. Then set the property to !If [IsUsEast1, <value>, !Ref AWS::NoValue]. In other Regions, AWS::NoValue makes CloudFormation omit the property.