Skip to content

Commit

Permalink
Removing code-deploy and enabling circuit-breaker
Browse files Browse the repository at this point in the history
  • Loading branch information
adenot committed Jul 19, 2024
1 parent a9c5206 commit 9777ed4
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 376 deletions.
35 changes: 0 additions & 35 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ variable "service_role_arn" {
default = null
}

variable "codedeploy_role_arn" {
default = null
description = "Existing IAM CodeDeploy role ARN created by ECS cluster module"
}

variable "task_role_arn" {
description = "Existing task role ARN created by ECS cluster module"
default = null
Expand Down Expand Up @@ -140,10 +135,6 @@ variable "alb_listener_https_arn" {
description = "ALB HTTPS Listener created by ECS cluster module"
}

variable "test_traffic_route_listener_arn" {
description = "ALB HTTPS Listener for Test Traffic created by ECS cluster module"
}

variable "alb_dns_name" {
description = "ALB DNS Name"
default = ""
Expand Down Expand Up @@ -244,21 +235,6 @@ variable "alb_only" {
description = "Whether to deploy only an alb and no cloudFront or not with the cluster"
}

variable "codedeploy_wait_time_for_cutover" {
default = 0
description = "Time in minutes to route the traffic to the new application deployment"
}

variable "codedeploy_wait_time_for_termination" {
default = 0
description = "Time in minutes to terminate the new deployment"
}

variable "codedeploy_deployment_config_name" {
default = "CodeDeployDefault.ECSAllAtOnce"
description = "Specifies the deployment configuration for CodeDeploy"
}

variable "cloudwatch_logs_create" {
default = true
description = "Whether to create cloudwatch log resources or not"
Expand Down Expand Up @@ -356,12 +332,6 @@ variable "placement_constraints" {
default = []
}

variable "create_iam_codedeployrole" {
type = bool
default = true
description = "Create Codedeploy IAM Role for ECS or not."
}

variable "alarm_prefix" {
type = string
description = "String prefix for cloudwatch alarms. (Optional)"
Expand Down Expand Up @@ -481,11 +451,6 @@ variable "redirects" {
default = {}
}

variable "deployment_controller" {
default = "CODE_DEPLOY"
description = "Type of deployment controller. Valid values: CODE_DEPLOY, ECS, EXTERNAL."
}

variable "ecs_service_capacity_provider_strategy" {
description = "(Optional) The capacity provider strategy to use for the service. Can be one or more. These can be updated without destroying and recreating the service only if set to [] and not changing from 0 capacity_provider_strategy blocks to greater than 0, or vice versa."
default = [{}]
Expand Down
111 changes: 7 additions & 104 deletions alb-target-group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,21 @@ resource "aws_lb_listener_rule" "green" {
dynamic "condition" {
for_each = length(var.paths) > 0 ? [var.paths] : []
content {
path_pattern {
values = toset(condition.value)
}
path_pattern { values = toset(condition.value) }
}
}

dynamic "condition" {
for_each = length(var.hostnames) > 0 ? [var.hostnames] : []
content {
host_header {
values = toset(condition.value)
}
host_header { values = toset(condition.value) }
}
}

dynamic "condition" {
for_each = length(var.source_ips) > 0 ? [var.source_ips] : []
content {
source_ip {
values = toset(condition.value)
}
source_ip { values = toset(condition.value) }
}
}

Expand All @@ -44,9 +38,8 @@ resource "aws_lb_listener_rule" "green" {
}

lifecycle {
ignore_changes = [
action[0].target_group_arn
]
ignore_changes = [action[0].target_group_arn]
replace_triggered_by = [aws_lb_target_group.green]
}

priority = try(
Expand All @@ -56,55 +49,7 @@ resource "aws_lb_listener_rule" "green" {
)
)

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}

resource "aws_lb_listener_rule" "blue" {
listener_arn = var.test_traffic_route_listener_arn

action {
type = "forward"
target_group_arn = aws_lb_target_group.blue.arn
}

dynamic "condition" {
for_each = length(var.paths) > 0 ? [var.paths] : []
content {
path_pattern {
values = toset(condition.value)
}
}
}

dynamic "condition" {
for_each = length(var.hostnames) > 0 ? [var.hostnames] : []
content {
host_header {
values = toset(condition.value)
}
}
}

lifecycle {
ignore_changes = [
action[0].target_group_arn
]
}

priority = var.alb_priority != 0 ? var.alb_priority + 1 : null

tags = merge(
var.tags,
{
"Terraform" = true
},
)

tags = merge(var.tags, { "Terraform" = true }, )
}

resource "aws_lb_listener_rule" "redirects" {
Expand Down Expand Up @@ -200,47 +145,5 @@ resource "aws_lb_target_group" "green" {
}
}

tags = merge(
var.tags,
{
"Terraform" = true
},
)
tags = merge(var.tags, { "Terraform" = true }, )
}

resource "aws_lb_target_group" "blue" {
name = var.compat_keep_target_group_naming ? "${var.cluster_name}-${var.name}-bl" : format("%s-bl-%s", substr("${var.cluster_name}-${replace(var.name, "_", "-")}", 0, 24), random_string.alb_prefix.result)
port = var.port
protocol = var.protocol
vpc_id = var.vpc_id
deregistration_delay = 10
target_type = var.launch_type == "FARGATE" ? "ip" : "instance"

health_check {
path = var.healthcheck_path
interval = var.healthcheck_interval
healthy_threshold = var.healthy_threshold
unhealthy_threshold = var.unhealthy_threshold
timeout = var.healthcheck_timeout
matcher = var.healthcheck_matcher
protocol = var.protocol
}

dynamic "stickiness" {
for_each = var.dynamic_stickiness
iterator = stickiness

content {
cookie_duration = stickiness.value.cookie_duration
cookie_name = stickiness.value.cookie_name
type = stickiness.value.type
}
}

tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
84 changes: 20 additions & 64 deletions cloudwatch-alarms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,18 @@ resource "aws_cloudwatch_metric_alarm" "min_healthy_tasks" {
insufficient_data_actions = []
treat_missing_data = "ignore"

tags = merge(
var.tags,
{
"Terraform" = true
},
)

metric_query {
id = "e1"
expression = "MAX(REMOVE_EMPTY([m1, m2]))"
label = "HealthyHostCountCombined"
return_data = "true"
}

metric_query {
id = "m1"

metric {
metric_name = "HealthyHostCount"
namespace = "AWS/ApplicationELB"
period = "60"
stat = "Maximum"
unit = "Count"

dimensions = {
LoadBalancer = join("/", slice(split("/", data.aws_lb_listener.ecs.load_balancer_arn), 1, 4))
TargetGroup = aws_lb_target_group.blue.arn_suffix
}
}

metric_name = "HealthyHostCount"
namespace = "AWS/ApplicationELB"
period = "60"
statistic = "Maximum"
unit = "Count"

dimensions = {
LoadBalancer = join("/", slice(split("/", data.aws_lb_listener.ecs.load_balancer_arn), 1, 4))
TargetGroup = aws_lb_target_group.green.arn_suffix
}

metric_query {
id = "m2"

metric {
metric_name = "HealthyHostCount"
namespace = "AWS/ApplicationELB"
period = "60"
stat = "Maximum"
unit = "Count"

dimensions = {
LoadBalancer = join("/", slice(split("/", data.aws_lb_listener.ecs.load_balancer_arn), 1, 4))
TargetGroup = aws_lb_target_group.green.arn_suffix
}
}
}
tags = merge(var.tags, { "Terraform" = true }, )
}

resource "aws_cloudwatch_metric_alarm" "high_cpu_usage" {
Expand All @@ -81,43 +44,36 @@ resource "aws_cloudwatch_metric_alarm" "high_cpu_usage" {
statistic = "Average"
unit = "Percent"

tags = merge(
var.tags,
{
"Terraform" = true
},
)
dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.default.name
}

tags = merge(var.tags, { "Terraform" = true }, )
}

resource "aws_cloudwatch_metric_alarm" "ecs_running_tasks" {
count = length(var.alarm_sns_topics) > 0 && var.alarm_ecs_running_tasks_threshold > 0 ? 1 : 0

alarm_name = "${try(data.aws_iam_account_alias.current[0].account_alias, var.alarm_prefix)}-ecs-${var.name}-running-tasks"
alarm_description = "Ecs service running tasks is lower than the threshold"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "RunningTaskCount"
namespace = "ECS/ContainerInsights"
period = "30"
statistic = "Average"
threshold = var.alarm_ecs_running_tasks_threshold
alarm_description = "Ecs service running tasks is lower than the threshold"
alarm_actions = var.alarm_sns_topics
ok_actions = var.alarm_sns_topics
insufficient_data_actions = []
treat_missing_data = "ignore"
tags = merge(
var.tags,
{
"Terraform" = true
},
)

metric_name = "RunningTaskCount"
namespace = "ECS/ContainerInsights"
period = "30"
statistic = "Average"

dimensions = {
ClusterName = var.cluster_name
ServiceName = aws_ecs_service.default.name
}
}

tags = merge(var.tags, { "Terraform" = true }, )
}
Loading

0 comments on commit 9777ed4

Please sign in to comment.