CloudFormation テンプレートから、個々のパラメーターに複数の値を使用して AWS CloudFormation スタックを作成する必要があります。どうすればよいですか?

CloudFormation には CommaDelimitedList パラメータータイプが含まれており、そのパラメーターと AWS 固有のパラメータータイプを使用して、アカウントのリソース ID 値を指定できます。CloudFormation コンソールで AWS 固有のパラメータータイプを使用して、無効な値を回避します。

AWS コマンドラインインターフェイス (AWS CLI) の create-stack CloudFormation コマンドを使用する場合、AWS 固有の list パラメータータイプのパラメーター値はカンマ区切りの文字列で渡す必要があります。

CloudFormation テンプレートの作成に関する情報については、「AWS CloudFormation の使用開始」を参照してください。

CloudFormation テンプレートに、list パラメータータイプを追加します。次の CloudFormation テンプレートの例では、キー Subnets​ を持つパラメータは、複数の異なるサブネット ID 値を受け付けることができるリストパラメータタイプを指定します。

JSON

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Parameters" : { 
   
    "VpcId" : {
      "Type" : "AWS::EC2::VPC::Id",
      "Description" : "VpcId of your existing Virtual Private Cloud (VPC)",
      "ConstraintDescription" : "must be the VPC Id of an existing Virtual Private Cloud."
    },

    "Subnets" : {
      "Type" : "List<AWS::EC2::Subnet::Id>",
      "Description" : "The list of SubnetIds in your Virtual Private Cloud (VPC)",
      "ConstraintDescription" : "must be a list of at least two existing subnets associated with at least two different availability zones. They should be residing in the selected Virtual Private Cloud."
    },

    "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "t2.small",
      "AllowedValues" : [ "t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large"],
      "ConstraintDescription" : "must be a T2 instance type."
    },

    "KeyName" : {
      "Description" : "The EC2 Key Pair to allow SSH access to the instances",
      "Type" : "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },

    "SSHLocation" : {
      "Description" : "The IP address range that can be used to SSH to the EC2 instances",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "0.0.0.0/0",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
      }
   } ,
  
    "Mappings": {
        "AWSRegionAMIEC2": {
            "ap-northeast-1": {
                "AMZLN": "ami-ceafcba8"
            }, 
            "ap-southeast-1": {
                "AMZLN": "ami-68097514"
            }, 
            "ap-southeast-2": {
                "AMZLN": "ami-942dd1f6"
            }, 
            "eu-central-1": {
                "AMZLN": "ami-5652ce39"
            }, 
            "eu-west-1": {
                "AMZLN": "ami-d834aba1"
            }, 
            "us-east-1": {
                "AMZLN": "ami-97785bed"
            }, 
            "us-west-1": {
                "AMZLN": "ami-824c4ee2"
            }, 
            "us-west-2": {
                "AMZLN": "ami-f2d3638a"
            }
        }
    }, 

  "Resources" : {

    "WebServerGroup" : {
      "Type" : "AWS::AutoScaling::AutoScalingGroup",
      "Properties" : {
        "VPCZoneIdentifier" : { "Ref" : "Subnets" },
        "LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
        "MinSize" : "1",
        "MaxSize" : "3"
      },
      "CreationPolicy" : {
        "ResourceSignal" : {
          "Timeout" : "PT15M",
          "Count"   : "1"
        }
      },

      "UpdatePolicy": {
        "AutoScalingRollingUpdate": {
          "MinInstancesInService": "1",
          "MaxBatchSize": "1",
          "PauseTime" : "PT15M",
          "WaitOnResourceSignals": "true"
        }
      }
    },
    "LaunchConfig" : {
      "Type" : "AWS::AutoScaling::LaunchConfiguration",
    "Metadata" : {
        "Comment" : "Install httpd",
        "AWS::CloudFormation::Init" : {
          "config" : {
            "packages" : {
              "yum" : {
                "httpd" : []
              }
            }
          }
        }
     },
      "Properties" : {
        "KeyName" : { "Ref" : "KeyName" },
         "ImageId": {
                    "Fn::FindInMap": [
                        "AWSRegionAMIEC2", 
                        {
                            "Ref": "AWS::Region"
                        }, 
                        "AMZLN"
                    ]
                }, 
        "SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
        "InstanceType" : { "Ref" : "InstanceType" },
        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
             "#!/bin/bash -xe\n",
             "/opt/aws/bin/cfn-init -v ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource LaunchConfig ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n",

             "/opt/aws/bin/cfn-signal -e $? ",
             "         --stack ", { "Ref" : "AWS::StackName" },
             "         --resource WebServerGroup ",
             "         --region ", { "Ref" : "AWS::Region" }, "\n"
        ]]}}
      }
    },
    "InstanceSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable SSH access ",
        "SecurityGroupIngress" : [ {
          "IpProtocol" : "tcp",
          "FromPort" : "22",
          "ToPort" : "22",
          "CidrIp" : { "Ref" : "SSHLocation"}
        } ],
        "VpcId" : { "Ref" : "VpcId" }
      }
     }
   }
  }

