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

Change to Terraform 0.12. #9

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SHELL := /bin/bash

# List of targets the `readme` target should call before generating the readme
export README_DEPS ?= docs/targets.md docs/terraform.md
export TERRAFORM_VERSION=0.12.10

-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)

Expand Down
144 changes: 84 additions & 60 deletions alarms.tf
Original file line number Diff line number Diff line change
@@ -1,119 +1,143 @@
module "cpu_utilization_high_alarm_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.3"
name = "${var.name}"
namespace = "${var.namespace}"
stage = "${var.stage}"
attributes = "${compact(concat(var.attributes, list("cpu", "utilization", "high")))}"
name = var.name
namespace = var.namespace
stage = var.stage
attributes = compact(concat(var.attributes, ["cpu", "utilization", "high"]))
}

module "cpu_utilization_low_alarm_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.3"
name = "${var.name}"
namespace = "${var.namespace}"
stage = "${var.stage}"
attributes = "${compact(concat(var.attributes, list("cpu", "utilization", "low")))}"
name = var.name
namespace = var.namespace
stage = var.stage
attributes = compact(concat(var.attributes, ["cpu", "utilization", "low"]))
}

module "memory_utilization_high_alarm_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.3"
name = "${var.name}"
namespace = "${var.namespace}"
stage = "${var.stage}"
attributes = "${compact(concat(var.attributes, list("memory", "utilization", "high")))}"
name = var.name
namespace = var.namespace
stage = var.stage
attributes = compact(concat(var.attributes, ["memory", "utilization", "high"]))
}

module "memory_utilization_low_alarm_label" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.1.3"
name = "${var.name}"
namespace = "${var.namespace}"
stage = "${var.stage}"
attributes = "${compact(concat(var.attributes, list("memory", "utilization", "low")))}"
name = var.name
namespace = var.namespace
stage = var.stage
attributes = compact(concat(var.attributes, ["memory", "utilization", "low"]))
}

locals {
thresholds = {
CPUUtilizationHighThreshold = "${min(max(var.cpu_utilization_high_threshold, 0), 100)}"
CPUUtilizationLowThreshold = "${min(max(var.cpu_utilization_low_threshold, 0), 100)}"
MemoryUtilizationHighThreshold = "${min(max(var.memory_utilization_high_threshold, 0), 100)}"
MemoryUtilizationLowThreshold = "${min(max(var.memory_utilization_low_threshold, 0), 100)}"
CPUUtilizationHighThreshold = min(max(var.cpu_utilization_high_threshold, 0), 100)
CPUUtilizationLowThreshold = min(max(var.cpu_utilization_low_threshold, 0), 100)
MemoryUtilizationHighThreshold = min(max(var.memory_utilization_high_threshold, 0), 100)
MemoryUtilizationLowThreshold = min(max(var.memory_utilization_low_threshold, 0), 100)
}

dimensions_map = {
"service" = {
"ClusterName" = "${var.cluster_name}"
"ServiceName" = "${var.service_name}"
"ClusterName" = var.cluster_name
"ServiceName" = var.service_name
}

"cluster" = {
"ClusterName" = "${var.cluster_name}"
"ClusterName" = var.cluster_name
}
}
}

resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
count = "${local.enabled}"
alarm_name = "${module.cpu_utilization_high_alarm_label.id}"
count = local.enabled
alarm_name = module.cpu_utilization_high_alarm_label.id
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "${var.cpu_utilization_high_evaluation_periods}"
evaluation_periods = var.cpu_utilization_high_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "${var.cpu_utilization_high_period}"
period = var.cpu_utilization_high_period
statistic = "Average"
threshold = "${local.thresholds["CPUUtilizationHighThreshold"]}"
alarm_description = "${format(var.alarm_description, "CPU", "High", var.cpu_utilization_high_period/60, var.cpu_utilization_high_evaluation_periods)}"
alarm_actions = ["${compact(var.cpu_utilization_high_alarm_actions)}"]
ok_actions = ["${compact(var.cpu_utilization_high_ok_actions)}"]
threshold = local.thresholds["CPUUtilizationHighThreshold"]
alarm_description = format(
var.alarm_description,
"CPU",
"High",
var.cpu_utilization_high_period / 60,
var.cpu_utilization_high_evaluation_periods,
)
alarm_actions = compact(var.cpu_utilization_high_alarm_actions)
ok_actions = compact(var.cpu_utilization_high_ok_actions)

dimensions = "${local.dimensions_map[var.service_name == "" ? "cluster" : "service"]}"
dimensions = local.dimensions_map[var.service_name == "" ? "cluster" : "service"]
}

resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
count = "${local.enabled}"
alarm_name = "${module.cpu_utilization_low_alarm_label.id}"
count = local.enabled
alarm_name = module.cpu_utilization_low_alarm_label.id
comparison_operator = "LessThanThreshold"
evaluation_periods = "${var.cpu_utilization_low_evaluation_periods}"
evaluation_periods = var.cpu_utilization_low_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "${var.cpu_utilization_low_period}"
period = var.cpu_utilization_low_period
statistic = "Average"
threshold = "${local.thresholds["CPUUtilizationLowThreshold"]}"
alarm_description = "${format(var.alarm_description, "CPU", "Low", var.cpu_utilization_low_period/60, var.cpu_utilization_low_evaluation_periods)}"
alarm_actions = ["${compact(var.cpu_utilization_low_alarm_actions)}"]
ok_actions = ["${compact(var.cpu_utilization_low_ok_actions)}"]
threshold = local.thresholds["CPUUtilizationLowThreshold"]
alarm_description = format(
var.alarm_description,
"CPU",
"Low",
var.cpu_utilization_low_period / 60,
var.cpu_utilization_low_evaluation_periods,
)
alarm_actions = compact(var.cpu_utilization_low_alarm_actions)
ok_actions = compact(var.cpu_utilization_low_ok_actions)

dimensions = "${local.dimensions_map[var.service_name == "" ? "cluster" : "service"]}"
dimensions = local.dimensions_map[var.service_name == "" ? "cluster" : "service"]
}

resource "aws_cloudwatch_metric_alarm" "memory_utilization_high" {
count = "${local.enabled}"
alarm_name = "${module.memory_utilization_high_alarm_label.id}"
count = local.enabled
alarm_name = module.memory_utilization_high_alarm_label.id
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "${var.memory_utilization_high_evaluation_periods}"
evaluation_periods = var.memory_utilization_high_evaluation_periods
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
period = "${var.memory_utilization_high_period}"
period = var.memory_utilization_high_period
statistic = "Average"
threshold = "${local.thresholds["MemoryUtilizationHighThreshold"]}"
alarm_description = "${format(var.alarm_description, "Memory", "Hight", var.memory_utilization_high_period/60, var.memory_utilization_high_evaluation_periods)}"
alarm_actions = ["${compact(var.memory_utilization_high_alarm_actions)}"]
ok_actions = ["${compact(var.memory_utilization_high_ok_actions)}"]
threshold = local.thresholds["MemoryUtilizationHighThreshold"]
alarm_description = format(
var.alarm_description,
"Memory",
"Hight",
var.memory_utilization_high_period / 60,
var.memory_utilization_high_evaluation_periods,
)
alarm_actions = compact(var.memory_utilization_high_alarm_actions)
ok_actions = compact(var.memory_utilization_high_ok_actions)

dimensions = "${local.dimensions_map[var.service_name == "" ? "cluster" : "service"]}"
dimensions = local.dimensions_map[var.service_name == "" ? "cluster" : "service"]
}

resource "aws_cloudwatch_metric_alarm" "memory_utilization_low" {
count = "${local.enabled}"
alarm_name = "${module.memory_utilization_low_alarm_label.id}"
count = local.enabled
alarm_name = module.memory_utilization_low_alarm_label.id
comparison_operator = "LessThanThreshold"
evaluation_periods = "${var.memory_utilization_low_evaluation_periods}"
evaluation_periods = var.memory_utilization_low_evaluation_periods
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
period = "${var.memory_utilization_low_period}"
period = var.memory_utilization_low_period
statistic = "Average"
threshold = "${local.thresholds["MemoryUtilizationLowThreshold"]}"
alarm_description = "${format(var.alarm_description, "Memory", "Low", var.memory_utilization_low_period/60, var.memory_utilization_low_evaluation_periods)}"
alarm_actions = ["${compact(var.memory_utilization_low_alarm_actions)}"]
ok_actions = ["${compact(var.memory_utilization_low_ok_actions)}"]
threshold = local.thresholds["MemoryUtilizationLowThreshold"]
alarm_description = format(
var.alarm_description,
"Memory",
"Low",
var.memory_utilization_low_period / 60,
var.memory_utilization_low_evaluation_periods,
)
alarm_actions = compact(var.memory_utilization_low_alarm_actions)
ok_actions = compact(var.memory_utilization_low_ok_actions)

dimensions = "${local.dimensions_map[var.service_name == "" ? "cluster" : "service"]}"
dimensions = local.dimensions_map[var.service_name == "" ? "cluster" : "service"]
}

6 changes: 4 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
data "aws_caller_identity" "default" {}
data "aws_caller_identity" "default" {
}

locals {
enabled = "${var.enabled == "true" ? 1 : 0}"
enabled = var.enabled == "true" ? 1 : 0
}

1 change: 0 additions & 1 deletion outputs.tf

This file was deleted.

Loading