AWS CloudFormation テンプレートの個々のパラメータに複数の値を使用するにはどうすればよいですか?

所要時間2分
0

個々のパラメータに複数の値を使用して、AWS CloudFormation テンプレートからスタックを作成または更新したいと考えています。

簡単な説明

AWS CloudFormation テンプレートの個々のパラメータには、以下の方法で複数の値を渡すことができます。

解決方法

AWS 固有のパラメータータイプを使用して、AWS アカウントから既存の AWS 値の事前入力されたリストから値を選択する

重要: AWS CloudFormation は、選択した入力値をアカウントの既存の値に照らして検証します。

次のサンプル AWS CloudFormation テンプレートでは、 SecurityGroups キーを指定したパラメータで、 SecurityGroupIdsに複数の値を受け入れることができる AWS 固有のパラメータタイプが指定されています。

JSON テンプレート:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "List<AWS::EC2::SecurityGroup::Id>",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML テンプレート:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups

AWS CLI を使用してスタックをデプロイするには、次のコマンドを使用します。

注: StackName をスタックの名前に置き換えてください。TemplateFileName を使用しているファイルの名前に置き換えてください。[ParameterValue] で、セキュリティグループ ID を入力します。

aws cloudformation create-stack --stack-name StackName --template-body file://TemplateFileName
--parameters ParameterKey=SecurityGroups,ParameterValue="sg-0123456789\,sg-2345678901"

CommaDelimitedList パラメータタイプを使用して入力値を入力する

次の AWS CloudFormation テンプレートのサンプルでは、SecurityGroups キーを指定したパラメータで、SecurityGroupIds に複数の値を受け入れることができる CommaDelimitedList タイプが指定されています。

JSON テンプレート:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SecurityGroups": {
      "Type": "CommaDelimitedList",
      "Description": "The list of SecurityGroupIds in your Virtual Private Cloud (VPC)",
      "Default": "sg-a123fd85, sg-b456ge94"
    }
  },
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-79fd7eee",
        "KeyName": "testkey",
        "SecurityGroupIds": {
          "Ref": "SecurityGroups"
        }
      }
    }
  }
}

YAML テンプレート:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SecurityGroups:
    Type: CommaDelimitedList
    Description: The list of SecurityGroupIds in your Virtual Private Cloud (VPC)
    Default: sg-a123fd85, sg-b456ge94
Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: ami-79fd7eee
      KeyName: testkey
      SecurityGroupIds: !Ref SecurityGroups

AWS公式
AWS公式更新しました 1年前
コメントはありません

関連するコンテンツ