Last active
March 14, 2019 23:18
-
-
Save eskp/1da40eab6af7db2952d9decd849e8c18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module "ecs_update_lambdas" { | |
source = "git::https://github.com/xero-oss/ecs-cluster-update-lambda.git//src" | |
region = "${var.region}" | |
} | |
# Send notifications to the SNS topic created by ecs_update_lambdas module on all important Auto Scaling events | |
resource "aws_autoscaling_notification" "asg-terminate" { | |
group_names = [ | |
"${aws_autoscaling_group.ecs-autoscaling-group.name}" | |
] | |
notifications = [ | |
"autoscaling:EC2_INSTANCE_LAUNCH", | |
"autoscaling:EC2_INSTANCE_LAUNCH_ERROR", | |
"autoscaling:EC2_INSTANCE_TERMINATE", | |
"autoscaling:EC2_INSTANCE_TERMINATE_ERROR", | |
] | |
topic_arn = "${module.ecs_update_lambdas.sns_arn}" | |
} | |
# Hook to pause container instances for draining before proceding with termination | |
resource "aws_autoscaling_lifecycle_hook" "asg-terminate" { | |
name = "asg-terminate" | |
autoscaling_group_name = "${aws_autoscaling_group.ecs-autoscaling-group.name}" | |
default_result = "ABANDON" | |
heartbeat_timeout = 420 | |
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING" | |
# the ARN of the SNS topic that drain-lambda subscribes to | |
notification_target_arn = "${module.ecs_update_lambdas.sns_arn}" | |
role_arn = "${aws_iam_role.asg.arn}" | |
} | |
# IAM role which allows ASG to post to the SNS topic | |
resource "aws_iam_role" "asg" { | |
name = "asg-terminate-${var.region}" | |
assume_role_policy = "${data.aws_iam_policy_document.asg.json}" | |
} | |
data "aws_iam_policy_document" "asg" { | |
statement { | |
actions = ["sts:AssumeRole"] | |
principals { | |
type = "Service" | |
identifiers = [ | |
"autoscaling.amazonaws.com", | |
] | |
} | |
} | |
} | |
resource "aws_iam_role_policy_attachment" "asg" { | |
role = "${aws_iam_role.asg.name}" | |
policy_arn = "arn:aws:iam::aws:policy/service-role/AutoScalingNotificationAccessRole" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment