Search code examples
amazon-web-servicesaws-cloudformationamazon-elbelastic-ip

AWS network Load Balancer CloudFormation IP


In my CloudFormation template I created elastic IP and network load balancer. There is no problem during creating:

  Subnet1a:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: VPC
    AvailabilityZone: 'eu-west-1a'
    CidrBlock: 
      Ref: 042SubnetCidr
    MapPublicIpOnLaunch: true

  LoadBalancerElasticIP:
    Type: AWS::EC2::EIP

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      IpAddressType: ipv4
      Name: network-loadbalancer
      Scheme: internet-facing
      Subnets: 
       - Ref: Subnet1a
    Type: network

Listeners and Target Groups are created separately and also are working with LB without elastic IP.

Now I'm trying to assign this elastic IP to load balancer, by chaning Subnets to SubnetMappings property, but it's gaving me error: "LoadBalancer CREATE_FAILED Property SubnetId cannot be empty."

  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      IpAddressType: ipv4
      Name: network-loadbalancer
      Scheme: internet-facing
      SubnetMappings: 
        - AllocationId: !Ref LoadBalancerElasticIP
        - SubnetId: !Ref Subnet1a
    Type: network

I've been trying different solutions for a while. Can't see what's wrong. Any ideas? Should I create network interface? And Assign eip to interface then interface to load balancer?


Solution

  • Based on the documentation of AWS::ElasticLoadBalancingV2::LoadBalancer the expected value of SubnetMappings is List of SubnetMapping but you are passing two lists. You should change it to the following:

      LoadBalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
          IpAddressType: ipv4
          Name: network-loadbalancer
          Scheme: internet-facing
          SubnetMappings: 
            - AllocationId: !Ref LoadBalancerElasticIP
              SubnetId: !Ref Subnet1a