Skip to content

Commit

Permalink
/deploy sit
Browse files Browse the repository at this point in the history
  • Loading branch information
sliu008 committed Jan 6, 2025
1 parent 6d61428 commit 0951152
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 68 deletions.
144 changes: 78 additions & 66 deletions terraform/fargate/fargate.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
locals {
log_group_name = "${var.prefix}-${var.app_name}-fargate-EcsLogs"
task_family = "${var.prefix}-${var.app_name}-fargate"
service_name = "${var.prefix}-${var.app_name}-fargate-service"
scale_up_name = "${var.prefix}-${var.app_name}-fargate-policy-scale-up"
scale_down_name = "${var.prefix}-${var.app_name}-fargate-policy-scale-down"
alarm_scale_up = "${var.prefix}-${var.app_name}-fargate-alarm-scale-up"
alarm_scale_down = "${var.prefix}-${var.app_name}-fargate-alarm-scale-down"
}

resource "aws_cloudwatch_log_group" "fargate_log_group" {
name = local.log_group_name
resource "aws_cloudwatch_log_group" "fargate_cloudwatch_log_group" {
name = "${var.prefix}-${var.app_name}-fargate-EcsLogs"
retention_in_days = 30
tags = var.tags
}

resource "aws_ecs_task_definition" "app" {
family = local.task_family
family = "${var.prefix}-${var.app_name}-fargate"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.cpu
Expand All @@ -30,29 +20,30 @@ resource "aws_ecs_task_definition" "app" {

container_definitions = jsonencode([
{
name = local.task_family
name = "${var.prefix}-${var.app_name}-fargate"
cpu = var.cpu
essential = true
image = var.image
memoryReservation = var.memory

environment = [for k, v in var.environment : { name = k, value = v }]
command = var.command
ephemeralStorage = var.ephemeralStorageSize
command = var.command
ephemeralStorage = var.ephemeralStorageSize

logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.fargate_log_group.name
awslogs-region = var.region
awslogs-group = aws_cloudwatch_log_group.fargate_cloudwatch_log_group.name
awslogs-region = var.region
awslogs-stream-prefix = "container"
}
}
}
])
}

