hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.87k stars 9.21k forks source link

Feature: attach ALB to ECS Fargate resource #13368

Open janaka opened 4 years ago

janaka commented 4 years ago

Community Note

Description

Enable attached an ALB / target group to an already created ECS Fargate service

New or Affected Resource(s)

Potential Terraform Configuration

References

https://discuss.hashicorp.com/t/modularising-ecs-service-creation-with-optional-alb/8740

ljluestc commented 1 year ago
# Define the ECS task definition (if not already defined)
resource "aws_ecs_task_definition" "example" {
  family                   = "my-ecs-service"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  execution_role_arn        = aws_iam_role.ecs_execution_role.arn

  # Define container definitions, network configurations, etc.
  # ...
}

# Define the ECS service (if not already defined)
resource "aws_ecs_service" "example" {
  name            = "my-ecs-service"
  cluster         = aws_ecs_cluster.example.id
  task_definition = aws_ecs_task_definition.example.arn
  launch_type     = "FARGATE"

  # Define desired count, IAM roles, security groups, etc.
  # ...
}

# Define the ALB target group
resource "aws_lb_target_group" "example" {
  name        = "my-target-group"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = aws_vpc.example.id

  # Additional target group settings (health checks, stickiness, etc.)
  # ...
}

# Attach the ALB target group to the ECS service
resource "aws_lb_target_group_attachment" "example" {
  target_group_arn  = aws_lb_target_group.example.arn
  target_id         = aws_ecs_service.example.name
  port              = 80
}

# Output the ALB DNS name (for reference)
output "alb_dns_name" {
  value = aws_lb.example.dns_name
}
charltonstanley commented 10 months ago
# Define the ECS task definition (if not already defined)
resource "aws_ecs_task_definition" "example" {
  family                   = "my-ecs-service"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  execution_role_arn        = aws_iam_role.ecs_execution_role.arn

  # Define container definitions, network configurations, etc.
  # ...
}

# Define the ECS service (if not already defined)
resource "aws_ecs_service" "example" {
  name            = "my-ecs-service"
  cluster         = aws_ecs_cluster.example.id
  task_definition = aws_ecs_task_definition.example.arn
  launch_type     = "FARGATE"

  # Define desired count, IAM roles, security groups, etc.
  # ...
}

# Define the ALB target group
resource "aws_lb_target_group" "example" {
  name        = "my-target-group"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = aws_vpc.example.id

  # Additional target group settings (health checks, stickiness, etc.)
  # ...
}

# Attach the ALB target group to the ECS service
resource "aws_lb_target_group_attachment" "example" {
  target_group_arn  = aws_lb_target_group.example.arn
  target_id         = aws_ecs_service.example.name
  port              = 80
}

# Output the ALB DNS name (for reference)
output "alb_dns_name" {
  value = aws_lb.example.dns_name
}

This doesn't work. Error:

│ Error: registering targets with target group: ValidationError: Instance ID 'my-ecs-service' is not valid
│       status code: 400, request id: bfxxxx54-xxxx-xxxx-xxxx-2fxxxxxxxxxxxx
│ 
│   with module.alb_target_group.aws_lb_target_group_attachment.main,
│   on ../../modules/alb_target_group/main.tf line 27, in resource "aws_lb_target_group_attachment" "main":
│   27: resource "aws_lb_target_group_attachment" "main" {
│ 

Fargate containers don't seem to have the Container instance ID field populated.

image

vegarab commented 1 week ago

Much needed addition!