We are trying to create a Network Load Balancer through cloudformation in the private subnet and we have 6 private subnets - 2 in each availability zone.
Currently we pass in the SubnetIDs manually by picking one subnet in each AZ as below.
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Type: network
Scheme: internal
Subnets: !Ref SubnetID
Tags:
- Key: Name
Value: !Ref EnvName
where SubnetID
is a parameter that accepts a list of strings.
Is there a way to get this subnet information automatically in cloudformation (pick private subnet ids one for each availability zone) through infrastructure as code
P.S: Passing all the 6 subnets as a list fails the load balancer creation because LB somehow picks 2 subnets in the same AZ and that is not allowed.
I am looking for a fully automated solution or any best practice to do this?