Skip to content

Commit d102361

Browse files
authored
Merge pull request #2 from mslipets/fix/asg-scale-to-0-on-destroy
fix for hashicorp/terraform-provider-aws#4852
2 parents 861ec7b + e539898 commit d102361

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ ecs_additional_iam_statements = [
9393
| cluster_arn | The ECS cluster ARN |
9494
| cluster_asg_name | The ECS cluster Auto Scaling Group name, used to attach Auto Scaling Policies |
9595
| cluster_asg_arn | The ECS cluster Auto Scaling Group arn, used for ECS capacity providers |
96+
| cluster_aws_launch_configuration_name | The ECS cluster AutoScaling Group aws_launch_configuration Name |
9697
| cluster_iam_role_arn | The ECS cluster IAM role ARN, useful for attaching to ECR repos |
9798

9899
## Authors

main.tf

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ data "template_file" "user_data" {
1818
template = file("${path.module}/user_data.tpl")
1919
vars = {
2020
ecs_cluster_name = var.ecs_name
21-
efs_id = var.efs_id
22-
http_proxy = var.http_proxy
23-
http_proxy_port = var.http_proxy_port
21+
efs_id = var.efs_id
22+
http_proxy = var.http_proxy
23+
http_proxy_port = var.http_proxy_port
2424
}
2525
}
2626

@@ -81,6 +81,21 @@ resource "aws_ecs_cluster" "this" {
8181
depends_on = [aws_ecs_capacity_provider.this]
8282
}
8383

84+
##NB: https://github.com/hashicorp/terraform-provider-aws/issues/4852
85+
## The Cluster cannot be deleted/renamed while Container Instances are active or draining.
86+
resource "null_resource" "asg-scale-to-0-on-destroy" {
87+
triggers = {
88+
cluster_arn = aws_ecs_cluster.this.arn
89+
capacity_providers_arn = join(",", aws_ecs_cluster.this.capacity_providers)
90+
asg_name = aws_autoscaling_group.this.name
91+
}
92+
provisioner "local-exec" {
93+
when = destroy
94+
command = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${self.triggers.asg_name} --min-size 0 --max-size 0 --desired-capacity 0"
95+
}
96+
depends_on = [aws_ecs_cluster.this]
97+
}
98+
8499
resource "aws_autoscaling_group" "this" {
85100
name = var.ecs_name
86101
min_size = var.ecs_min_size

outputs.tf

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@
22
# Outputs
33
#------------------------------------------------------------------------------
44
output "cluster_id" {
5-
value = aws_ecs_cluster.this.id
5+
description = "Cluster ID"
6+
value = aws_ecs_cluster.this.id
67
}
78

89
output "cluster_arn" {
9-
value = aws_ecs_cluster.this.arn
10+
description = "Cluster ARN"
11+
value = aws_ecs_cluster.this.arn
1012
}
1113

1214
output "cluster_asg_name" {
13-
value = aws_autoscaling_group.this.name
15+
description = "Cluster AutoScaling Group Name"
16+
value = aws_autoscaling_group.this.name
1417
}
1518

1619
output "cluster_asg_arn" {
17-
value = aws_autoscaling_group.this.arn
20+
description = "Cluster AutoScaling Group ARN"
21+
value = aws_autoscaling_group.this.arn
1822
}
1923

2024
output "cluster_iam_role_arn" {
21-
value = aws_iam_role.this.arn
25+
description = "Cluster IAM role ARN"
26+
value = aws_iam_role.this.arn
27+
}
28+
29+
output "cluster_aws_launch_configuration_name" {
30+
description = "Cluster AutoScaling Group aws_launch_configuration Name"
31+
value = aws_launch_configuration.this.name
2232
}

0 commit comments

Comments
 (0)