Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to TF 0.12 #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ resource "aws_iam_role" "sns" {
]
}
EOF

}

# creating policy document and attaching as inline policies instead of using the AutoScalingNotificationAccessRole
Expand All @@ -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" {
Expand All @@ -59,6 +60,7 @@ resource "aws_iam_role" "lambda" {
]
}
EOF

}

data "aws_iam_policy_document" "lambda" {
Expand Down Expand Up @@ -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" {
Expand All @@ -110,55 +112,60 @@ 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" {
name = "${var.autoscaling_group_name}-sns-topic"
}

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
}

13 changes: 8 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}