使用Terraform的ECS和应用程序负载平衡器临时端口

使用Terraform的ECS和应用程序负载平衡器临时端口,terraform,amazon-ecs,Terraform,Amazon Ecs,我尝试使用terraform构建一个前面有ALB的ECS集群。由于我使用了动态端口映射,目标将不会注册为正常。我玩了健康检查和成功代码,如果我把它设置为301,一切都很好 ECS data "template_file" "mb_task_template" { template = file("${path.module}/templates/marketplace-backend.json.tpl") vars = { name = "${var.mb_image_

我尝试使用terraform构建一个前面有ALB的ECS集群。由于我使用了动态端口映射,目标将不会注册为正常。我玩了健康检查和成功代码,如果我把它设置为301,一切都很好

ECS

data "template_file" "mb_task_template" {
  template = file("${path.module}/templates/marketplace-backend.json.tpl")
  vars = {
    name      = "${var.mb_image_name}"
    port      = "${var.mb_port}"
    image     = "${aws_ecr_repository.mb.repository_url}"
    log_group = "${aws_cloudwatch_log_group.mb.name}"
    region    = "${var.region}"
  }
}

resource "aws_ecs_cluster" "mb" {
  name = var.mb_image_name
}

resource "aws_ecs_task_definition" "mb" {
  family                = var.mb_image_name
  container_definitions = data.template_file.mb_task_template.rendered
  volume {
    name      = "mb-home"
    host_path = "/ecs/mb-home"
  }
}


resource "aws_ecs_service" "mb" {
  name            = var.mb_repository_url
  cluster         = aws_ecs_cluster.mb.id
  task_definition = aws_ecs_task_definition.mb.arn
  desired_count   = 2
  iam_role        = var.aws_iam_role_ecs
  depends_on      = [aws_autoscaling_group.mb]
  load_balancer {
    target_group_arn = var.target_group_arn
    container_name   = var.mb_image_name
    container_port   = var.mb_port
  }
}


resource "aws_autoscaling_group" "mb" {
  name                      = var.mb_image_name
  availability_zones        = ["${var.availability_zone}"]
  min_size                  = var.min_instance_size
  max_size                  = var.max_instance_size
  desired_capacity          = var.desired_instance_capacity
  health_check_type         = "EC2"
  health_check_grace_period = 300
  launch_configuration      = aws_launch_configuration.mb.name
  vpc_zone_identifier       = flatten([var.vpc_zone_identifier])
  lifecycle {
    create_before_destroy = true
  }
}

data "template_file" "user_data" {
  template = file("${path.module}/templates/user_data.tpl")
  vars = {
    ecs_cluster_name = "${var.mb_image_name}"
  }
}

resource "aws_launch_configuration" "mb" {
  name_prefix                 = var.mb_image_name
  image_id                    = var.ami
  instance_type               = var.instance_type
  security_groups             = ["${var.aws_security_group}"]
  iam_instance_profile        = var.aws_iam_instance_profile
  key_name                    = var.key_name
  associate_public_ip_address = true
  user_data                   = data.template_file.user_data.rendered
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_cloudwatch_log_group" "mb" {
  name              = var.mb_image_name
  retention_in_days = 14
}
ALB

locals {
  target_groups = ["1", "2"]
}

resource "aws_alb" "mb" {
  name               = "${var.mb_image_name}-alb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = ["${aws_security_group.mb_alb.id}"]
  subnets            = var.subnets
  tags = {
    Name = var.mb_image_name
  }
}

resource "aws_alb_target_group" "mb" {
  count       = length(local.target_groups)
  name        = "${var.mb_image_name}-tg-${element(local.target_groups, count.index)}"
  port        = var.mb_port
  protocol    = "HTTP"
  vpc_id      = var.vpc_id
  target_type = "instance"
  health_check {
    path                = "/health"
    protocol            = "HTTP"
    timeout             = "10"
    interval            = "15"
    healthy_threshold   = "3"
    unhealthy_threshold = "3"
    matcher             = "200-299"
  }
  lifecycle {
    create_before_destroy = true
  }
  tags = {
    Name = var.mb_image_name
  }
}

resource "aws_alb_listener" "mb_https" {
  load_balancer_arn = aws_alb.mb.arn
  port              = 443
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn   = module.dns.certificate_arn
  default_action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.mb.0.arn
  }
}

resource "aws_alb_listener_rule" "mb_https" {
  listener_arn = aws_alb_listener.mb_https.arn
  priority     = 100
  action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.mb.0.arn
  }
  condition {
    field  = "path-pattern"
    values = ["/health/"]
  }
}

好的。看起来上面的代码正在工作。我在网络方面有不同的问题。

这里有问题吗?如果你也能发布你的地形代码,如果你有问题的话,这可能会很有帮助。理想情况下,这应该是一种地形,这样人们可以更容易地理解它并运行您的地形。