I am defining a cloudformation stack where the security group should allow ingress traffic from specified IP addresses. I have defined these IP addresses as mapping and they will grow in future when we onboard new customers on our platform. My current cloudformation stack looks like
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
This does not allow the SG to be created no matter I separate them by ' ', ',' or ';'.
2nd method I wanted to try was to define these mappings as a list and iterate them dynamically depending on number of elements configured. For PROD
and NON-PROD
the list will have different number of IP addresses, so I won't be able to define indexes. E.g. Production will have 4 IP addresses and Non-Prod might have only 2 IP addresses. If I define indexes for !Select, the same CFN template will not work for both the environments.
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
Is there a way to get around this problem?