Skip to content

Commit

Permalink
PLAT-2120 terraform 0.12 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Kintaro-Oe committed Dec 5, 2019
1 parent 374b36d commit 50c1a90
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 102 deletions.
161 changes: 84 additions & 77 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,78 +1,85 @@
locals {
service_name = "${var.env}-${lookup(var.release, "component")}"
service_name = "${var.env}-${var.release["component"]}"
}

module "ecs_update_monitor" {
source = "github.com/mergermarket/tf_ecs_update_monitor"
source = "mergermarket/ecs-update-monitor/acuris"
version = "2.0.0"

cluster = "${var.ecs_cluster}"
service = "${module.service.name}"
taskdef = "${module.taskdef.arn}"
cluster = var.ecs_cluster
service = module.service.name
taskdef = module.taskdef.arn
}

module "service" {
source = "github.com/mergermarket/tf_load_balanced_ecs_service?ref=no-target-group"
source = "mergermarket/load-balanced-ecs-service-no-target-group/acuris"
version = "2.0.0"

name = "${local.service_name}${var.name_suffix}"
cluster = "${var.ecs_cluster}"
task_definition = "${module.taskdef.arn}"
container_name = "${lookup(var.release, "component")}${var.name_suffix}"
container_port = "${var.port}"
desired_count = "${var.desired_count}"
target_group_arn = "${var.target_group_arn}"
deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"
deployment_maximum_percent = "${var.deployment_maximum_percent}"
cluster = var.ecs_cluster
task_definition = module.taskdef.arn
container_name = "${var.release["component"]}${var.name_suffix}"
container_port = var.port
desired_count = var.desired_count
target_group_arn = var.target_group_arn
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
deployment_maximum_percent = var.deployment_maximum_percent
}

module "taskdef" {
source = "github.com/mergermarket/tf_ecs_task_definition_with_task_role"
source = "mergermarket/task-definition-with-task-role/acuris"
version = "2.0.1"

family = "${local.service_name}${var.name_suffix}"
container_definitions = ["${module.service_container_definition.rendered}"]
policy = "${var.task_role_policy}"
assume_role_policy = "${var.assume_role_policy}"
volume = "${var.taskdef_volume}"
env = "${var.env}"
release = "${var.release}"
container_definitions = [module.service_container_definition.rendered]
policy = var.task_role_policy
assume_role_policy = var.assume_role_policy
volume = var.taskdef_volume
env = var.env
release = var.release
}

module "service_container_definition" {
source = "github.com/mergermarket/tf_ecs_container_definition"

name = "${lookup(var.release, "component")}${var.name_suffix}"
image = "${lookup(var.release, "image_id")}"
cpu = "${var.cpu}"
memory = "${var.memory}"
container_port = "${var.port}"
nofile_soft_ulimit = "${var.nofile_soft_ulimit}"
mountpoint = "${var.container_mountpoint}"
port_mappings = "${var.container_port_mappings}"
application_secrets = "${var.application_secrets}"
platform_secrets = "${var.platform_secrets}"

container_env = "${merge(
map(
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDOUT", "${local.service_name}${var.name_suffix}-stdout",
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDERR", "${local.service_name}${var.name_suffix}-stderr",
"STATSD_HOST", "172.17.42.1",
"STATSD_PORT", "8125",
"STATSD_ENABLED", "true",
"ENV_NAME", "${var.env}",
"COMPONENT_NAME", "${lookup(var.release, "component")}",
"VERSION", "${lookup(var.release, "version")}"
),
source = "mergermarket/ecs-container-definition/acuris"
version = "2.0.0"

name = "${var.release["component"]}${var.name_suffix}"
image = var.release["image_id"]
cpu = var.cpu
memory = var.memory
container_port = var.port
nofile_soft_ulimit = var.nofile_soft_ulimit
mountpoint = var.container_mountpoint
port_mappings = var.container_port_mappings
application_secrets = var.application_secrets
platform_secrets = var.platform_secrets

container_env = merge(
{
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDOUT" = "${local.service_name}${var.name_suffix}-stdout"
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDERR" = "${local.service_name}${var.name_suffix}-stderr"
"STATSD_HOST" = "172.17.42.1"
"STATSD_PORT" = "8125"
"STATSD_ENABLED" = "true"
"ENV_NAME" = var.env
"COMPONENT_NAME" = var.release["component"]
"VERSION" = var.release["version"]
},
var.common_application_environment,
var.application_environment,
var.secrets
)}"

