I'm trying to create security groups in pulumi with inline rules, specifically rules that reference another security group as a source. None of these group exist yet and need to be created in a loop from a variable as there may be quite a lot of them.
Here's a example of what the variables looks like:
compute:security_groups:
- name: load-balancer
inbound:
- cidr_blocks: null
from_port: 80
protocol: tcp
source_security_group: cache
to_port: 80
- cidr_blocks: null
from_port: 22
protocol: tcp
source_security_group: logging
to_port: 22
- name: cache
inbound:
- cidr_blocks: null
from_port: 6379
protocol: tcp
source_security_group: load-balancer
to_port: 6379
- name: logging
inbound:
- cidr_blocks: null
from_port: 22
protocol: tcp
source_security_group: cache
to_port: 22
- cidr_blocks: null
from_port: 5044
protocol: tcp
source_security_group: load-balancer
to_port: 5044
We should create a security group called name
and include an inline rule for each item under inbound
. The source security group needs to to point back to the group referenced in source_security_group
The closest I've gotten is to create the rules first with as a SecurityGroupIngressArgs
then add them to a seperatly created group. Something like:
env_name = pulumi.get_stack()
default_tags = {'resource-group': env_name, 'environment': env_name, 'managed-by': "pulumi"}
### Securty groups
# Create groups
sg_list = []
for sg in config.require_object('security_groups'):
# create rules
inbound_list = []
for item in sg['inbound']:
source_group_name = env_name + '-' + item['source_security_group']
inbound_list.append(aws.ec2.SecurityGroupIngressArgs(
from_port = item['from_port'],
to_port = item['to_port'],
protocol = item['protocol'],
security_groups = sg_list[source_group_name.id]
))
# create groups
sg_name = env_name + '-' + sg.get('name')
sg_list.append(aws.ec2.SecurityGroup(sg_name,
description = sg.get('description'),
vpc_id = kv_vpc['id'],
ingress = inbound_list,
egress = outbound_list,
tags = {**default_tags,**{'Name': sg_name}}
)
)
This approach work fine for cidr blocks but with security groups as the source I can't reference security group id's that have not been created yet. I'm not sure how to structure this with pulumi.