如何在Terraform中将ECS服务连接到AWS自动扩展组

huangapple go评论75阅读模式
英文:

How is an ECS service in Terraform connected to an AWS autoscaling group

问题

The ECS service definition does not directly reference the autoscaling group. Instead, it uses the Elastic Load Balancer (ELB) target group to route traffic to the ECS tasks. The relationship is as follows:

  1. The aws_ecs_service resource you provided configures an ECS service named "tf-example-ecs-ghost." This service tells ECS how to run and maintain a specified number of tasks using the task definition you've defined.

  2. Within the load_balancer block in the aws_ecs_service resource, you specify a target_group_arn. This tells the ECS service to send traffic to the specified target group.

  3. The aws_alb_target_group resource, which is not shown in your code snippet but is likely defined elsewhere in your configuration, creates the target group that is associated with your Application Load Balancer (ALB). This target group specifies how to route incoming traffic to the registered targets, which in this case are the ECS tasks.

  4. The ALB itself is not shown in your snippet, but it is assumed to be created elsewhere. The ALB listens for incoming traffic and forwards it to the registered targets based on the rules defined in the target group.

  5. Finally, the aws_autoscaling_group resource is responsible for managing the autoscaling of the ECS instances. While the ECS service doesn't directly reference this autoscaling group, the ECS tasks running on instances launched by the autoscaling group can register with the target group when they start. This is typically achieved through the ECS service's task placement strategies and placement constraints.

In summary, the ECS service knows where to find the autoscaling group indirectly through the ALB and the target group. When ECS tasks are started on instances launched by the autoscaling group, they register with the target group, and the ALB routes traffic to them. This way, ECS and autoscaling are loosely coupled through the ALB and target group configuration.

英文:

I'm new to Terraform and ECS and I'm using this example to create an ECS EC2 type cluster that will autoscale and use an application load balancer.

My question is: How does this code snippet in main.tf

resource "aws_ecs_service" "test" {
  name            = "tf-example-ecs-ghost"
  cluster         = "${aws_ecs_cluster.main.id}"
  task_definition = "${aws_ecs_task_definition.ghost.arn}"
  desired_count   = "${var.service_desired}"   
  iam_role        = "${aws_iam_role.ecs_service.name}"

  load_balancer {
    target_group_arn = "${aws_alb_target_group.test.id}"
    container_name   = "ghost"
    container_port   = "2368"
  }

  depends_on = [
    "aws_iam_role_policy.ecs_service",
    "aws_alb_listener.front_end",
  ]
}

connected to the resource aws_autoscaling_group.app:

resource "aws_autoscaling_group" "app" {
  name                 = "tf-test-asg"
  vpc_zone_identifier  = ["${aws_subnet.main.*.id}"]
  min_size             = "${var.asg_min}"
  max_size             = "${var.asg_max}"
  desired_capacity     = "${var.asg_desired}"
  launch_configuration = "${aws_launch_configuration.app.name}"
}

How does an ECS service definition know where to find this autoscaling group as there are no interpolation variables in the aws_ecs_service resource definition pointing to the aws_autoscaling_group resource? It references a target group but target group doesn't reference an autoscaling group. That's why I'm confused as there's no apparent reference between resource "ecs-service" and resource "aws-autoscaling". Or maybe the code is missing smth? Please, provide a thorough explanation if possible.

答案1

得分: 4

不要回答我要翻译的问题。

ECS服务安排在ECS集群上,这是实例的逻辑分组,可以是EC2或Fargate(甚至不在AWS上,使用ECS Anywhere),或混合。

如果要将EC2实例加入ECS集群,需要安装ECS代理,配置它以加入正确的集群,并为实例提供与ECS交互所需的IAM权限。可以在独立的EC2实例或自动扩展组上执行此操作。

至于目标组,这是负载均衡器知道要将流量发送到哪些内容的方式。对于普通的EC2实例,您需要以某种方式将EC2实例注册到目标组中。对于ECS服务,可以配置它们将服务中的所有任务注册到目标组中。然后,当应该发送到目标组的流量到达负载均衡器时,该流量将传递到相关的ECS任务。请注意,负载均衡器可以具有多个目标组,其中配置了不同的负载均衡器监听规则,以将流量发送到不同的目标组(或执行固定响应或重定向等操作),因此负载均衡器可以支持多个ECS服务。

英文:

It doesn't.

ECS services are scheduled on an ECS cluster which is a logical grouping of instances, either EC2 or Fargate (or not even on AWS with ECS Anywhere!) or mixed.

If you want to join EC2 instances to the ECS cluster then you need to install the ECS agent, configure it to join the correct cluster and provide the necessary IAM permissions for the instance to be able to interact with ECS. You can either do this with standalone EC2 instances or an autoscaling group.

As for target groups, this is how a load balancer knows what things to send traffic to. In the case of straight EC2 instances you would register the EC2 instance with the target group in some way. With ECS services these can be configured to register all the tasks in the service with the target group. Then when traffic that should be sent to the target group reaches the load balancer that traffic is sent on to the relevant ECS task. Note that a load balancer can have multiple target groups with different load balancer listener rules configured to send traffic to different target groups (or perform fixed responses or redirects etc) so that a load balancer can support multiple ECS services.

答案2

得分: 0

这是已连接的监听器。这是它:

资源 "aws_alb_listener" "front_end" {
  load_balancer_arn = aws_alb.main.id
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = aws_alb_target_group.test.id
    type             = "forward"
  }
}

取自您提到的确切示例。

英文:

It IS connected by Listener. Here it is:

resource "aws_alb_listener" "front_end" {
  load_balancer_arn = aws_alb.main.id
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = aws_alb_target_group.test.id
    type             = "forward"
  }
}

Taken from the exact example you mentioned.

huangapple
  • 本文由 发表于 2020年1月3日 20:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定