Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
(The Cluster cannot be deleted/renamed while Container Instances are active or draining. )
+ attempt to inverse dependencies on efs_sg_ids and efs_id for ASG aws_launch_configuration
  • Loading branch information
mslipets committed Jan 15, 2021
1 parent 861ec7b commit e539898
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ ecs_additional_iam_statements = [
| cluster_arn | The ECS cluster ARN |
| cluster_asg_name | The ECS cluster Auto Scaling Group name, used to attach Auto Scaling Policies |
| cluster_asg_arn | The ECS cluster Auto Scaling Group arn, used for ECS capacity providers |
| cluster_aws_launch_configuration_name | The ECS cluster AutoScaling Group aws_launch_configuration Name |
| cluster_iam_role_arn | The ECS cluster IAM role ARN, useful for attaching to ECR repos |

## Authors
Expand Down
21 changes: 18 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ data "template_file" "user_data" {
template = file("${path.module}/user_data.tpl")
vars = {
ecs_cluster_name = var.ecs_name
efs_id = var.efs_id
http_proxy = var.http_proxy
http_proxy_port = var.http_proxy_port
efs_id = var.efs_id
http_proxy = var.http_proxy
http_proxy_port = var.http_proxy_port
}
}

Expand Down Expand Up @@ -81,6 +81,21 @@ resource "aws_ecs_cluster" "this" {
depends_on = [aws_ecs_capacity_provider.this]
}

##NB: https://github.com/hashicorp/terraform-provider-aws/issues/4852
## The Cluster cannot be deleted/renamed while Container Instances are active or draining.
resource "null_resource" "asg-scale-to-0-on-destroy" {
triggers = {
cluster_arn = aws_ecs_cluster.this.arn
capacity_providers_arn = join(",", aws_ecs_cluster.this.capacity_providers)
asg_name = aws_autoscaling_group.this.name
}
provisioner "local-exec" {
when = destroy
command = "aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${self.triggers.asg_name} --min-size 0 --max-size 0 --desired-capacity 0"
}
depends_on = [aws_ecs_cluster.this]
}

resource "aws_autoscaling_group" "this" {
name = var.ecs_name
min_size = var.ecs_min_size
Expand Down
20 changes: 15 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@
# Outputs
#------------------------------------------------------------------------------
output "cluster_id" {
value = aws_ecs_cluster.this.id
description = "Cluster ID"
value = aws_ecs_cluster.this.id
}

output "cluster_arn" {
value = aws_ecs_cluster.this.arn
description = "Cluster ARN"
value = aws_ecs_cluster.this.arn
}

output "cluster_asg_name" {
value = aws_autoscaling_group.this.name
description = "Cluster AutoScaling Group Name"
value = aws_autoscaling_group.this.name
}

output "cluster_asg_arn" {
value = aws_autoscaling_group.this.arn
description = "Cluster AutoScaling Group ARN"
value = aws_autoscaling_group.this.arn
}

output "cluster_iam_role_arn" {
value = aws_iam_role.this.arn
description = "Cluster IAM role ARN"
value = aws_iam_role.this.arn
}

output "cluster_aws_launch_configuration_name" {
description = "Cluster AutoScaling Group aws_launch_configuration Name"
value = aws_launch_configuration.this.name
}

0 comments on commit e539898

Please sign in to comment.