Search code examples
amazon-web-servicesterraformterraform-provider-awsaws-fargate

Can't create a ECS FARGATE_SPOT using Terraform's ECS module


I'm learning Terraform and I'm trying to create a ECS Fargate Spot using the AWS ECS module on Terraform.

Here is the HCL script that I'm running:

module "ecs" {
  source  = "terraform-aws-modules/ecs/aws"
  version = "5.7.2"

  cluster_name = "ecs-test"

  fargate_capacity_providers = {
    FARGATE = {
      default_capacity_provider_strategy = {
        weight = 50
      }
    }
    FARGATE_SPOT = {
      default_capacity_provider_strategy = {
        weight = 50
      }
    }
  }

  services = {
    my_app = {
      name          = "my_app_service"
      cpu           = 256
      memory        = 512
      desired_count = 1

      capacity_provider_strategies = [
        {
          capacity_provider = "FARGATE_SPOT"
          base              = 0
          weight            = 1
        }
      ]

      # Task definition
      container_definitions = {
        postgres = {
          cpu       = 256
          memory    = 512
          essential = true
          image     = "my_app_image"

          port_mappings = [
            {
              name          = "my-app-port"
              containerPort = 123
              protocol      = "tcp"
            }
          ]
        }
      }

      # Network
      subnet_ids = [
        "subnet-123",
      ]

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

      assign_public_ip = true
    }
  }
}

When I apply the script above (with terraform apply), the cluster, the service and everything are created properly in my AWS account, however when I check the configuration of my service it's a normal FARGATE instance:

enter image description here

If I create a new service manually, using the AWS UI interface, I can create a FARGATE_SPOT without any problem, and it looks like this when I check the service's Configuration tab (expected result):

enter image description here

Question: what am I doing wrong in my HCL script above? I want to use Terraform's ECS module to create a service with capacity provider = FARGATE_SPOT.


Solution

  • You have defined capacity_provider_strategies in your services map, but that's not actually a valid input. It should be capacity_provider_strategy. Please see this line of the module documentation on how it uses its inputs for services.