J'ai besoin de créer une pile AWS CloudFormation à partir d'un modèle CloudFormation en utilisant plusieurs valeurs pour les paramètres individuels. Comment puis-je procéder ?

CloudFormation inclut des types de paramètres CommaDelimitedList pouvant être utilisés avec des types de paramètres AWS spécifiques pour spécifier les valeurs d'ID de ressource dans votre compte. Utilisez les types de paramètres spécifiques à AWS avec la console CloudFormation pour éviter les valeurs non valides.

Lorsque vous utilisez la commande create-stack de l'interface de ligne de commande AWS CloudFormation, vous devez transmettre les valeurs de paramètre pour les types de paramètre de liste spécifiques à AWS en tant que chaîne délimitée par des virgules.

Pour plus d'informations sur la création d'un modèle CloudFormation, consultez Mise en route avec AWS CloudFormation.

Ajoutez un type de paramètre de liste à votre modèle CloudFormation. Avec ces exemples de modèle CloudFormation, le paramètre avec la clé Sous-réseaux spécifie un type de paramètre de liste qui peut accepter plusieurs valeurs d'ID de sous-réseau distinctes :

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

Cette page vous a-t-elle été utile ? Oui | Non

Retour au Centre de connaissances AWS Support

Vous avez besoin d'aide ? Consultez le site du Centre AWS Support

Date de publication : 23/12/2016

Date de mise à jour : 02/08/2018