YAML

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  VpcId:
    Type : 'AWS::EC2::VPC::Id'
    Description : VpcId of your existing Virtual Private Cloud (VPC)
    ConstraintDescription : >-
      must be the VPC Id of an existing Virtual Private Cloud
  Subnets:
    Type: 'List<AWS::EC2::Subnet::Id>'
    Description: The list of SubnetIds in your Virtual Private Cloud (VPC)
    ConstraintDescription: >-
      must be a list of at least two existing subnets associated with at least
      two different availability zones. They should be residing in the selected
      Virtual Private Cloud.
  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t2.small
    AllowedValues:
      - t2.nano
      - t2.micro
      - t2.small
      - t2.medium
      - t2.large
    ConstraintDescription: must be a T2 instance type.
  KeyName:
    Description: The EC2 Key Pair to allow SSH access to the instances
    Type: 'AWS::EC2::KeyPair::KeyName'
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
  SSHLocation:
    Description: The IP address range that can be used to SSH to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Mappings:
  AWSRegionAMIEC2:
    ap-northeast-1:
      AMZLN: ami-ceafcba8
    ap-southeast-1:
      AMZLN: ami-68097514
    ap-southeast-2:
      AMZLN: ami-942dd1f6
    eu-central-1:
      AMZLN: ami-5652ce39
    eu-west-1:
      AMZLN: ami-d834aba1
    us-east-1:
      AMZLN: ami-97785bed
    us-west-1:
      AMZLN: ami-824c4ee2
    us-west-2:
      AMZLN: ami-f2d3638a
Resources:
  WebServerGroup:
    Type: 'AWS::AutoScaling::AutoScalingGroup'
    Properties:
      VPCZoneIdentifier: !Ref Subnets
      LaunchConfigurationName: !Ref LaunchConfig
      MinSize: '1'
      MaxSize: '3'
    CreationPolicy:
      ResourceSignal:
        Timeout: PT15M
        Count: '1'
    UpdatePolicy:
      AutoScalingRollingUpdate:
        MinInstancesInService: '1'
        MaxBatchSize: '1'
        PauseTime: PT15M
        WaitOnResourceSignals: 'true'
  LaunchConfig:
    Type: 'AWS::AutoScaling::LaunchConfiguration'
    Metadata:
      Comment: Install httpd
      'AWS::CloudFormation::Init':
        config:
          packages:
            yum:
              httpd: []
    Properties:
      KeyName: !Ref KeyName
      ImageId: !FindInMap 
        - AWSRegionAMIEC2
        - !Ref 'AWS::Region'
        - AMZLN
      SecurityGroups:
        - !Ref InstanceSecurityGroup
      InstanceType: !Ref InstanceType
      UserData: !Base64 
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -xe
            - '/opt/aws/bin/cfn-init -v '
            - '         --stack '
            - !Ref 'AWS::StackName'
            - '         --resource LaunchConfig '
            - '         --region '
            - !Ref 'AWS::Region'
            - |+

            - '/opt/aws/bin/cfn-signal -e $? '
            - '         --stack '
            - !Ref 'AWS::StackName'
            - '         --resource WebServerGroup '
            - '         --region '
            - !Ref 'AWS::Region'
            - |+

  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: 'Enable SSH access '
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref SSHLocation
      VpcId: !Ref VpcId

このページは役に立ちましたか? はい | いいえ

AWS サポート ナレッジ センターに戻る

サポートが必要ですか? AWS サポートセンターをご覧ください。

公開日: 2016 年 12 月 23 日

更新: 2018 年 08 月 02 日