1
votes

We use Terraform to provision an AWS Autoscaling Group and ECS Cluster. We decided to add a capacity provider to the ECS cluster and allow that to manage our scaling, as per https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-auto-scaling.html

But when we added the capacity provider resource and connected it to the cluster via the capacity_providers attribute on the cluster resource, terraform plan started complaining about a "Cylce". I'm not sure how else to configure this - it seems like every "connection" between the resources is setup correctly, and yet it seems terraform cannot provision this:

resource "aws_ecs_cluster" "cluster" {
  name               = "${var.environment}_cluster"
  capacity_providers = [aws_ecs_capacity_provider.capacity_provider.name]
}

resource "aws_ecs_capacity_provider" "capacity_provider" {
  name = "${var.environment}_capacity_provider"

  auto_scaling_group_provider {
    auto_scaling_group_arn = aws_autoscaling_group.autoscaling_group.arn
    managed_termination_protection = "ENABLED"

    managed_scaling {
      maximum_scaling_step_size = 1
      minimum_scaling_step_size = 1
      status                    = "ENABLED"
      target_capacity           = 100
    }
  }
}

resource "aws_launch_configuration" "launch_configuration" {
  name_prefix                 = "${var.environment}_launch_configuration-"
  image_id                    = data.aws_ami.ecs_os.id
  instance_type               = var.cluster_instance_type
  iam_instance_profile        = var.ecs_instance_profile
  security_groups             = var.security_groups["ecs_tasks"]
  associate_public_ip_address = true
  key_name                    = aws_key_pair.ssh_access.key_name
  user_data                   = data.template_file.launch_user_data.rendered

  lifecycle {
    create_before_destroy = true
  }
}

data "template_file" "launch_user_data" {
  template = "${file("${path.module}/taskdefs/user_data")}"

  vars = {
    ecs_cluster = aws_ecs_cluster.cluster.name
    efs_id      = var.efs_id
    aws_region  = data.aws_region.current.name
  }
}

resource "aws_autoscaling_group" "autoscaling_group" {
  name                  = "${var.environment}_asg"
  max_size              = 4
  min_size              = 1
  vpc_zone_identifier   = [var.public_subnet_id]
  launch_configuration  = aws_launch_configuration.launch_configuration.name
  health_check_type     = "EC2"
  protect_from_scale_in = true
}

terraform plan output:

Error: Cycle: module.compute.aws_ecs_cluster.cluster, module.compute.data.template_file.launch_user_data, module.compute.aws_launch_configuration.launch_configuration, module.compute.aws_autoscaling_group.autoscaling_group, module.compute.aws_ecs_capacity_provider.capacity_provider

Edit: to be clear: I understand that there is in fact a cycle. My question is how is it possible to have terraform provision this without having a cycle? I can't see how that is possible.

2

2 Answers

0
votes

At first, you can define the name of the cluster like this.

locals {
  cluster_name = "${var.environment}_cluster"
}              

And then

resource "aws_ecs_cluster" "cluster" {
  name               = local.cluster_name
  capacity_providers = [aws_ecs_capacity_provider.capacity_provider.name]
}

data "template_file" "launch_user_data" {
  template = "${file("${path.module}/taskdefs/user_data")}"

  vars = {
    ecs_cluster = local.cluster_name
    efs_id      = var.efs_id
    aws_region  = data.aws_region.current.name
  }
}

This will avoid the cycle.

I hope this will help.

0
votes

I've noticed this before too but ended up not using capacity providers.

If you go into the AWS ECS web console, it appears the only place to create a capacity provider manually is INSIDE AN EXISTING CLUSTER. It doesn't seem to be a separate resource inside AWS but a sub-resource of a cluster. This is the real chicken-and-egg problem I think.

I think there would need to be a way to have terraform create the cluster (without the capacity provider), THEN create the capacity provider, and then finally assign it to the cluster. Smells like a bug report.