Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Latest commit

 

History

History
99 lines (70 loc) · 3.61 KB

File metadata and controls

99 lines (70 loc) · 3.61 KB

ECS container instance draining on lifecycle events

Conventional Commits CI

Overview

Automates Container Instance Draining in Amazon ECS by removing tasks from an instance before scaling down a cluster with Auto Scaling Groups. Heavily inspired by this blog post from AWS. Lambda source code taken from and terraform inspired by the CloudFormation stack from Amazon available here.

It works by consuming lifecyle events from an autoscaling group. When an autoscaling:EC2_INSTANCE_TERMINATING event happens for the specified ASG it is placed on an SNS topic which in turn triggers lambda that will drain the tasks first from the ECS instance and then terminate the instance once the number of tasks on the instance become zero.

Architecture

Usage

data "aws_region" "current" {}

module "example_module_test" {
  source = "git::https://github.com/edispark/terraform-ecs-container-instance-draining"

  autoscaling_group_name = "arn:partition:service:region:account-id:autoScalingGroupName/XXX"
  autoscaling_group_arn = "my-asg-name"
  ecs_cluster_arn = "arn:partition:service:region:account-id:cluster/XXX"
  ecs_cluster_name = "my-ecs-cluster-name"

  region                 = data.aws_region.current.name
}

Requirements

Name Version
terraform >= 0.12.26
aws >= 3.0
local 1.4.0

Providers

Name Version
aws >= 3.0
local 1.4.0

Inputs

Name Description Type Default Required
autoscaling_group_arn The name of the Auto Scaling group to which you want to assign the lifecycle hook to string n/a yes
autoscaling_group_name The name of the Auto Scaling group to which you want to assign the lifecycle hook to string n/a yes
ecs_cluster_arn Specifies the ECS Cluster ARN with which the resources would be associated string n/a yes
ecs_cluster_name Specifies the ECS Cluster Name with which the resources would be associated string n/a yes
region AWS Region for ECS cluster string n/a yes
tags Additional tags (_e.g._ { BusinessUnit : ABC }) map(string) {} no

Outputs

Name Description
sns_topic_arn Topic used by ASG to send notifications when instance state is changing

Development

Prerequisites

Configurations

  • Configure pre-commit hooks
pre-commit install
  • Configure golang deps for tests
> go get github.com/gruntwork-io/terratest/modules/terraform
> go get github.com/stretchr/testify/assert

Tests

  • Tests are available in test directory
  • In the test directory, run the below command
go test