1
votes

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?

1

1 Answers

4
votes

If you know which subnets in the list are private you can manually hand pick them. For example:

Subnets:
  - !Select [0, !Ref SubnetID]
  - !Select [1, !Ref SubnetID]

For fully autonomous solution when you don't know which subnets are private, which not, you would have to develop a custom resource lambda function which would return the list of subnets of interest into your CFN stack.