Search code examples
amazon-web-servicesaws-cloudformationamazon-elb

Cloudformation application load balancer elastic IP error


I am trying to automate a stack consisting of one Fargate cluster, multiple services and one application load balancer with Cloudformation.

Unfortunately the creation of the LoadBalancer fails with the following error message: "Elastic IPs are not supported for load balancers with type 'application'"

I know that elastic IPs are not supported however I cannot figure out why Cloudformation tries to assign an elastic IP to my loadbalancer. I found no hints in the reference about some value defaulting to elastic IP assignment.

"Resources": {
    "Cluster": {
      "Type": "AWS::ECS::Cluster",
      "Properties": {}
    },
    "Service": {
      "Type": "AWS::ECS::Service",
      "Properties": {
        "Cluster": {
          "Ref": "Cluster"
        },
        "TaskDefinition": {
          "Ref": "Task"
        },
        "LoadBalancers": [
          {
            "ContainerName": "service1",
            "ContainerPort": 80,
            "LoadBalancerName": {
              "Ref": "LoadBalancer"
            },
            "TargetGroupArn": {
              "Ref": "TargetGroup"
            }
          }
        ],
        "NetworkConfiguration": {
          "AwsvpcConfiguration": {
            "AssignPublicIp": "false",
            "Subnets": [
              {
                "Ref": "Subnet1"
              },
              {
                "Ref": "Subnet2"
              }
            ]
          }
        }
      }
    },
    "Task": {
      "Type": "AWS::ECS::TaskDefinition",
      "Properties": {
        "ContainerDefinitions": [
          {
            "PortMappings": [
              {
                "HostPort": 80,
                "Protocol": "tcp",
                "ContainerPort": 80
              }
            ],
            "Environment": [
              {
                "Name": "SERVER_PORT",
                "Value": "80"
              }
            ],
            "Image": "arn",
            "Essential": true,
            "Name": "service1",
            "Memory": 2048
          }
        ],
        "TaskRoleArn": "arn",
        "NetworkMode": "awsvpc"
      }
    },

    "LoadBalancer": {
      "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
      "Properties": {
        "SubnetMappings": [
          {
            "SubnetId": {
              "Ref": "Subnet1"
            },
            "AllocationId": "subnet-1"
          },
          {
            "SubnetId": {
              "Ref": "Subnet2"
            },
            "AllocationId": "subnet-2"
          }
        ],
        "SecurityGroups": [
          {
            "Ref": "VPCSecurityGroup"
          }
        ]
      }
    },
    "VPC": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": "10.0.0.0/16"
      }
    },
    "VPCSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "GroupDescription": "security group"
      }
    },
    "Subnet1": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "CidrBlock": "10.0.0.0/24",
        "MapPublicIpOnLaunch": false
      }
    },
    "Subnet2": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "CidrBlock": "10.0.1.0/24",
        "MapPublicIpOnLaunch": false
      }
    },
    "Listener": {
      "Type": "AWS::ElasticLoadBalancingV2::Listener",
      "Properties": {
        "LoadBalancerArn": {
          "Ref": "LoadBalancer"
        },
        "DefaultActions": [
          {
            "Type": "FORWARD"
          }
        ],
        "Port": 443,
        "Protocol": "HTTPS",
        "Certificates": [
          {
            "CertificateArn": "arn"
          }
        ]
      }
    },
    "TargetGroup": {
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "VpcId": {
          "Ref": "VPC"
        },
        "Port": 80,
        "Protocol": "HTTP"
      }
    },
    "ListenerRule": {
      "Type": "AWS::ElasticLoadBalancingV2::ListenerRule",
      "Properties": {
        "Actions": [
          {
            "Type": "FORWARD"
          }
        ],
        "Priority": 1,
        "Conditions": [],
        "ListenerArn": {
          "Ref": "Listener"
        }
      }
    }


Solution

  • I fixed the elastic IP error by removing the SubnetMappings property and declaring the Subnets property instead.

      "LoadBalancer": {
                "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
                "Properties": {
                    "Subnets": [
                        {
    
                          "Ref": "PublicSubnet1"
    
                        },
                        {
    
                          "Ref": "PublicSubnet2"
    
                        }
                    ],
                    "SecurityGroups": [
                        {
                            "Ref": "VPCSecurityGroup"
                        }
                    ]
                }
        }