AWS CloudFormation VPC CIDR分配给安全组

问题描述

当我选择VPC网络时,如何将VPC的CIDR分配给安全组?

Parameters:
  VpcId:
    Type: 'AWS::EC2::VPC::Id'
    Description: VpcId of your existing Virtual Private Cloud (VPC)
    ConstraintDescription: must be the VPC Id of an existing Virtual Private Cloud.

  EC2SecurityGroup:
      Type: 'AWS::EC2::SecurityGroup'
      Properties:
        VpcId: !Ref VpcId
        GroupDescription: SecurityGroup
        SecurityGroupIngress:
          - IpProtocol: tcp
            FromPort: '22'
            ToPort: '22'
            CidrIp: 

感谢帮助。

解决方法

您无法基于作为参数传递的ID获得VPC CIDR范围。您要么必须传递 CIDR作为第二个参数,要么构造一个custom resource来为您完成此操作。

自定义资源将是 lambda函数,该函数将VPC ID作为输入参数。然后,使用AWS开发工具包,该函数将查询VPC,并将其CIDR返回给CloudFormation,以在您的安全组中使用。