我正在定义一个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。
我想尝试的第二种方法是将这些映射定义为列表,并根据配置的元素数动态地对其进行迭代。对于PROD
和NON-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
有没有办法解决这个问题?
答案 0 :(得分:0)
AWS cloudformation's Custom resources使您可以在创建,更新(如果更改了自定义资源)或删除堆栈时在AWS CloudFormation运行的模板中编写自定义配置逻辑。
您可以使用AWS Lambda-backed Custom Resources。将Lambda函数与自定义资源关联时,每当创建,更新或删除自定义资源时,都会调用该函数。 AWS CloudFormation调用Lambda API来调用该函数并将所有请求数据(例如请求类型和资源属性)传递给该函数。
Lambda函数的强大功能和可定制性与AWS CloudFormation结合使用,您可以通过自定义方式更新安全组。
有一些开源项目可以帮助您快速编写