Sub topic 4
Pseudo parameters: AWS::Region, AWS::StackName and friends
This chapter assumes you can read a basic CloudFormation template and know how Ref and Fn::Sub work in outline. Pseudo parameters are values that CloudFormation fills in for you. You don't declare them in the Parameters section. You use them as if they were parameters, and their values depend on where and how the stack is deployed.
The working model
A pseudo parameter is a predefined name that always resolves when a stack is created or updated. You read it with Ref or with ${...} inside Fn::Sub. Because the values come from the deployment context, the same template can run in any account or Region without edits.
| Pseudo parameter | Value |
|---|---|
| `AWS::Region` | The Region where the stack is created, such as `us-east-1` |
| `AWS::AccountId` | The 12-digit ID of the account that owns the stack |
| `AWS::StackName` | The name given to the stack |
| `AWS::StackId` | The full ARN of the stack |
| `AWS::Partition` | `aws`, `aws-cn` or `aws-us-gov`, depending on the partition |
| `AWS::URLSuffix` | The domain suffix for the partition, usually `amazonaws.com` |
| `AWS::NoValue` | Removes a property when used with `Fn::If` |
AWS::NotificationARNs also exists. It returns the list of SNS topic ARNs that receive stack events.
Using them
The most common pattern is building unique, readable names with Fn::Sub. S3 bucket names must be unique across all accounts, so including the account ID and Region avoids collisions.
AWSTemplateFormatVersion: "2010-09-09"
Description: Pseudo parameters in names, ARNs and optional properties
Parameters:
EnableLogging:
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
Conditions:
LoggingOn: !Equals [!Ref EnableLogging, "true"]
Resources:
AppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${AWS::StackName}-${AWS::AccountId}-${AWS::Region}"
LoggingConfiguration: !If
- LoggingOn
- DestinationBucketName: !Sub "${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-logs"
- !Ref AWS::NoValue
Outputs:
PolicyResourceArn:
Description: Bucket ARN built with the partition pseudo parameter
Value: !Sub "arn:${AWS::Partition}:s3:::${AppBucket}"
Region:
Value: !Ref AWS::RegionThe Fn::If returns the logging block when the condition is true. Otherwise it returns AWS::NoValue, which makes CloudFormation behave as if the property were never written. Note that this example only illustrates the syntax. A real logging destination bucket would need to exist and allow log delivery.
Common errors
- Hardcoded ARNs. Writing
arn:aws:s3:::...fails in China and GovCloud partitions. Use${AWS::Partition}. - Declaring them as parameters. Names starting with
AWS::are reserved. You can't define your own. - Uppercase bucket names.
AWS::StackNamemay contain uppercase letters, which S3 rejects. Use a lowercase stack name, or a different naming scheme. - Assuming a fixed length. Stack names can be long, and many resources limit name length. Check the limit for the resource type.
- Using
AWS::NoValueoutsideFn::If. It works withRefanywhere, but its purpose is to drop a property conditionally.
Exercise
You deploy the same template as stack app in account 111122223333 in two Regions. A resource uses BucketName: !Sub "${AWS::StackName}-${AWS::AccountId}". Why does the second deployment fail, and what is the fix?
Show answer
Both deployments produce the bucket name app-111122223333. S3 bucket names are global, so the second creation collides with the first. Add ${AWS::Region} to the name, giving app-111122223333-us-east-1 and app-111122223333-eu-west-1.