首页 > 解决方案 > 在无服务器 yaml 配置中使用 fn::split 不起作用

问题描述

我正在使用无服务器框架在 AWS 上部署 API。我的serverless.yml文件中有以下内容:

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          fn::split:
            delimiter: ','
            value: ${env:VPC_SG_ID}

VPC_SG_ID包含以下字符串:sg-1111111111,sg-222222222,sg-3333333333

但是,在部署应用程序时,我收到以下错误: An error occurred: MyLambdaFunction - Value of property SecurityGroupIds must be of type List of String.

如果我对 SGs 列表进行硬编码,它可以正常工作:

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          - "sg-1111111111"
          - "sg-2222222222"
          - "sg-3333333333"

为什么 fn::split 函数不返回字符串列表?

编辑:

以下配置导致相同的错误

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          Fn::Split:
            - ','
            - ${env:VPC_SG_ID}

标签: amazon-web-servicesyamlamazon-cloudformationserverless-framework

解决方案


如果将安全组作为输入参数添加到模板

Parameters:
  VPCSGID:
    Type: String
    Description: Comma separated Security Groups

安全组可以拆分!Split

SecurityGroupIds: !Split [",", !Ref VPCSGID]

可以拆分Fn:Split

SecurityGroupIds: { "Fn::Split": [",", !Ref VPCSGID] }

sam deploy 的参数可以传递为

sam deploy --parameter-overrides 'ParameterKey=VPCSGID,ParameterValue=sg-011111,sg-222222'

推荐阅读