Come faccio a passare i parametri CommadeLimitedList agli stack annidati in AWS CloudFormation?

3 minuti di lettura
0

Desidero passare i parametri CommadeLimitedList agli stack annidati in AWS CloudFormation.

Breve descrizione

Non puoi passare valori di tipo CommaDelimitedList a uno stack annidato. Usa invece la funzione intrinseca Fn::Join nello stack principale per convertire il tipo CommaDelimitedList nel tipo String.

Risoluzione

L'esempio seguente mostra come passare un elenco di SecurityGroupIds da uno stack principale a uno stack annidato.

  1. Apri il file JSON o YAML dello stack principale, quindi imposta il tipo di SecurityGroupIds su CommadeLimitedList.

Nella sezione Risorse del file JSON, la funzione Fn::Join restituisce la stringa combinata. Nella sezione Risorse del file YAML, la funzione !Join restituisce la stringa combinata. In entrambi i file JSON e YAML, la stringa combinata converte il tipo di parametro SecurityGroupIds da CommadeLimitedList a String.

Esempio di file JSON principale:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SubnetId": {
      "Type": "AWS::EC2::Subnet::Id"
    },
    "SecurityGroupIds": {
      "Type": "List<AWS::EC2::SecurityGroup::Id>"
    },
    "KeyName": {
      "Type": "AWS::EC2::KeyPair::KeyName"
    },
    "ImageId": {
      "Type": "String"
    }
  },
  "Resources": {
    "Instance": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml",
        "Parameters": {
          "SubnetId": {
            "Ref": "SubnetId"
          },
          "SecurityGroupIds": {
            "Fn::Join": [
              ",",
              {
                "Ref": "SecurityGroupIds"
              }
            ]
          },
          "KeyName": {
            "Ref": "KeyName"
          },
          "ImageId": {
            "Ref": "ImageId"
          }
        }
      }
    }
  }
}

Esempio di file YAML principale:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SubnetId:
    Type: 'AWS::EC2::Subnet::Id'
  SecurityGroupIds:
    Type: 'List<AWS::EC2::SecurityGroup::Id>'
  KeyName:
    Type: 'AWS::EC2::KeyPair::KeyName'
  ImageId:
    Type: String
Resources:
  Instance:
    Type: 'AWS::CloudFormation::Stack'
    Properties:
      TemplateURL: 'https://s3.amazonaws.com/cloudformation-templates-us-east-2/nested.yml'
      Parameters:
        SubnetId: !Ref SubnetId
        SecurityGroupIds: !Join
          - ','
          - !Ref SecurityGroupIds
        KeyName: !Ref KeyName
        ImageId: !Ref ImageId

Nota: se passi due sottoreti, come ["subnet-aaaa, subnet-bbbb"], l'output di Fn::Join è {"subnet-aaaa, subnet-bbbb"}.

  1. Nel file JSON o YAML del tuo stack annidato, imposta il tipo di SecurityGroupIds su CommadeLimitedList.

Esempio di file JSON annidato:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "SubnetId": {
      "Type": "String"
    },
    "SecurityGroupIds": {
      "Type": "CommaDelimitedList"
    },
    "KeyName": {
      "Type": "String"
    },
    "ImageId": {
      "Type": "String"
    }
  },
  "Resources": {
    "Ec2instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": {
          "Ref": "ImageId"
        },
        "KeyName": {
          "Ref": "KeyName"
        },
        "SecurityGroupIds": {
          "Ref": "SecurityGroupIds"
        },
        "SubnetId": {
          "Ref": "SubnetId"
        }
      }
    }
  }
}

Esempio di file YAML annidato:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  SubnetId:
    Type: String
  SecurityGroupIds:
    Type: CommaDelimitedList
  KeyName:
    Type: String
  ImageId:
    Type: String
Resources:
  Ec2instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      ImageId: !Ref ImageId
      KeyName: !Ref KeyName
      SecurityGroupIds: !Ref SecurityGroupIds
      SubnetId: !Ref SubnetId

Nota: nello stack annidato, la stringa combinata dello stack principale viene passata a SecurityGroupIds come CommadeLimitedList. Ad esempio, il valore {"sg-aaaaa, sg-bbbbb"} viene riconvertito in ["sg-aaaaa", "sg-bbbbb"]. Pertanto, SecurityGroupIds deve essere referenziato direttamente da SecurityGroupIds: !Rif. SecurityGroupIds e non come elenco di stringhe.


Informazioni correlate

Utilizzo di stack annidati

AWS::CloudFormation::Stack

AWS UFFICIALE
AWS UFFICIALEAggiornata 2 anni fa