Skip to content
Permalink
Browse files

More inline docs

  • Loading branch information
maartenvanderhoef committed Jun 11, 2018
1 parent 4101930 commit 38389a0e532a5273c9a4a8215f3991233a088ffb
Showing with 145 additions and 234 deletions.
  1. +1 −1 LICENSE
  2. +117 −111 main.tf
  3. +7 −61 modules/alb_handling/main.tf
  4. +1 −1 modules/ecs_autoscaling/main.tf
  5. +16 −56 modules/iam/main.tf
  6. +3 −4 outputs.tf
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Blinkist
Copyright (c) 2018 Maarten van der Hoef Blinkist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
228 main.tf
@@ -12,7 +12,7 @@ locals {
fargate_enabled = "${lookup(var.ecs_properties,"service_launch_type", "EC2") == "FARGATE" ? true : false }"
launch_type = "${local.fargate_enabled ? "FARGATE" : "EC2" }"

awsvpc_enabled = "${length(var.awsvpc_subnets) > 0 ? 1 : 0}"
awsvpc_enabled = "${length(var.awsvpc_subnets) > 0 ? true : false }"
}

#
@@ -25,7 +25,8 @@ module "iam" {
name = "${local.cluster_name}-${var.name}"

# Create defines if any resources need to be created inside the module
create = "${var.create ? 1 : 0}"
#create = "${var.create}"
create = true

# Region is used multiple times inside this module, we pass this through so that we don't need multiple datasources
region = "${local.region}"
@@ -58,6 +59,9 @@ module "alb_handling" {
# Create defines if we need to create resources inside this module
create = "${var.create}"

# lb_vpc_id sets the VPC ID of where the LB resides
lb_vpc_id = "${lookup(var.load_balancing_properties,"lb_vpc_id", "")}"

# lb_arn defines the arn of the ALB
lb_arn = "${lookup(var.load_balancing_properties,"lb_arn", "")}"

@@ -83,117 +87,119 @@ module "alb_handling" {
target_type = "${local.awsvpc_enabled ? "ip" : "instance"}"
}

###### CloudWatch Logs
resource "aws_cloudwatch_log_group" "app" {
count = "${var.create}"
name = "${local.cluster_name}/${var.name}"
retention_in_days = 14
}

#
# This sub-module creates the ECS Task definition
#
module "ecs_task_definition" {
source = "./modules/ecs_task_definition/"

# The name of the task_definition ( generally, a combination of the cluster name and the service name.)
name = "${local.cluster_name}-${var.name}"
cluster_name = "${local.cluster_name}"

# container_properties defines a list of maps of container_properties
container_properties = "${var.container_properties}"

# awsvpc_enabled sets if the ecs task definition is awsvpc
awsvpc_enabled = "${local.awsvpc_enabled}"

# fargate_enabled sets if the ecs task definition has launch_type FARGATE
fargate_enabled = "${local.fargate_enabled}"

# cloudwatch_loggroup_name sets the loggroup name of the cloudwatch loggroup made for this service.
cloudwatch_loggroup_name = "${aws_cloudwatch_log_group.app.name}"

# container_envvars defines a list of maps filled with key-val pairs of environment variables needed for the ecs task definition.
container_envvars = "${var.container_envvars}"

# ecs_taskrole_arn sets the IAM role of the task.
ecs_taskrole_arn = "${module.iam.ecs_taskrole_arn}"

# ecs_task_execution_role_arn sets the task-execution role needed for FARGATE. This role is also empty in case of EC2
ecs_task_execution_role_arn = "${module.iam.ecs_task_execution_role_arn}"

# Launch type, either EC2 or FARGATE
launch_type = "${local.launch_type}"

# region, needed for Logging..
region = "${local.region}"
}

####### CloudWatch Logs
#resource "aws_cloudwatch_log_group" "app" {
# count = "${var.create}"
# name = "${local.cluster_name}/${var.name}"
# retention_in_days = 14
#}
#
# This sub-module creates the ECS Service
#
module "ecs_service" {
source = "./modules/ecs_service/"
name = "${local.cluster_name}-${var.name}"

# create defines if resources are being created inside this module
create = "${var.create}"

cluster_id = "${local.cluster_id}"

# ecs_task_definition_arn is the arn of the task definition, created by the ecs_task_definition module
ecs_task_definition_arn = "${module.ecs_task_definition.aws_ecs_task_definition_arn}"

# launch_type either EC2 or FARGATE
launch_type = "${local.launch_type}"

# deployment_maximum_percent sets the maximum size of the deployment in % of the normal size.
deployment_maximum_percent = "${lookup(var.capacity_properties,"deployment_maximum_percent", var.default_capacity_properties_deployment_maximum_percent)}"

# deployment_minimum_healthy_percent sets the minimum % in capacity at depployment
deployment_minimum_healthy_percent = "${lookup(var.capacity_properties,"deployment_minimum_healthy_percent", var.default_capacity_properties_deployment_minimum_healthy_percent)}"

# awsvpc_subnets defines the subnets for an awsvpc ecs module
awsvpc_subnets = "${var.awsvpc_subnets}"

# awsvpc_security_group_ids defines the vpc_security_group_ids for an awsvpc module
awsvpc_security_group_ids = "${var.awsvpc_security_group_ids}"

# lb_create sets if the ECS Service will be LB connected
alb_connected = "${module.alb_handling.target_group_created}"

# lb_target_group_arn sets the arn of the target_group the service needs to connect to
lb_target_group_arn = "${module.alb_handling.lb_target_group_arn}"

# desired_capacity sets the initial capacity in task of the ECS Service
desired_capacity = "${lookup(var.capacity_properties,"desired_capacity", var.default_capacity_properties_desired_capacity)}"

# container_name sets the name of the container, this is used for the load balancer section inside the ecs_service to connect to a container_name defined inside the
# task definition, container_port sets the port for the same container.
container_name = "${module.ecs_task_definition.container0_name}"

container_port = "${module.ecs_task_definition.container0_port}"
}

##
## This sub-module creates the ECS Task definition
##
#module "ecs_task_definition" {
# source = "./modules/ecs_task_definition/"
#
# This modules sets the scaling properties of the ECS Service
# # The name of the task_definition ( generally, a combination of the cluster name and the service name.)
# name = "${local.cluster_name}-${var.name}"
# cluster_name = "${local.cluster_name}"
#
module "ecs_autoscaling" {
source = "./modules/ecs_autoscaling/"

# create defines if resources inside this module are being created.
create = "${(var.create ? 1 : 0 ) * (length(var.scaling_properties) > 0 ? 1 : 0 )}"

cluster_name = "${local.cluster_name}"

# ecs_service_name is derived from the actual ecs_service, this to force dependency at creation.
ecs_service_name = "${module.ecs_service.ecs_service_name}"

# desired_min_capacity, in case of autoscaling, desired_min_capacity sets the minimum size in tasks
desired_min_capacity = "${lookup(var.capacity_properties,"desired_min_capacity", var.default_capacity_properties_desired_min_capacity)}"

# desired_max_capaity, in case of autoscaling, desired_max_capacity sets the maximum size in tasks
desired_max_capacity = "${lookup(var.capacity_properties,"desired_max_capacity", var.default_capacity_properties_desired_max_capacity)}"
# # container_properties defines a list of maps of container_properties
# container_properties = "${var.container_properties}"
#
# # awsvpc_enabled sets if the ecs task definition is awsvpc
# awsvpc_enabled = "${local.awsvpc_enabled}"
#
# # fargate_enabled sets if the ecs task definition has launch_type FARGATE
# fargate_enabled = "${local.fargate_enabled}"
#
# # cloudwatch_loggroup_name sets the loggroup name of the cloudwatch loggroup made for this service.
# cloudwatch_loggroup_name = "${aws_cloudwatch_log_group.app.name}"
#
# # container_envvars defines a list of maps filled with key-val pairs of environment variables needed for the ecs task definition.
# container_envvars = "${var.container_envvars}"
#
# # ecs_taskrole_arn sets the IAM role of the task.
# ecs_taskrole_arn = "${module.iam.ecs_taskrole_arn}"
#
# # ecs_task_execution_role_arn sets the task-execution role needed for FARGATE. This role is also empty in case of EC2
# ecs_task_execution_role_arn = "${module.iam.ecs_task_execution_role_arn}"
#
# # Launch type, either EC2 or FARGATE
# launch_type = "${local.launch_type}"
#
# # region, needed for Logging..
# region = "${local.region}"
#}
#
##
## This sub-module creates the ECS Service
##
#module "ecs_service" {
# source = "./modules/ecs_service/"
# name = "${local.cluster_name}-${var.name}"
#
# # create defines if resources are being created inside this module
# create = "${var.create}"
#
# cluster_id = "${local.cluster_id}"
#
# # ecs_task_definition_arn is the arn of the task definition, created by the ecs_task_definition module
# ecs_task_definition_arn = "${module.ecs_task_definition.aws_ecs_task_definition_arn}"
#
# # launch_type either EC2 or FARGATE
# launch_type = "${local.launch_type}"
#
# # deployment_maximum_percent sets the maximum size of the deployment in % of the normal size.
# deployment_maximum_percent = "${lookup(var.capacity_properties,"deployment_maximum_percent", var.default_capacity_properties_deployment_maximum_percent)}"
#
# # deployment_minimum_healthy_percent sets the minimum % in capacity at depployment
# deployment_minimum_healthy_percent = "${lookup(var.capacity_properties,"deployment_minimum_healthy_percent", var.default_capacity_properties_deployment_minimum_healthy_percent)}"
#
# # awsvpc_subnets defines the subnets for an awsvpc ecs module
# awsvpc_subnets = "${var.awsvpc_subnets}"
#
# # awsvpc_security_group_ids defines the vpc_security_group_ids for an awsvpc module
# awsvpc_security_group_ids = "${var.awsvpc_security_group_ids}"
#
# # lb_create sets if the ECS Service will be LB connected
# alb_connected = "${module.alb_handling.target_group_created}"
#
# # lb_target_group_arn sets the arn of the target_group the service needs to connect to
# lb_target_group_arn = "${module.alb_handling.lb_target_group_arn}"
#
# # desired_capacity sets the initial capacity in task of the ECS Service
# desired_capacity = "${lookup(var.capacity_properties,"desired_capacity", var.default_capacity_properties_desired_capacity)}"
#
# # container_name sets the name of the container, this is used for the load balancer section inside the ecs_service to connect to a container_name defined inside the
# # task definition, container_port sets the port for the same container.
# container_name = "${module.ecs_task_definition.container0_name}"
#
# container_port = "${module.ecs_task_definition.container0_port}"
#}
#
##
## This modules sets the scaling properties of the ECS Service
##
#module "ecs_autoscaling" {
# source = "./modules/ecs_autoscaling/"
#
# # create defines if resources inside this module are being created.
# create = "${(var.create ? 1 : 0 ) * (length(var.scaling_properties) > 0 ? 1 : 0 )}"
#
# cluster_name = "${local.cluster_name}"
#
# # ecs_service_name is derived from the actual ecs_service, this to force dependency at creation.
# ecs_service_name = "${module.ecs_service.ecs_service_name}"
#
# # desired_min_capacity, in case of autoscaling, desired_min_capacity sets the minimum size in tasks
# desired_min_capacity = "${lookup(var.capacity_properties,"desired_min_capacity", var.default_capacity_properties_desired_min_capacity)}"
#
# # desired_max_capaity, in case of autoscaling, desired_max_capacity sets the maximum size in tasks
# desired_max_capacity = "${lookup(var.capacity_properties,"desired_max_capacity", var.default_capacity_properties_desired_max_capacity)}"
#
# # scaling_properties holds a list of maps with the scaling properties defined.
# scaling_properties = "${var.scaling_properties}"
#}

# scaling_properties holds a list of maps with the scaling properties defined.
scaling_properties = "${var.scaling_properties}"
}
@@ -1,79 +1,25 @@
variable "route53_zone_id" {
default = ""
}

variable "route53_name" {
default = ""
}

variable "unhealthy_threshold" {}

variable "cluster_name" {
default = ""
}

variable "name" {
default = ""
}

variable "lb_arn" {
default = ""
}

variable "lb_listener_arn" {
default = ""
}

variable "lb_listener_arn_https" {
default = ""
}

variable "target_type" {
default = ""
}

variable "lb_vpc_id" {
default = ""
}

variable "health_uri" {
default = ""
}

variable "create_route53_zone" {
default = true
}

variable "custom_listen_host" {
default = ""
}

variable "create" {
default = true
}

locals {
# We only craete when var.create is true and a LB ARN is given
create = "${(var.create ? 1 : 0 ) * ( length(var.lb_arn) > 0 ? 1 : 0)}"
}

output "create" {
value = "${local.create}"
create_as_count = "${(var.create == true ? 1 : 0 ) * ( length(var.lb_arn) > 0 ? 1 : 0)}"
}

data "aws_route53_zone" "selected" {
count = "${(local.create == true ? 1 : 0) * ( var.create_route53_zone == true ? 1 : 0 )}"
count = "${local.create_as_count * ( var.create_route53_zone == true ? 1 : 0 )}"
zone_id = "${var.route53_zone_id}"
}

data "aws_lb" "main" {
count = "${var.create}"
count = "${local.create_as_count}"
arn = "${var.lb_arn}"
}

## Route53 DNS Record
resource "aws_route53_record" "record" {
count = "${(local.create == true ? 1 : 0) * ( var.create_route53_zone == true ? 1 : 0 )}"
count = "${local.create_as_count * ( var.create_route53_record == true ? 1 : 0 )}"
zone_id = "${data.aws_route53_zone.selected.zone_id}"
name = "${var.route53_name}"
type = "CNAME"
@@ -85,7 +31,7 @@ resource "aws_route53_record" "record" {
## aws_lb_target_group inside the ECS Task will be created when the service is not the default forwarding service
## It will not be created when the service is not attached to a load balancer like a worker
resource "aws_lb_target_group" "service" {
count = "${local.create}"
count = "${local.create_as_count}"
name = "${var.cluster_name}-${var.name}"
port = 80
protocol = "HTTP"
@@ -101,7 +47,7 @@ resource "aws_lb_target_group" "service" {
##
## An aws_lb_listener_rule will only be created when a service has a load balancer attached
resource "aws_lb_listener_rule" "host_based_routing" {
count = "${local.create}"
count = "${local.create_as_count}"

listener_arn = "${var.lb_listener_arn}"

@@ -119,7 +65,7 @@ resource "aws_lb_listener_rule" "host_based_routing" {
##
## An aws_lb_listener_rule will only be created when a service has a load balancer attached
resource "aws_lb_listener_rule" "host_based_routing-ssl" {
count = "${local.create}"
count = "${local.create_as_count}"

listener_arn = "${var.lb_listener_arn_https}"

@@ -39,7 +39,7 @@ resource "aws_appautoscaling_policy" "policy" {
name = "${local.cluster_plus_service_name}-${lookup(var.scaling_properties[count.index], "type")}-${element(var.direction[lookup(var.scaling_properties[count.index], "direction")],1)}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${var.ecs_service_name}"
resource_id = "service/${var.cluster_name}/${var.ecs_service_name}"
step_scaling_policy_configuration {
adjustment_type = "${lookup(var.scaling_properties[count.index], "adjustment_type")}"
cooldown = "${lookup(var.scaling_properties[count.index], "cooldown")}"

0 comments on commit 38389a0

Please sign in to comment.
You can’t perform that action at this time.