You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CloudFormation中拼接列表与ImportValue值?

问题分析与解决方法

你的核心问题是ECS Service的SecurityGroups字段需要接收字符串列表,但你用!Join生成了逗号分隔的单个字符串,格式不匹配,同时参数文件存在格式错误。以下是具体修正方案:

1. 修正CloudFormation模板配置

不需要手动拆分原列表再添加新值,直接用以下两种方法合并原有安全组列表和导入的单个安全组:

方案一:用!Split + !Join组合(兼容旧版CloudFormation)

Parameters:
  SecurityGroupIds:
    Type: List<AWS::EC2::SecurityGroup::Id>
Resources:
  Service:
    Type: AWS::ECS::Service
    Properties:
      NetworkConfiguration:
        AwsvpcConfiguration:
          SecurityGroups: !Split [",", !Join [",", [!Ref SecurityGroupIds, !ImportValue !Sub "${param1}-${param2}-efs-SecurityGroupId"]]]

原理:!Join会将原列表参数(自动转为逗号分隔字符串)和导入的单个安全组拼接成完整的逗号分隔字符串,再通过!Split拆回列表,完全匹配SecurityGroups的格式要求。

方案二:用!Flatten合并列表(更简洁)

Parameters:
  SecurityGroupIds:
    Type: List<AWS::EC2::SecurityGroup::Id>
Resources:
  Service:
    Type: AWS::ECS::Service
    Properties:
      NetworkConfiguration:
        AwsvpcConfiguration:
          SecurityGroups: !Flatten
            - !Ref SecurityGroupIds
            - - !ImportValue !Sub "${param1}-${param2}-efs-SecurityGroupId"

原理:!Ref SecurityGroupIds返回原生列表,第二个元素是包含单个安全组的嵌套列表,!Flatten会将两者合并为单层列表。

2. 修正参数文件格式

你提供的参数文件多了一层冗余大括号,正确格式二选一:

格式一(适合aws cloudformation deploy命令)

[
  {
    "ParameterKey": "SecurityGroupIds",
    "ParameterValue": "sg-065f0903f9c1eeaa2,sg-09f820ab09fa69c5f"
  }
]

格式二(适合aws cloudformation create-stack命令)

{
  "Parameters": {
    "SecurityGroupIds": "sg-065f0903f9c1eeaa2,sg-09f820ab09fa69c5f"
  }
}

内容的提问来源于stack exchange,提问作者Charlie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:05:10