Aws ecs fargate 可用性区域如何工作?

How Aws ecs fargate availablity zone works?

主要的两个问题与 terraform 代码。

  1. Alb for Ecs fargate 用于路由到另一个可用区域?或路由到容器
  2. 如果我根据可用区编号(us-east-2a、2b、2c,因此编号为 3 并创建 3 个子网)创建子网并使用 alb 将其映射到 ecs 集群,可用区是否申请?

我正在尝试像下图一样构建基础设施

resource "aws_vpc" "cluster_vpc" {
  tags = {
    Name = "ecs-vpc"
  }
  cidr_block = "10.30.0.0/16"
}

data "aws_availability_zones" "available" {

}

resource "aws_subnet" "cluster" {
  vpc_id            = aws_vpc.cluster_vpc.id
  count             = length(data.aws_availability_zones.available.names)
  cidr_block        = "10.30.${10 + count.index}.0/24"
  availability_zone = data.aws_availability_zones.available.names[count.index]
  tags = {
    Name = "ecs-subnet"
  }
}


resource "aws_internet_gateway" "cluster_igw" {
  vpc_id = aws_vpc.cluster_vpc.id

  tags = {
    Name = "ecs-igw"
  }
}

resource "aws_route_table" "public_route" {
  vpc_id = aws_vpc.cluster_vpc.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.cluster_igw.id
  }

  tags = {
    Name = "ecs-route-table"
  }
}

resource "aws_route_table_association" "to-public" {
  count          = length(aws_subnet.cluster)
  subnet_id      = aws_subnet.cluster[count.index].id
  route_table_id = aws_route_table.public_route.id
}
resource "aws_ecs_cluster" "staging" {
  name = "service-ecs-cluster"
}

resource "aws_ecs_service" "staging" {
  name            = "staging"
  cluster         = aws_ecs_cluster.staging.id
  task_definition = aws_ecs_task_definition.service.arn
  desired_count   = 1
  launch_type     = "FARGATE"

  network_configuration {
    security_groups  = [aws_security_group.ecs_tasks.id]
    subnets          = aws_subnet.cluster[*].id
    assign_public_ip = true
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.staging.arn
    container_name   = var.app_name
    container_port   = var.container_port
  }

resource "aws_lb" "staging" {
  name               = "alb"
  subnets            = aws_subnet.cluster[*].id
  load_balancer_type = "application"
  security_groups    = [aws_security_group.lb.id]

  access_logs {
    bucket  = aws_s3_bucket.log_storage.id
    prefix  = "frontend-alb"
    enabled = true
  }

  tags = {
    Environment = "staging"
    Application = var.app_name
  }
}

... omit like lb_target, or specific components

Alb for Ecs fargate is for routing to another avaliablity zones? or routing to containers

不是真的。它是为您的 ECS 服务提供一个固定的端点 (url)。 ALB 将自动在您的 ECS 服务之间分配来自 Internet 的传入连接。它们可以在一个或多个 AZ 中。在您的情况下,它是 只有 1 个 AZ,因为您使用的是 desired_count = 1。这意味着您在单个 AZ 中将只有 1 个 ECS 服务。

If I create a subnet based on the availability zone number (us-east-2a, 2b, 2c, so number is 3 and create 3 subnets) and map it to an ecs cluster with alb, does the availability zone apply?

是的,因为您的 ALB 已通过 aws_subnet.cluster[*].id 为与 ECS 服务相同的子网启用。但是正如第一个问题中所解释的,您在一个 AZ 中将只有一项服务。

my intent is to build infra which has three availability zone and also deploy aws fargate on three availablity zone.

如前所述,您的 desired_count = 1 因此您将不会拥有跨 3 个可用区的 ECS 服务。

此外,您只创建了 public 个子网,而您的示意图显示 ECS 服务应该是私有的。