iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪂

How to Specify Parameter Input Order in CloudFormation

に公開

Hello, I'm Tomada.

Since I love CloudFormation, I usually create resources from templates when testing, but I've been slightly bothered by how parameters don't always appear in the order I expect when entering them.

When the parameter input order is not specified

Let's look at an example.

In the template below, the following 5 parameters are specified from the top:

  1. EnvironmentName
  2. VPCCIDR
  3. PrivateSubnetCIDR
  4. Ec2ImageId
  5. Ec2InstanceType
  6. KeyPair
...
Parameters:
  EnvironmentName:
    Type: String
    Default: xxxxxxxx-env

  VPCCIDR:
    Type: String
    Default: 10.3.0.0/16

  PrivateSubnetCIDR:
    Type: String
    Default: 10.3.0.0/24

  Ec2ImageId:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2

  Ec2InstanceType:
    Type: String
    Default: t3.nano

  KeyPair:
    Type: String
    Default: CfKeyPair
...

However, when the template is uploaded, the parameter input order becomes random and is completely different from the order in which they were written (from the top) in the template.

While this is manageable when the number of parameters is small, it can cause confusion or input errors as the number increases, so let's try specifying the input order.

Specifying the parameter input order

Setting up parameter groups

To specify the input order, the first step is to divide the parameters into several groups.
For example, you might separate VPC-related parameters from EC2-related ones.
(While you can simply put all parameters into a single group if you only want to specify the order, I recommend grouping them for better visibility.)

We will group them as follows:

  • Stack-related
    • EnvironmentName
  • VPC-related
    • VPCCIDR
    • PrivateSubnetCIDR
  • EC2-related
    • Ec2ImageId
    • Ec2InstanceType
    • KeyPair

Specifying parameter groups in the template

In this case, you specify the groups by creating a section at the beginning of the template under [Metadata] > [AWS::CloudFormation::Interface] > [ParameterGroups].

AWSTemplateFormatVersion: "2010-09-09"

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      -
        Label:
          default: Stack Configuration
        Parameters:
          - EnvironmentName
      -
        Label:
          default: VPC Configuration
        Parameters:
          - VPCCIDR
          - PrivateSubnetCIDR
      -
        Label:
          default: EC2 Configuration
        Parameters:
          - Ec2ImageId
          - Ec2InstanceType
          - KeyPair
...

Uploading the template

After uploading the template above, the parameters are now separated by group, and the input fields are organized in the same order as specified in the template.

This improves visibility during input and leads to fewer mistakes since you enter parameters in the same order every time.
Additionally, since the template itself becomes easier to read, I plan to use parameter groups actively from now on.

Discussion