magnificent-pillow-80370
04/17/2023, 4:34 PMdef core_network_vpc_attachment_data_per_segment(core_network_segments, region):
"""
Creates a dictionary of segments per vpc per subnets. these are consumed in the vpcattachment to core network
"""
# Instantiate an ec2 client
ec2_client = create_aws_client(service_name="ec2", region=region)
# Parse the segment list
segment_list = []
vpc_corenetwork_attachment_data = {}
for segment_data in core_network_segments:
segment_list.append(segment_data["name"])
# Parse VPCs by the segment
for segment in segment_list:
subnets_per_vpc_per_segment_list = []
vpcs_by_tag = describe_vpcs_by_filter(
ec2_client=ec2_client,
filters=[{
"Name": "tag:core_network_segment",
"Values": [
segment
]
}
]
)
if vpcs_by_tag["Vpcs"]:
# Parse private subnets of a VPC
for vpc in vpcs_by_tag["Vpcs"]:
vpc_and_subnets_data = {}
# vpc id
vpc_id = vpc['VpcId']
# calculating the VPC ARN
vpc_arn = f"arn:aws:ec2:{region}:{vpc['OwnerId']}:vpc/{vpc_id}"
# finding vpc name based on the Name tag
for vpc_tags in vpc["Tags"]:
if vpc_tags["Key"] == "Name":
vpc_name = vpc_tags["Value"]
# Looking for all the private subnets in a vpc. Core Network attachment will be created on these private
# subnets till we get Core subnets in all the VPCs
_subnets = describe_subnet_by_filter(ec2_client=ec2_client, filters=[
{
"Name": "vpc-id",
"Values": [vpc["VpcId"]]
},
{
"Name": "tag:Name",
"Values": ["*private*"]
}
])
# Parsing subnet ARNs for a given VPC
subnets_per_vpc_list = []
for _subnet in _subnets["Subnets"]:
subnets_per_vpc_list.append(_subnet["SubnetArn"])
vpc_and_subnets_data["vpc_name"] = vpc_name
vpc_and_subnets_data["vpc_arn"] = vpc_arn
vpc_and_subnets_data["vpc_id"] = vpc_id
vpc_and_subnets_data["subnet_arn"] = subnets_per_vpc_list
subnets_per_vpc_per_segment_list.append(vpc_and_subnets_data)
if subnets_per_vpc_per_segment_list:
vpc_corenetwork_attachment_data.update({segment: subnets_per_vpc_per_segment_list})
return vpc_corenetwork_attachment_data
which will be used in https://www.pulumi.com/registry/packages/aws/api-docs/networkmanager/vpcattachment/
any help is appreciated.. I started with CDK for Terraform but gave up and now looking at Pulumi.