Skip to content

Commit

Permalink
FEAT: Allow user to pass in existing target group if any
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumit Sarkar committed Jul 3, 2019
1 parent 535ff4a commit d5b4590
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ resource "aws_ecs_service" "main" {
}

load_balancer {
target_group_arn = aws_alb_target_group.main.id
target_group_arn = var.alb_target_group_id_override == "" ? aws_alb_target_group.main.id : var.alb_target_group_id_override
container_name = var.container_name
container_port = var.port
}

depends_on = [aws_alb_listener_rule.attach_listener]
}

resource "aws_alb_target_group" "main" {
count = var.alb_target_group_id_override == "" ? 1 : 0
name = "tg${var.environment}${var.service_name}"

health_check {
Expand All @@ -68,7 +67,7 @@ resource "aws_alb_target_group" "main" {
}

resource "aws_alb_listener_rule" "attach_listener" {
count = length(var.alb_listener_rule_arns)
count = var.alb_target_group_id_override == "" ? length(var.alb_listener_rule_arns) : 0

priority = var.rule_priority

Expand All @@ -91,7 +90,9 @@ resource "aws_alb_listener_rule" "attach_listener" {
#
# Application AutoScaling resources
#

resource "aws_appautoscaling_target" "main" {
count = var.disable_auto_scaling ? 1 : 0
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
Expand All @@ -102,6 +103,7 @@ resource "aws_appautoscaling_target" "main" {
}

resource "aws_appautoscaling_policy" "up" {
count = var.disable_auto_scaling ? 1 : 0
name = "appScalingPolicy${var.environment}${title(var.service_name)}ScaleUp"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
Expand All @@ -122,6 +124,7 @@ resource "aws_appautoscaling_policy" "up" {
}

resource "aws_appautoscaling_policy" "down" {
count = var.disable_auto_scaling ? 1 : 0
name = "appScalingPolicy${title(var.environment)}${title(var.service_name)}ScaleDown"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
Expand Down
10 changes: 10 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ variable "task_definition_arn" {

variable "alb_listener_rule_arns" {
type = list(string)
default = []
}

variable "rule_type" {
Expand Down Expand Up @@ -140,3 +141,12 @@ variable "placement_constraints" {
]
}

variable "disable_auto_scaling" {
description = "Setting this setting to true will result in not creating auto scaling policies for the service"
default = false
}

variable "alb_target_group_id_override" {
description = "User has the option to provide a AWS Target Group ID"
default = ""
}

0 comments on commit d5b4590

Please sign in to comment.