带Terraform的ECS [英] ECS with Terraform

查看:79
本文介绍了带Terraform的ECS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用Terraform管理ECS服务的良好/权威性参考或课程.我已引用创建了ECS服务,但是我无法进入任务在该群集上运行的状态.

Is there a good / definitive reference or course for managing a ECS service using Terraform. I have referred this which creates the ECS Service, but I can't get to a state where my task runs on that cluster.

这是我现在拥有的:

# create the VPC
resource "aws_vpc" "vpc" {
  cidr_block           = var.cidr_vpc
  instance_tenancy     = var.instanceTenancy
  enable_dns_support   = var.dnsSupport
  enable_dns_hostnames = var.dnsHostNames
  tags = {
    Name = "tdemo"
  }
}

# Create the Internet Gateway
resource "aws_internet_gateway" "igw" {
  vpc_id = "${aws_vpc.vpc.id}"
  tags = {
    Name = "tdemo"
  }
}

# Create the Public subnet
resource "aws_subnet" "subnet_public1" {
  vpc_id                  = "${aws_vpc.vpc.id}"
  cidr_block              = var.cidr_pubsubnet1
  map_public_ip_on_launch = "true"
  availability_zone       = var.availability_zone1
  tags = {
    Name = "tdemo"
  }
}

resource "aws_subnet" "subnet_public2" {
  vpc_id                  = "${aws_vpc.vpc.id}"
  cidr_block              = var.cidr_pubsubnet2
  map_public_ip_on_launch = "true"
  availability_zone      = var.availability_zone2
  tags = {
    Name = "tdemo"
  }
}

# Route table to connect to Internet Gateway
resource "aws_route_table" "rta_public" {
  vpc_id = "${aws_vpc.vpc.id}"
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.igw.id}"
  }
  tags = {
    Name = "tdemo"
  }
}

# Create Route Table Association to make the subet public over internet
resource "aws_route_table_association" "rta_subnet_public" {
  subnet_id      = "${aws_subnet.subnet_public1.id}"
  route_table_id = "${aws_route_table.rta_public.id}"
}

# Configure Security Group inbound and outbound rules
resource "aws_security_group" "sg_22" {
  name   = "sg_22"
  vpc_id = "${aws_vpc.vpc.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 0
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "tdemo"
  }
}

###############################################################################
resource "aws_iam_role" "ecs-service-role" {
  name               = "tdemo-ecs-service-role"
  path               = "/"
  assume_role_policy = "${data.aws_iam_policy_document.ecs-service-policy.json}"
}

resource "aws_iam_role_policy_attachment" "ecs-service-role-attachment" {
  role       = "${aws_iam_role.ecs-service-role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
}

data "aws_iam_policy_document" "ecs-service-policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ecs.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "ecs-instance-role" {
  name               = "tdemo-ecs-instance-role"
  path               = "/"
  assume_role_policy = "${data.aws_iam_policy_document.ecs-instance-policy.json}"
}

data "aws_iam_policy_document" "ecs-instance-policy" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ec2.amazonaws.com"]
    }
  }
}

resource "aws_iam_role_policy_attachment" "ecs-instance-role-attachment" {
  role       = "${aws_iam_role.ecs-instance-role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}

resource "aws_iam_instance_profile" "ecs-instance-profile" {
  name  = "tdemo-ecs-instance-profile"
  path  = "/"
  roles = ["${aws_iam_role.ecs-instance-role.id}"]
  provisioner "local-exec" {
    command = "ping 127.0.0.1 -n 11 > nul"
  }
}

resource "aws_launch_configuration" "ecs-launch-configuration" {
  name                 = "tdemo-ecs-launch-configuration"
  image_id             = var.amiid
  instance_type        = "t2.xlarge"
  iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}"

  root_block_device {
    volume_type           = "standard"
    volume_size           = 100
    delete_on_termination = true
  }

  lifecycle {
    create_before_destroy = true
  }

  security_groups             = ["${aws_security_group.sg_22.id}"]
  associate_public_ip_address = "true"
  key_name                    = "${var.ecs_public_keyname}"
  user_data                   = <<-EOF
    #! /bin/bash
    echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
    sudo sysctl -w vm.max_map_count=524288
    sudo apt-get update
    sudo apt-get install -y apache2
    sudo systemctl start apache2
    sudo systemctl enable apache2
    echo "<h1>Deployed via Terraform</h1>" | sudo tee /var/www/html/index.html
    EOF
}

