Pseudo Parameters in AWS CloudFormation

When working with AWS CloudFormation, there are various features and functionalities that can simplify the process of deploying and managing infrastructure as code. One such feature is the use of pseudo parameters. Pseudo parameters are predefined variables that can be used within CloudFormation templates to provide dynamic information about the stack, region, and other resources. In this article, we will explore what pseudo parameters are and how they can be used effectively in AWS CloudFormation.

What are Pseudo Parameters?

Pseudo parameters are predefined variables that are automatically available within AWS CloudFormation templates. These parameters cannot be modified or overridden and are specific to each stack. They provide useful information about the stack, resources, and the AWS environment in which the stack is being deployed. Pseudo parameters can be used in various sections of a CloudFormation template, such as the Resources, Outputs, and Metadata sections.

Using pseudo parameters in our CloudFormation templates can help make our templates more flexible and reusable. They eliminate the need for hardcoding values and enable our templates to adapt to different environments and regions without any manual intervention.

Available Pseudo Parameters in AWS CloudFormation

Let’s take a look at some of the commonly used pseudo parameters in AWS CloudFormation:

  • AWS::AccountId: This pseudo parameter returns the AWS account ID of the account in which the stack is being created. It can be used to create unique resource names or to configure resources that are specific to a particular AWS account.
  • AWS::Region: The AWS::Region pseudo parameter returns the AWS region in which the stack is being created. It is useful when we want to create region-specific resources or configure resources based on the region.
  • AWS::StackId: This pseudo parameter returns the unique ID of the stack. It can be used to associate resources with the stack or to create resource names that are unique within the stack.
  • AWS::StackName: The AWS::StackName pseudo parameter returns the name of the stack. It can be used to reference the stack name in resource configurations or to create resource names that are based on the stack name.
  • AWS::NoValue: This pseudo parameter can be used to specify a value of “null” or “empty” for a parameter or property. It is useful when we want to conditionally include or exclude certain resources or properties based on user input.
  • AWS::NotificationARNs: This pseudo parameter is used to return the list of notification Amazon Resource Names (ARNs) for the current CloudFormation stack.
  • AWS::Partition: This pseudo parameter is used to return the partition that the resource is in. For standard AWS Regions, the partition is aws. For resources in other partitions, the partition is aws-partitionname. For example, the partition for resources in the China (Beijing and Ningxia) Region is aws-cn and the partition for resources in the AWS GovCloud (US-West) region is aws-us-gov.
  • AWS::URLSuffix: This pseudo parameter is used to return the suffix for a domain. The suffix is typically amazonaws.com, but might differ by Region. For example, the suffix for the China (Beijing) Region is amazonaws.com.cn.

Pseudo parameters in AWS CloudFormation is very easy to use.

Examples of Using Pseudo Parameters in AWS CloudFormation Templates

Now, let’s see some examples of how these pseudo parameters can be used in AWS CloudFormation templates:

Example 1: Creating a Unique S3 Bucket Name

In this example, we want to create an S3 bucket with a name that is unique within the AWS account. We can achieve this by using the AWS::AccountId pseudo parameter in the bucket name:

Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub my-bucket-${AWS::AccountId}

By appending the AWS::AccountId pseudo parameter to the bucket name, we ensure that the bucket name is unique within the AWS account, regardless of the region or stack name.

Example 2: Creating Region-Specific Resources

In this example, we want to create an Amazon RDS database instance in a region-specific subnet. We can use the AWS::Region pseudo parameter to specify the desired region:

Resources:
  MyDBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceIdentifier: my-db-instance
      Engine: MySQL
      MasterUsername: admin
      MasterUserPassword: password
      VPCSecurityGroups:
        - Fn::ImportValue: my-vpc-security-group
      AvailabilityZone: !Sub ${AWS::Region}a
      MultiAZ: false

By using the AWS::Region pseudo parameter in the AvailabilityZone property, we ensure that the database instance is created in the specified region’s availability zone.

Example 3: Creating Resource Names Based on Stack Name

In this example, we want to create an Amazon SNS topic with a name that is based on the stack name. We can use the AWS::StackName pseudo parameter to achieve this:

Resources:
  MyTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: !Sub my-topic-${AWS::StackName}

By appending the AWS::StackName pseudo parameter to the topic name, we ensure that the topic name is based on the stack name and remains consistent even if the stack is updated or recreated.

Conclusion

Pseudo parameters in AWS CloudFormation provide a convenient way to access dynamic information about the stack, resources, and AWS environment. They enable us to create flexible and reusable templates that can adapt to different environments and regions. By understanding the available pseudo parameters and their usage, we can enhance the power and flexibility of our CloudFormation templates.