labels = "${merge(map(
"component", var.release["component"],
"env", var.env,
"team", var.release["team"],
"version", var.release["version"],
"logentries.token", var.logentries_token
), var.container_labels)}"
var.secrets,
)

labels = merge(
{
"component" = var.release["component"]
"env" = var.env
"team" = var.release["team"]
"version" = var.release["version"]
"logentries.token" = var.logentries_token
},
var.container_labels,
)
}

resource "aws_cloudwatch_log_group" "stdout" {
Expand All @@ -86,56 +93,56 @@ resource "aws_cloudwatch_log_group" "stderr" {
}

resource "aws_cloudwatch_log_subscription_filter" "kinesis_log_stdout_stream" {
count = "${var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0}"
count = var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0
name = "kinesis-log-stdout-stream-${local.service_name}"
destination_arn = "${var.platform_config["datadog_log_subscription_arn"]}"
destination_arn = var.platform_config["datadog_log_subscription_arn"]
log_group_name = "${local.service_name}${var.name_suffix}-stdout"
filter_pattern = ""
depends_on = ["aws_cloudwatch_log_group.stdout"]
depends_on = [aws_cloudwatch_log_group.stdout]
}

resource "aws_cloudwatch_log_subscription_filter" "kinesis_log_stderr_stream" {
count = "${var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0}"
count = var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0
name = "kinesis-log-stdout-stream-${local.service_name}"
destination_arn = "${var.platform_config["datadog_log_subscription_arn"]}"
destination_arn = var.platform_config["datadog_log_subscription_arn"]
log_group_name = "${local.service_name}${var.name_suffix}-stderr"
filter_pattern = ""
depends_on = ["aws_cloudwatch_log_group.stderr"]
depends_on = [aws_cloudwatch_log_group.stderr]
}

resource "aws_appautoscaling_target" "ecs" {
count = "${var.allow_overnight_scaledown}"
min_capacity = "${var.desired_count}"
max_capacity = "${var.desired_count}"
count = var.allow_overnight_scaledown
min_capacity = var.desired_count
max_capacity = var.desired_count
resource_id = "service/${var.ecs_cluster}/${local.service_name}${var.name_suffix}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_scheduled_action" "scale_down" {
count = "${var.allow_overnight_scaledown}"
count = var.allow_overnight_scaledown
name = "scale_down-${local.service_name}${var.name_suffix}"
service_namespace = "${aws_appautoscaling_target.ecs.service_namespace}"
resource_id = "${aws_appautoscaling_target.ecs.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.ecs.scalable_dimension}"
schedule = "cron(*/30 ${var.overnight_scaledown_start_hour}-${(var.overnight_scaledown_end_hour)-1} ? * * *)"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = "cron(*/30 ${var.overnight_scaledown_start_hour}-${var.overnight_scaledown_end_hour - 1} ? * * *)"

scalable_target_action {
min_capacity = "${var.overnight_scaledown_min_count}"
max_capacity = "${var.overnight_scaledown_min_count}"
min_capacity = var.overnight_scaledown_min_count
max_capacity = var.overnight_scaledown_min_count
}
}

resource "aws_appautoscaling_scheduled_action" "scale_back_up" {
count = "${var.allow_overnight_scaledown}"
count = var.allow_overnight_scaledown
name = "scale_up-${local.service_name}${var.name_suffix}"
service_namespace = "${aws_appautoscaling_target.ecs.service_namespace}"
resource_id = "${aws_appautoscaling_target.ecs.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.ecs.scalable_dimension}"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = "cron(10 ${var.overnight_scaledown_end_hour} ? * MON-FRI *)"

scalable_target_action {
min_capacity = "${var.desired_count}"
max_capacity = "${var.desired_count}"
min_capacity = var.desired_count
max_capacity = var.desired_count
}
}
7 changes: 4 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
output "task_role_arn" {
value = "${module.taskdef.task_role_arn}"
value = module.taskdef.task_role_arn
}

output "task_role_name" {
value = "${module.taskdef.task_role_name}"
value = module.taskdef.task_role_name
}

output "taskdef_arn" {
value = "${module.taskdef.arn}"
value = module.taskdef.arn
}

46 changes: 24 additions & 22 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,86 @@ variable "env" {

variable "platform_config" {
description = "Platform configuration"
type = "map"
type = map(string)
default = {}
}

variable "release" {
type = "map"
type = map(string)
description = "Metadata about the release"
}

variable "secrets" {
type = "map"
type = map(string)
description = "Secret credentials fetched using credstash"
default = {}
}

variable "common_application_environment" {
description = "Environment parameters passed to the container for all environments"
type = "map"
type = map(string)
default = {}
}

variable "application_environment" {
description = "Environment specific parameters passed to the container"
type = "map"
type = map(string)
default = {}
}

variable "ecs_cluster" {
type = "string"
type = string
description = "The ECS cluster"
default = "default"
}

variable "port" {
type = "string"
type = string
description = "The port that container will be running on"
}

variable "cpu" {
type = "string"
type = string
description = "CPU unit reservation for the container"
}

variable "memory" {
type = "string"
type = string
description = "The memory reservation for the container in megabytes"
}

variable "nofile_soft_ulimit" {
type = "string"
type = string
description = "The soft ulimit for the number of files in container"
default = "4096"
}

variable "desired_count" {
description = "The number of instances of the task definition to place and keep running."
type = "string"
type = string
default = "3"
}

variable "name_suffix" {
description = "Set a suffix that will be applied to the name in order that a component can have multiple services per environment"
type = "string"
type = string
default = ""
}

variable "target_group_arn" {
description = "The ALB target group for the service."
type = "string"
type = string
}

variable "logentries_token" {
description = "The Logentries token used to be able to get logs sent to a specific log set."
type = "string"
type = string
default = ""
}

variable "task_role_policy" {
description = "IAM policy document to apply to the tasks via a task role"
type = "string"
type = string

default = <<END
{
Expand All @@ -97,35 +97,36 @@ variable "task_role_policy" {
]
}
END

}

variable "assume_role_policy" {
description = "A valid IAM policy for assuming roles - optional"
type = "string"
type = string
default = ""
}

variable "taskdef_volume" {
description = "Map containing 'name' and 'host_path' used to add a volume mapping to the taskdef."
type = "map"
type = map(string)
default = {}
}

variable "container_mountpoint" {
description = "Map containing 'sourceVolume', 'containerPath' and 'readOnly' (optional) to map a volume into a container."
type = "map"
type = map(string)
default = {}
}

variable "container_port_mappings" {
description = "JSON document containing an array of port mappings for the container defintion - if set port is ignored (optional)."
default = ""
type = "string"
type = string
}

variable "container_labels" {
description = "Additional docker labels to apply to the container."
type = "map"
type = map(string)
default = {}
}

Expand Down Expand Up @@ -166,12 +167,13 @@ variable "overnight_scaledown_end_hour" {

variable "application_secrets" {
description = "A list of application specific secret names that can be found in aws secrets manager"
type = "list"
type = list(string)
default = []
}

variable "platform_secrets" {
description = "A list of common secret names for \"the platform\" that can be found in secrets manager"
type = "list"
type = list(string)
default = []
}

Loading

0 comments on commit 50c1a90

Please sign in to comment.