From 9cb2b84e5bb3983392c2d4c0e1f2879b42bbf9a7 Mon Sep 17 00:00:00 2001 From: Bryant Biggs Date: Mon, 25 Jul 2022 15:03:26 -0400 Subject: [PATCH] fix: Allow for both Fargate and EC2/Autoscaling capacity providers in same cluster (#69) --- .pre-commit-config.yaml | 2 +- README.md | 1 + examples/complete/main.tf | 8 ++++++++ main.tf | 18 +++++++++--------- variables.tf | 6 ++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9eaabc7..727e21c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.73.0 + rev: v1.74.1 hooks: - id: terraform_fmt - id: terraform_validate diff --git a/README.md b/README.md index 30468b4..e09cbf2 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,7 @@ No modules. | [cluster\_name](#input\_cluster\_name) | Name of the cluster (up to 255 letters, numbers, hyphens, and underscores) | `string` | `""` | no | | [cluster\_settings](#input\_cluster\_settings) | Configuration block(s) with cluster settings. For example, this can be used to enable CloudWatch Container Insights for a cluster | `map(string)` |
{
"name": "containerInsights",
"value": "enabled"
}
| no | | [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no | +| [default\_capacity\_provider\_use\_fargate](#input\_default\_capacity\_provider\_use\_fargate) | Determines whether to use Fargate or autoscaling for default capacity provider strategy | `bool` | `true` | no | | [fargate\_capacity\_providers](#input\_fargate\_capacity\_providers) | Map of Fargate capacity provider definitions to use for the cluster | `any` | `{}` | no | | [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 22e7adc..9c70e5e 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -42,6 +42,14 @@ module "ecs" { } } + default_capacity_provider_use_fargate = false + + # Capacity provider - Fargate + fargate_capacity_providers = { + FARGATE = {} + FARGATE_SPOT = {} + } + # Capacity provider - autoscaling groups autoscaling_capacity_providers = { one = { diff --git a/main.tf b/main.tf index c9a070f..0d53949 100644 --- a/main.tf +++ b/main.tf @@ -51,18 +51,14 @@ resource "aws_ecs_cluster" "this" { ################################################################################ locals { - # We are merging these together so that we can reference the ECS capacity provider - # (ec2 autoscaling) created in this module below. Fargate is easy since its just - # static values, but the autoscaling cappacity provider needs to be self-referenced from - # within this module - cluster_capacity_providers = merge( - var.fargate_capacity_providers, - { for k, v in var.autoscaling_capacity_providers : k => merge(aws_ecs_capacity_provider.this[k], v) } + default_capacity_providers = merge( + { for k, v in var.fargate_capacity_providers : k => v if var.default_capacity_provider_use_fargate }, + { for k, v in var.autoscaling_capacity_providers : k => v if !var.default_capacity_provider_use_fargate } ) } resource "aws_ecs_cluster_capacity_providers" "this" { - count = var.create ? 1 : 0 + count = var.create && length(merge(var.fargate_capacity_providers, var.autoscaling_capacity_providers)) > 0 ? 1 : 0 cluster_name = aws_ecs_cluster.this[0].name capacity_providers = distinct(concat( @@ -71,7 +67,7 @@ resource "aws_ecs_cluster_capacity_providers" "this" { )) dynamic "default_capacity_provider_strategy" { - for_each = local.cluster_capacity_providers + for_each = local.default_capacity_providers iterator = strategy content { @@ -80,6 +76,10 @@ resource "aws_ecs_cluster_capacity_providers" "this" { weight = try(strategy.value.default_capacity_provider_strategy.weight, null) } } + + depends_on = [ + aws_ecs_capacity_provider.this + ] } ################################################################################ diff --git a/variables.tf b/variables.tf index dd66323..6a74075 100644 --- a/variables.tf +++ b/variables.tf @@ -39,6 +39,12 @@ variable "cluster_settings" { # Capacity Providers ################################################################################ +variable "default_capacity_provider_use_fargate" { + description = "Determines whether to use Fargate or autoscaling for default capacity provider strategy" + type = bool + default = true +} + variable "fargate_capacity_providers" { description = "Map of Fargate capacity provider definitions to use for the cluster" type = any