resource "aws_ecs_service" "fargate_service" {
name = local.service_name
resource "aws_ecs_service" "fargate_ecs_service" {
name = "${var.prefix}-${var.app_name}-fargate-service"
cluster = var.ecs_cluster_arn
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.desired_count
Expand All @@ -64,63 +55,84 @@ resource "aws_ecs_service" "fargate_service" {
}
}

resource "aws_appautoscaling_target" "fargate_target" {
resource "aws_appautoscaling_target" "fargate_ecs_target" {
max_capacity = var.max_capacity
min_capacity = var.min_capacity
resource_id = "service/${var.cluster_name}/${aws_ecs_service.fargate_service.name}"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.fargate_ecs_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_policy" "fargate_scale" {
dynamic "scale_policy" {
for_each = {
scale_up = { name = local.scale_up_name, cooldown = var.scale_up_cooldown, step_adjustment = var.scale_up_step_adjustment }
scale_down = { name = local.scale_down_name, cooldown = var.scale_down_cooldown, step_adjustment = var.scale_down_step_adjustment }
}
content {
name = scale_policy.value.name
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.fargate_target.resource_id
scalable_dimension = aws_appautoscaling_target.fargate_target.scalable_dimension
service_namespace = aws_appautoscaling_target.fargate_target.service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = scale_policy.value.cooldown
metric_aggregation_type = "Average"

dynamic "step_adjustment" {
for_each = scale_policy.value.step_adjustment
content {
metric_interval_lower_bound = lookup(step_adjustment.value, "metric_interval_lower_bound", null)
metric_interval_upper_bound = lookup(step_adjustment.value, "metric_interval_upper_bound", null)
scaling_adjustment = lookup(step_adjustment.value, "scaling_adjustment")
}
}
resource "aws_appautoscaling_policy" "fargate_scale_down" {
name = "${var.prefix}-${var.app_name}-fargate-policy-scale-down"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.fargate_ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.fargate_ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.fargate_ecs_target.service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.scale_down_cooldown
metric_aggregation_type = "Average"

dynamic "step_adjustment" {
for_each = var.scale_down_step_adjustment
content {
metric_interval_lower_bound = lookup(step_adjustment.value, "metric_interval_lower_bound")
metric_interval_upper_bound = lookup(step_adjustment.value, "metric_interval_upper_bound")
scaling_adjustment = lookup(step_adjustment.value, "scaling_adjustment")
}
}
}
}

resource "aws_cloudwatch_metric_alarm" "fargate_alarm" {
dynamic "alarm_config" {
for_each = {
scale_up = { name = local.alarm_scale_up, comparison = var.comparison_operator_scale_up, evaluation_periods = var.evaluation_periods_scale_up, metric_name = var.metric_name_scale_up, namespace = var.namespace_scale_up, period = var.period_scale_up, statistic = var.statistic_scale_up, threshold = var.threshold_scale_up, action = aws_appautoscaling_policy.fargate_scale["scale_up"].arn }
scale_down = { name = local.alarm_scale_down, comparison = var.comparison_operator_scale_down, evaluation_periods = var.evaluation_periods_scale_down, metric_name = var.metric_name_scale_down, namespace = var.namespace_scale_down, period = var.period_scale_down, statistic = var.statistic_scale_down, threshold = var.threshold_scale_down, action = aws_appautoscaling_policy.fargate_scale["scale_down"].arn }
}
content {
alarm_name = alarm_config.value.name
comparison_operator = alarm_config.value.comparison
evaluation_periods = alarm_config.value.evaluation_periods
metric_name = alarm_config.value.metric_name
namespace = alarm_config.value.namespace
period = alarm_config.value.period
statistic = alarm_config.value.statistic
threshold = alarm_config.value.threshold
alarm_description = "This metric monitors ECS Cluster fargate task ${alarm_config.key}"
alarm_actions = [alarm_config.value.action]
dimensions = var.scale_dimensions
resource "aws_appautoscaling_policy" "fargate_scale_up" {
name = "${var.prefix}-${var.app_name}-fargate-policy-scale-up"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.fargate_ecs_target.resource_id
scalable_dimension = aws_appautoscaling_target.fargate_ecs_target.scalable_dimension
service_namespace = aws_appautoscaling_target.fargate_ecs_target.service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.scale_up_cooldown
metric_aggregation_type = "Average"

dynamic "step_adjustment" {
for_each = var.scale_up_step_adjustment
content {
metric_interval_lower_bound = lookup(step_adjustment.value, "metric_interval_lower_bound")
metric_interval_upper_bound = lookup(step_adjustment.value, "metric_interval_upper_bound")
scaling_adjustment = lookup(step_adjustment.value, "scaling_adjustment")
}
}
}
}

resource "aws_cloudwatch_metric_alarm" "scale_up" {
alarm_name = "${var.prefix}-${var.app_name}-fargate-alarm-scale-up"
comparison_operator = var.comparison_operator_scale_up
evaluation_periods = var.evaluation_periods_scale_up
metric_name = var.metric_name_scale_up
namespace = var.namespace_scale_up
period = var.period_scale_up
statistic = var.statistic_scale_up
threshold = var.threshold_scale_up
alarm_description = "This metric monitors ECS Cluster fargate task going up"
alarm_actions = [aws_appautoscaling_policy.fargate_scale_up.arn]
dimensions = var.scale_dimensions
}

resource "aws_cloudwatch_metric_alarm" "scale_down" {
alarm_name = "${var.prefix}-${var.app_name}-fargate-alarm-scale-down"
comparison_operator = var.comparison_operator_scale_down
evaluation_periods = var.evaluation_periods_scale_down
metric_name = var.metric_name_scale_down
namespace = var.namespace_scale_down
period = var.period_scale_down
statistic = var.statistic_scale_down
threshold = var.threshold_scale_down
alarm_description = "This metric monitors ECS Cluster fargate task going down"
alarm_actions = [aws_appautoscaling_policy.fargate_scale_down.arn]
dimensions = var.scale_dimensions
}
3 changes: 1 addition & 2 deletions terraform/forge_py_fargate.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ module "forge_py_fargate" {
"CONFIG_BUCKET": "${var.prefix}-internal",
"CONFIG_DIR" : var.config_dir,
"CONFIG_URL" : var.config_url,
"PALETTE_URL" : var.palette_url
}

image = "${aws_ecr_repository.lambda-image-repo.repository_url}:${local.ecr_image_tag}"
ecs_cluster_arn = var.cluster_arn
ecs_cluster_arn = var.ecs_cluster_arn
subnet_ids = var.subnet_ids
scale_dimensions = var.scale_dimensions != null ? var.scale_dimensions : {"ServiceName" = "${var.prefix}-${var.app_name}-fargate-service","ClusterName" = var.ecs_cluster_name}

Expand Down
5 changes: 5 additions & 0 deletions terraform/variable.tf
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,8 @@ variable "scale_down_step_adjustment" {
}
]
}

variable ecs_cluster_arn{
description = "cluster to run fargate tasks in"
type = string
}

0 comments on commit 0951152

Please sign in to comment.