未使用Terraform创建的ecs实例

时间:2018-07-11 15:49:40

标签: terraform amazon-ecs terraform-provider-aws

因此,我试图通过terraform将公共托管的Docker容器启动到我的ECS中。计划已完成,没有任何错误,并启动了服务,但ECS实例为空,并且服务仅抱怨没有将ECS实例分配给群集。

作为我的基础架构的一部分,我已经定义:

  • VPC
  • 安全组
  • 主要角色
  • ecs群集
  • 带有ALB的ECS服务

鉴于其他所有内容都可以启动,并且我可以ssh到ecs(甚至手动拉并运行docker映像),我很确定所有联网都可以,这使我认为这可能是ECS服务与ALB?

ECS服务:

locals {
  application_name = "${var.environment}-kafka"
}

resource "aws_ecs_service" "kafka" {
  name                                = "${local.application_name}"
  iam_role                            = "${var.iam-role_arn}"
  cluster                             = "${var.ecs-cluster_id}"
  task_definition                     = "${aws_ecs_task_definition.kafka_definition.arn}"
  desired_count                       = "${var.count_kafka}"
  deployment_minimum_healthy_percent  = "${var.deployment_min_healthy_percent}"
  deployment_maximum_percent          = "${var.deployment_max_percent}"
  depends_on  = ["aws_alb_listener.kafka-alb-listener"]

  load_balancer {
    target_group_arn  = "${aws_alb_target_group.kafka-alb-target-group.arn}"
    container_port    = 80
    container_name    = "kafka"
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_ecs_task_definition" "kafka_definition" {
  family                = "${var.environment}_kafka"
  container_definitions = "${data.template_file.kafka_task.rendered}"

  lifecycle {
    create_before_destroy = true
  }
}

data "template_file" "kafka_task" {
  template= "${file("${path.module}/kafka_task_definition.tpl")}"

  vars {
    kafka_docker_image = "${var.kafka-docker-image_name}:${var.kafka-docker-image_tag}"
  }
}

ALB:

resource "aws_alb" "kakfa-alb" {
  name                = "${local.application_name}-alb"
  security_groups     = ["${var.security_groups_ids}"]
  subnets             = ["${var.public_subnet_ids}"]

  tags {
    Name        = "${local.application_name}-alb"
    Environment = "${var.environment}"
  }
}

resource "aws_alb_target_group" "kafka-alb-target-group" {
  name                = "${aws_alb.kakfa-alb.name}-target-group"
  port                = "5000"
  protocol            = "HTTP"
  vpc_id              = "${var.vpc_id}"
  depends_on          = ["aws_alb.kakfa-alb"]

  health_check {
    healthy_threshold   = "5"
    unhealthy_threshold = "2"
    interval            = "30"
    matcher             = "200"
    path                = "/"
    port                = "traffic-port"
    protocol            = "HTTP"
    timeout             = "5"
  }

  tags {
    Name        = "${aws_alb.kakfa-alb.name}-target-group"
    Environment = "${var.environment}"
  }
}

resource "aws_alb_listener" "kafka-alb-listener" {
  load_balancer_arn = "${aws_alb.kakfa-alb.arn}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = "${aws_alb_target_group.kafka-alb-target-group.arn}"
    type             = "forward"
  }
}

1 个答案:

答案 0 :(得分:1)

您可能尚未将实例添加到集群中。

user_data.sh:

#!/bin/bash

echo ECS_CLUSTER='${ecs_cluster_name}' > /etc/ecs/ecs.config

在您的Terraform配置中:

data "template_file" "user_data" {
  template = "${file("user_data.sh")}"

  vars {
    ecs_cluster_name = "${var.ecs_cluster_name}"
  }
}


resource "aws_launch_configuration" "current" {

  ...

  user_data = "${data.template_file.user_data.rendered}"

  ...

}