Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow for both Fargate and EC2/Autoscaling capacity providers in same cluster #69

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ No modules.
| <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 |
| <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 |
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
| <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 |
| <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 |
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |

Expand Down
8 changes: 8 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 9 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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
]
}

################################################################################
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down