部署ECS集群时出现Terraform错误

时间:2020-07-21 13:47:05

标签: amazon-web-services terraform amazon-ecs

this相关,为澄清起见,还有一个新问题

嗨,

这是我的.tf文件,它显示以下错误:

service tdemo-ecs-service无法放置任务,因为没有 容器实例满足其所有要求。原因:没有容器 在您的集群中找到了实例。有关更多信息,请参见 故障排除部分。

由于我是AWS和Terraform的新手,所以我不太了解该错误。任何帮助表示赞赏。

# Update your region
provider "aws" {
  region = "us-east-1"
}

variable "cidr_vpc" {
  default = "10.0.0.0/16"
}

variable "cidr_pubsubnet1" {
  default = "10.0.0.0/17"
}

variable "cidr_pubsubnet2" {
  default = "10.0.128.0/17"
}

variable "availability_zone1" {
  description = "availability zone to create subnet"
  default     = "us-east-1a"
}

variable "availability_zone2" {
  description = "availability zone to create subnet"
  default     = "us-east-1b"
}

variable "instanceTenancy" {
  default = "default"
}

variable "dnsSupport" {
  default = true
}

variable "dnsHostNames" {
  default = true
}

variable "amiid" {
  default = "ami-08f3d892de259504d"
}

variable "ecs_public_keyname" {
  description = "Existing Key name"
  default     = "inlinekeypair"
}

variable "ecs_cluster" {
  description = "Name of ECS Cluster"
  default = "tdemo-ecs-cluster"
}

variable "max_instance_size" {
  description = "Max instance size"
  default = 1 
}

variable "min_instance_size" {
  description = "Min instance size"
  default = 1 
}

variable "desired_capacity" {
  description = "Desired capacity"
  default = 1   
}

# 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=${var.ecs_cluster} >> /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"
    }

    depends_on = [
      // Important bit is here
      aws_alb.ecs-load-balancer
  ]
}

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

0 个答案:

没有答案