Cloudformation应用程序负载均衡器弹性IP错误

Cloudformation application load balancer elastic IP error

我正在尝试使用 Cloudformation 自动化一个堆栈,该堆栈由一个 Fargate 集群、多个服务和一个应用程序负载均衡器组成。

不幸的是,LoadBalancer 的创建失败并显示以下错误消息:"Elastic IPs are not supported for load balancers with type 'application'"

我知道不支持弹性 IP,但我不明白为什么 Cloudformation 会尝试为我的负载均衡器分配弹性 IP。我在参考中没有发现关于某些值默认为弹性 IP 分配的提示。

"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"
        }
      }
    }

我通过删除 SubnetMappings 属性 并声明子网 属性 来修复弹性 IP 错误。

  "LoadBalancer": {
            "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
            "Properties": {
                "Subnets": [
                    {

                      "Ref": "PublicSubnet1"

                    },
                    {

                      "Ref": "PublicSubnet2"

                    }
                ],
                "SecurityGroups": [
                    {
                        "Ref": "VPCSecurityGroup"
                    }
                ]
            }
    }