【问题标题】:Cloudformation Security Group configuration using listCloudformation 安全组配置使用列表
【发布时间】:2019-10-19 02:42:46
【问题描述】:

我正在定义一个 cloudformation 堆栈,其中安全组应允许来自指定 IP 地址的入口流量。我已将这些 IP 地址定义为映射,当我们在我们的平台上加入新客户时,它们将在未来增长。我当前的 cloudformation 堆栈看起来像

AWSTemplateFormatVersion: '2010-09-09'  
Description: Security group.

Parameters:
  VPCStackName:
    Type: String
    Description: The name of VPC stack

Mappings:
  # Security group configuration for different environments
  SecurityGroupConfiguration:
    PROD: 
      IPAddress: "149.250.241.202/32 149.250.241.202/32"
    NON-PROD: 
      IPAddress: "149.250.241.202/32, 149.250.241.204/32, 149.250.241.205/32"

Resources:

  # Add security groups and their ingress
  PublicSubnetSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Test security group
      VpcId: 
        Fn::ImportValue:
          !Sub "${VPCStackName}-vpcid"
      SecurityGroupIngress:
        - CidrIp: !FindInMap ['SecurityGroupConfiguration', 'PROD', 'IPAddress']
          IpProtocol: -1

这不允许创建SG,无论我用'',','或';'分隔它们。

我想尝试的第二种方法是将这些映射定义为一个列表,并根据配置的元素数量动态迭代它们。对于PRODNON-PROD,列表将有不同数量的IP 地址,因此我将无法定义索引。例如。生产将有 4 个 IP 地址,而非生产可能只有 2 个 IP 地址。如果我为 !Select 定义索引,相同的 CFN 模板将不适用于两种环境。

AWSTemplateFormatVersion: '2010-09-09'  
Description: Security group.

Parameters:
  VPCStackName:
    Type: String
    Description: The name of VPC stack

Mappings:
  # Security group configuration for different environments

  SecurityGroupConfiguration:
  PROD: 
    IPAddress: 
      - 149.250.241.202/32
      - 149.250.241.203/32
  NON-PROD: 
    IPAddress: 
      - 149.250.241.202/32
      - 149.250.241.204/32
      - 149.250.241.205/32

Resources:

  # Add security groups and their ingress
  PublicSubnetSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Test security group
      VpcId: 
        Fn::ImportValue:
          !Sub "${VPCStackName}-vpcid"
      SecurityGroupIngress:
        - CidrIp: for (i in SecurityGroupConfiguration)
            <Dynamically iterate over list to produce all the ip addresses>
            !Select [i, !FindInMap ['SecurityGroupConfiguration', 'PROD', 'IPAddress']]
          IpProtocol: -1

有没有办法解决这个问题?

【问题讨论】:

  • 您是否考虑过使用模板库来生成您的 CloudFormation 模板?
  • 什么样的模板库@nicholas.hauschild?我也可以从 jenkins 或任何其他 CI/CD 工具执行它们吗?
  • 对于 Python,这是我刚刚快速搜索的结果:Mako makotemplates.org
  • @nicholas.hauschild CFN 本身有没有其他方法可以实现这一目标?模板增加了额外的开销。不过感谢您的链接。

标签: amazon-web-services amazon-cloudformation aws-security-group


【解决方案1】:

简短回答:前缀列表
一个完全避免您的问题的可能解决方案是创建一个包含所有 IP 的前缀列表(并且可以在以后修改),而不是在您的 SG 中引用该前缀列表。 对您的前缀列表的未来更新会立即生效无需对您的实时 SG 进行任何修改。
详情请见Prefix lists

注意:如果您为大量 IP 设置此设置,请记住您的 VPC 限制(请参阅Amazon VPC quotas

【讨论】:

    【解决方案2】:

    AWS cloudformation's Custom resources 使您能够在模板中编写自定义预置逻辑,AWS CloudFormation 在您创建、更新(如果您更改了自定义资源)或删除堆栈时运行这些逻辑。

    您可以使用AWS Lambda-backed Custom Resources。当您将 Lambda 函数与自定义资源相关联时,只要创建、更新或删除自定义资源,就会调用该函数。 AWS CloudFormation 调用 Lambda API 来调用函数并将所有请求数据(例如请求类型和资源属性)传递给函数。

    Lambda 函数的强大功能和可定制性与 AWS CloudFormation 相结合,您可以以自定义方式更新安全组。

    有一些开源项目可以帮助你快速编写它

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2017-08-15
      • 2018-02-11
      • 2021-01-14
      • 2018-03-03
      • 2016-04-18
      • 2016-10-31
      • 2021-06-16
      • 2017-05-11
      相关资源
      最近更新 更多