resource "aws_ecs_cluster" "ecs-cluster" {
    name = var.ecs_cluster
}

###############################################################################
data "aws_ecs_task_definition" "ecs_task_definition" {
  task_definition = "${aws_ecs_task_definition.ecs_task_definition.family}"
}

resource "aws_ecs_task_definition" "ecs_task_definition" {
  family                = "hello_world"
  container_definitions = <<DEFINITION
  [
    {
      "name": "hello-world",
      "image": "nginx:latest",
      "essential": true,
      "portMappings": [
        {
          "containerPort": 80,
          "hostPort": 80
        }
      ],
      "memory": 500,
      "cpu": 10
    }
  ]
  DEFINITION
}

resource "aws_alb" "ecs-load-balancer" {
    name                = "ecs-load-balancer"
    security_groups     = ["${aws_security_group.sg_22.id}"]
    subnets             = ["${aws_subnet.subnet_public1.id}", "${aws_subnet.subnet_public2.id}"]

    tags = {
      Name = "ecs-load-balancer"
    }
}

resource "aws_alb_target_group" "ecs-target-group" {
    name                = "ecs-target-group"
    port                = "80"
    protocol            = "HTTP"
    vpc_id              = "${aws_vpc.vpc.id}"

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

    tags = {
      Name = "ecs-target-group"
    }
}

resource "aws_alb_listener" "alb-listener" {
    load_balancer_arn = "${aws_alb.ecs-load-balancer.arn}"
    port              = "80"
    protocol          = "HTTP"

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

resource "aws_autoscaling_group" "ecs-autoscaling-group" {
    name                        = "ecs-autoscaling-group"
    max_size                    = "${var.max_instance_size}"
    min_size                    = "${var.min_instance_size}"
    desired_capacity            = "${var.desired_capacity}"
    vpc_zone_identifier         = ["${aws_subnet.subnet_public1.id}", "${aws_subnet.subnet_public2.id}"]
    launch_configuration        = "${aws_launch_configuration.ecs-launch-configuration.name}"
    health_check_type           = "ELB"
  }

resource "aws_ecs_service" "ecs-service" {
    name            = "tdemo-ecs-service"
    iam_role        = "${aws_iam_role.ecs-service-role.name}"
    cluster         = "${aws_ecs_cluster.ecs-cluster.id}"
    task_definition = "${aws_ecs_task_definition.ecs_task_definition.family}:${max("${aws_ecs_task_definition.ecs_task_definition.revision}", "${data.aws_ecs_task_definition.ecs_task_definition.revision}")}"
    desired_count   = 1

    load_balancer {
        target_group_arn  = "${aws_alb_target_group.ecs-target-group.arn}"
        container_port    = 80
        container_name    = "hello-world"
    }
}

谢谢

推荐答案

显而易见的一件事可能是问题的根源(至少其中之一)是:

One thing that is apparent and that may be the source of the issue (at least one of them) is:

echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

但是,您的群集名称是var.ecs_cluster.因此,上一行应为:

However, your cluster name is var.ecs_cluster. Thus the above line should be:

echo ECS_CLUSTER=${var.ecs_cluster} >> /etc/ecs/ecs.config

请注意,可能还有很多其他问题,如果不实际部署您的Terraform脚本,这些问题并不清楚.

Please note, that there could be many other issues, which are not that clear to spot without actually deploying your terraform script.

这篇关于带Terraform的ECS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