如何在 AWS CloudFormation 中將 CommaDelimitedList 參數傳遞給巢狀堆疊?

2 分的閱讀內容
0

我想在 AWS CloudFormation 中將 CommaDelimitedList 參數傳遞給巢狀堆疊。

簡短說明

您無法將 CommaDelimitedList 類型的值傳遞給巢狀堆疊。請改為在父堆疊中使用 Fn::Join 內建函數,將 CommaDelimitedList 類型轉換成 String 類型。

解決方案

下列範例說明如何將 SecurityGroupIds 清單從父堆疊傳遞給巢狀堆疊。

1.    開啟父堆疊的 JSON 或 YAML 檔案,然後將 SecurityGroupIdsType 設為 CommaDelimitedList

在 JSON 檔案的 Resources 區段,Fn::Join 函數會傳回組合字串。在 YAML 檔案的 Resources 區段,!Join 函數會傳回組合字串。在 JSON 和 YAML 檔案中,組合字串都會將 SecurityGroupIds 參數類型從 CommaDelimitedList 轉換為 String

父系 JSON 檔案的範例:

{
  "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"
          }
        }
      }
    }
  }
}

父系 YAML 檔案的範例:

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

**注意事項:**如果傳遞兩個子網路,如 ["subnet-aaaa, subnet-bbbb"],則 Fn::Join 的輸出為 {"subnet-aaaa, subnet-bbbb"}

2.    在巢狀堆疊的 JSON 或 YAML 檔案中,將 SecurityGroupIdsType 設為 CommaDelimitedList

範例巢狀 JSON 檔案:

{
  "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"
        }
      }
    }
  }
}

範例巢狀 YAML 檔案:

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

注意事項:在巢狀堆疊中,來自父堆疊的組合字串會以 CommaDelimitedList 傳遞給SecurityGroupIds。例如,值 {"sg-aaaaa, sg-bbbbb"} 會轉換回 ["sg-aaaaa", "sg-bbbbb"]。因此,SecurityGroupIds 必須由 SecurityGroupIds: !Ref SecurityGroupIds 直接參考,且不得作為字串清單。


相關資訊

使用巢狀堆疊

AWS::CloudFormation::Stack

AWS 官方
AWS 官方已更新 2 年前