如何为不同的 AWS 系统添加不等数量的 VPC 设置?

How to add unequal number of VPC settings to diff AWS systems?

我们在 AWS 的两个不同环境中部署了一个代码:测试系统和生产系统。

在测试系统中,我们有3个子网和1个安全组必须附加到lambda。

在 prod 系统中,我们有一个子网和两个必须附加到 lambda 的安全组。 (我知道只有一个子网不是一个好习惯)。

我想在 serverless.yml 文件本身中声明这些。我试过了

provider:
  name: aws
  runtime: python3.6
  stage: ${opt:stage, 'test'}
  lambdaHashingVersion: 20201221
  region: ${env:AWS_DEFAULT_REGION}
  vpc:
    securityGroupIds:
      - ${self:custom.securityGroupIds.${self:provider.stage}1}
      - ${self:custom.securityGroupIds.${self:provider.stage}2}
    subnetIds:
      - ${self:custom.subnetIds.${self:provider.stage}1}
      - ${self:custom.subnetIds.${self:provider.stage}2}
      - ${self:custom.subnetIds.${self:provider.stage}3}

custom:
  securityGroupIds:
    test1: sg-xxxxxxxxxxx
    test2: ''
    prod: sg-yyyyyyyyyyy
  subnetIds:
    test1: subnet-xxxxxxxxxx
    test2: subnet-yyyyyyyyyy
    test3: subnet-zzzzzzzzzz
    prod1: subnet-aaaaaaaaaa
    prod2: ''
    prod3: ''
  pythonRequirements:
    dockerizePip: non-linux

functions:
  ec-frankfurt:
    handler: lambda_function.lambda_handler
    timeout: 60
    memorySize: 512

当通过 CI/CD 部署代码时,它会抛出错误。

Resource handler returned message: "1 validation error detected: Value '[sg-xxxxxxxxxxx, ]' at 'vpcConfig.securityGroupIds' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 1024, Member must have length greater than or equal to 0, Member must satisfy regular expression pattern: ^sg-[0-9a-zA-Z]*$]

有什么方法可以为这种情况声明 VPC 设置?

我相信这种方法对你的情况可能更有效:

service: dummy

provider:
  name: aws
  region: ${env:AWS_DEFAULT_REGION}
  vpc: ${self:custom.vpcConfig.${sls:stage}}

custom:
  vpcConfig:
    test:
      securityGroupIds:
        - sg-fortesting
        - sg-fortesting2
      subnetIds:
        - subnet-fortesting2
        - subnet-fortesting
    prod:
      securityGroupIds:
        - sg-forprod
        - sg-forprod2
      subnetIds:
        - subnet-forprod2
        - subnet-forprod

感觉(至少在我看来)分离 VPC 的整个配置并根据阶段解决它更清晰。