Skip to content

Commit

Permalink
fix: Ensure that launch type is not specified when a capacity provide…
Browse files Browse the repository at this point in the history
…r strategy is set on a service (#80)
  • Loading branch information
bryantbiggs authored Apr 26, 2023
1 parent b484eaa commit 873cccf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
34 changes: 25 additions & 9 deletions examples/ec2-autoscaling/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ module "ecs_cluster" {
# Capacity provider - autoscaling groups
default_capacity_provider_use_fargate = false
autoscaling_capacity_providers = {
one = {
auto_scaling_group_arn = module.autoscaling["one"].autoscaling_group_arn
# On-demand instances
ex-1 = {
auto_scaling_group_arn = module.autoscaling["ex-1"].autoscaling_group_arn
managed_termination_protection = "ENABLED"

managed_scaling = {
Expand All @@ -49,8 +50,9 @@ module "ecs_cluster" {
base = 20
}
}
two = {
auto_scaling_group_arn = module.autoscaling["two"].autoscaling_group_arn
# Spot instances
ex-2 = {
auto_scaling_group_arn = module.autoscaling["ex-2"].autoscaling_group_arn
managed_termination_protection = "ENABLED"

managed_scaling = {
Expand Down Expand Up @@ -82,7 +84,15 @@ module "ecs_service" {

# Task Definition
requires_compatibilities = ["EC2"]
launch_type = "EC2"
capacity_provider_strategy = {
# On-demand instances
ex-1 = {
capacity_provider = module.ecs_cluster.autoscaling_capacity_providers["ex-1"].name
weight = 1
base = 1
}
}

volume = {
my-vol = {}
}
Expand Down Expand Up @@ -199,19 +209,23 @@ module "autoscaling" {
version = "~> 6.5"

for_each = {
on-demand = {
instance_type = "t3.small"
# On-demand instances
ex-1 = {
instance_type = "t3.large"
use_mixed_instances_policy = false
mixed_instances_policy = {}
user_data = <<-EOT
#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${local.name}
ECS_LOGLEVEL=debug
ECS_CONTAINER_INSTANCE_TAGS=${jsonencode(local.tags)}
ECS_ENABLE_TASK_IAM_ROLE=true
EOF
EOT
}
spot = {
# Spot instances
ex-2 = {
instance_type = "t3.medium"
use_mixed_instances_policy = true
mixed_instances_policy = {
Expand All @@ -227,7 +241,7 @@ module "autoscaling" {
weighted_capacity = "2"
},
{
instance_type = "t3.medium"
instance_type = "t3.large"
weighted_capacity = "1"
},
]
Expand All @@ -237,6 +251,8 @@ module "autoscaling" {
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${local.name}
ECS_LOGLEVEL=debug
ECS_CONTAINER_INSTANCE_TAGS=${jsonencode(local.tags)}
ECS_ENABLE_TASK_IAM_ROLE=true
ECS_ENABLE_SPOT_INSTANCE_DRAINING=true
EOF
EOT
Expand Down
8 changes: 4 additions & 4 deletions modules/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ resource "aws_ecs_service" "this" {
force_new_deployment = local.is_external_deployment ? null : var.force_new_deployment
health_check_grace_period_seconds = var.health_check_grace_period_seconds
iam_role = local.iam_role_arn
launch_type = local.is_external_deployment ? null : var.launch_type
launch_type = local.is_external_deployment || length(var.capacity_provider_strategy) > 0 ? null : var.launch_type

dynamic "load_balancer" {
# Set by task set if deployment controller is external
Expand Down Expand Up @@ -264,7 +264,7 @@ resource "aws_ecs_service" "ignore_task_definition" {
force_new_deployment = local.is_external_deployment ? null : var.force_new_deployment
health_check_grace_period_seconds = var.health_check_grace_period_seconds
iam_role = local.iam_role_arn
launch_type = local.is_external_deployment ? null : var.launch_type
launch_type = local.is_external_deployment || length(var.capacity_provider_strategy) > 0 ? null : var.launch_type

dynamic "load_balancer" {
# Set by task set if deployment controller is external
Expand Down Expand Up @@ -1047,7 +1047,7 @@ resource "aws_ecs_task_set" "this" {
}
}

launch_type = var.launch_type
launch_type = length(var.capacity_provider_strategy) > 0 ? null : var.launch_type

dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategy
Expand Down Expand Up @@ -1128,7 +1128,7 @@ resource "aws_ecs_task_set" "ignore_task_definition" {
}
}

launch_type = var.launch_type
launch_type = length(var.capacity_provider_strategy) > 0 ? null : var.launch_type

dynamic "capacity_provider_strategy" {
for_each = var.capacity_provider_strategy
Expand Down

0 comments on commit 873cccf

Please sign in to comment.