diff --git a/main.tf b/main.tf index 6318396..479e736 100644 --- a/main.tf +++ b/main.tf @@ -16,6 +16,7 @@ resource "aws_iam_role" "sns" { ] } EOF + } # creating policy document and attaching as inline policies instead of using the AutoScalingNotificationAccessRole @@ -39,8 +40,8 @@ data "aws_iam_policy_document" "auto_scaling_notification_access" { resource "aws_iam_role_policy" "asg_notification_sns" { name = "${aws_iam_role.sns.name}-asg-notification-policy" - role = "${aws_iam_role.sns.id}" - policy = "${data.aws_iam_policy_document.auto_scaling_notification_access.json}" + role = aws_iam_role.sns.id + policy = data.aws_iam_policy_document.auto_scaling_notification_access.json } resource "aws_iam_role" "lambda" { @@ -59,6 +60,7 @@ resource "aws_iam_role" "lambda" { ] } EOF + } data "aws_iam_policy_document" "lambda" { @@ -93,14 +95,14 @@ data "aws_iam_policy_document" "lambda" { resource "aws_iam_role_policy" "lambda" { name = "${aws_iam_role.lambda.name}-policy" - role = "${aws_iam_role.lambda.id}" - policy = "${data.aws_iam_policy_document.lambda.json}" + role = aws_iam_role.lambda.id + policy = data.aws_iam_policy_document.lambda.json } resource "aws_iam_role_policy" "asg_notification_lambda" { name = "${aws_iam_role.lambda.name}-asg-notification-policy" - role = "${aws_iam_role.lambda.id}" - policy = "${data.aws_iam_policy_document.auto_scaling_notification_access.json}" + role = aws_iam_role.lambda.id + policy = data.aws_iam_policy_document.auto_scaling_notification_access.json } data "archive_file" "index" { @@ -110,36 +112,40 @@ data "archive_file" "index" { } resource "aws_lambda_function" "lambda" { - runtime = "python3.6" - filename = "${path.module}/files/index.zip" - function_name = "${substr(var.autoscaling_group_name,0,min(64, length(var.autoscaling_group_name)))}" - role = "${aws_iam_role.lambda.arn}" - handler = "index.lambda_handler" - timeout = "${var.function_sleep_time * 2}" - - source_code_hash = "${data.archive_file.index.output_base64sha256}" + runtime = "python3.6" + filename = "${path.module}/files/index.zip" + function_name = substr( + var.autoscaling_group_name, + 0, + min(64, length(var.autoscaling_group_name)), + ) + role = aws_iam_role.lambda.arn + handler = "index.lambda_handler" + timeout = var.function_sleep_time * 2 + + source_code_hash = data.archive_file.index.output_base64sha256 environment { variables = { - REGION = "${var.region}" - CLUSTER_NAME = "${var.cluster_name}" - SLEEP_TIME = "${var.function_sleep_time}" + REGION = var.region + CLUSTER_NAME = var.cluster_name + SLEEP_TIME = var.function_sleep_time } } lifecycle { # A workaround when running this code on different machines is to ignore changes, as described here: # https://github.com/hashicorp/terraform/issues/7613#issuecomment-241603087 - ignore_changes = ["filename"] + ignore_changes = [filename] } } resource "aws_lambda_permission" "sns" { statement_id = "AllowExecutionFromSNS" - function_name = "${aws_lambda_function.lambda.arn}" + function_name = aws_lambda_function.lambda.arn action = "lambda:InvokeFunction" principal = "sns.amazonaws.com" - source_arn = "${aws_sns_topic.asg_sns.arn}" + source_arn = aws_sns_topic.asg_sns.arn } resource "aws_sns_topic" "asg_sns" { @@ -147,18 +153,19 @@ resource "aws_sns_topic" "asg_sns" { } resource "aws_sns_topic_subscription" "asg_sns" { - topic_arn = "${aws_sns_topic.asg_sns.arn}" + topic_arn = aws_sns_topic.asg_sns.arn protocol = "lambda" - endpoint = "${aws_lambda_function.lambda.arn}" + endpoint = aws_lambda_function.lambda.arn } resource "aws_autoscaling_lifecycle_hook" "terminate" { - count = "${var.lambda_enabled}" + count = var.lambda_enabled name = "${var.autoscaling_group_name}-terminate-hook" - autoscaling_group_name = "${var.autoscaling_group_name}" - default_result = "${var.hook_default_result}" - heartbeat_timeout = "${var.hook_heartbeat_timeout}" + autoscaling_group_name = var.autoscaling_group_name + default_result = var.hook_default_result + heartbeat_timeout = var.hook_heartbeat_timeout lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING" - notification_target_arn = "${aws_sns_topic.asg_sns.arn}" - role_arn = "${aws_iam_role.sns.arn}" + notification_target_arn = aws_sns_topic.asg_sns.arn + role_arn = aws_iam_role.sns.arn } + diff --git a/variables.tf b/variables.tf index 235310c..73d878d 100644 --- a/variables.tf +++ b/variables.tf @@ -1,16 +1,19 @@ -variable "region" {} +variable "region" { +} -variable "cluster_name" {} +variable "cluster_name" { +} -variable "autoscaling_group_name" {} +variable "autoscaling_group_name" { +} variable "function_sleep_time" { description = "Number of seconds the function should sleep before checking ECS Instance Task Count again" - default = 15 + default = 15 } variable "lambda_enabled" { - default = true + default = 1 } variable "hook_heartbeat_timeout" { diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..ac97c6a --- /dev/null +++ b/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +}