Skip to content

Commit 9cb2b84

Browse files
authored
fix: Allow for both Fargate and EC2/Autoscaling capacity providers in same cluster (#69)
1 parent ea98201 commit 9cb2b84

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.73.0
3+
rev: v1.74.1
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ No modules.
231231
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Name of the cluster (up to 255 letters, numbers, hyphens, and underscores) | `string` | `""` | no |
232232
| <a name="input_cluster_settings"></a> [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)` | <pre>{<br> "name": "containerInsights",<br> "value": "enabled"<br>}</pre> | no |
233233
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
234+
| <a name="input_default_capacity_provider_use_fargate"></a> [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 |
234235
| <a name="input_fargate_capacity_providers"></a> [fargate\_capacity\_providers](#input\_fargate\_capacity\_providers) | Map of Fargate capacity provider definitions to use for the cluster | `any` | `{}` | no |
235236
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
236237

examples/complete/main.tf

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ module "ecs" {
4242
}
4343
}
4444

45+
default_capacity_provider_use_fargate = false
46+
47+
# Capacity provider - Fargate
48+
fargate_capacity_providers = {
49+
FARGATE = {}
50+
FARGATE_SPOT = {}
51+
}
52+
4553
# Capacity provider - autoscaling groups
4654
autoscaling_capacity_providers = {
4755
one = {

main.tf

+9-9
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,14 @@ resource "aws_ecs_cluster" "this" {
5151
################################################################################
5252

5353
locals {
54-
# We are merging these together so that we can reference the ECS capacity provider
55-
# (ec2 autoscaling) created in this module below. Fargate is easy since its just
56-
# static values, but the autoscaling cappacity provider needs to be self-referenced from
57-
# within this module
58-
cluster_capacity_providers = merge(
59-
var.fargate_capacity_providers,
60-
{ for k, v in var.autoscaling_capacity_providers : k => merge(aws_ecs_capacity_provider.this[k], v) }
54+
default_capacity_providers = merge(
55+
{ for k, v in var.fargate_capacity_providers : k => v if var.default_capacity_provider_use_fargate },
56+
{ for k, v in var.autoscaling_capacity_providers : k => v if !var.default_capacity_provider_use_fargate }
6157
)
6258
}
6359

6460
resource "aws_ecs_cluster_capacity_providers" "this" {
65-
count = var.create ? 1 : 0
61+
count = var.create && length(merge(var.fargate_capacity_providers, var.autoscaling_capacity_providers)) > 0 ? 1 : 0
6662

6763
cluster_name = aws_ecs_cluster.this[0].name
6864
capacity_providers = distinct(concat(
@@ -71,7 +67,7 @@ resource "aws_ecs_cluster_capacity_providers" "this" {
7167
))
7268

7369
dynamic "default_capacity_provider_strategy" {
74-
for_each = local.cluster_capacity_providers
70+
for_each = local.default_capacity_providers
7571
iterator = strategy
7672

7773
content {
@@ -80,6 +76,10 @@ resource "aws_ecs_cluster_capacity_providers" "this" {
8076
weight = try(strategy.value.default_capacity_provider_strategy.weight, null)
8177
}
8278
}
79+
80+
depends_on = [
81+
aws_ecs_capacity_provider.this
82+
]
8383
}
8484

8585
################################################################################

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ variable "cluster_settings" {
3939
# Capacity Providers
4040
################################################################################
4141

42+
variable "default_capacity_provider_use_fargate" {
43+
description = "Determines whether to use Fargate or autoscaling for default capacity provider strategy"
44+
type = bool
45+
default = true
46+
}
47+
4248
variable "fargate_capacity_providers" {
4349
description = "Map of Fargate capacity provider definitions to use for the cluster"
4450
type = any

0 commit comments

Comments
 (0)