Skip to content

Commit

Permalink
Use EC2 instead of Fargate
Browse files Browse the repository at this point in the history
  • Loading branch information
vertism committed Dec 13, 2023
1 parent bf3857f commit a3d2223
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
18 changes: 15 additions & 3 deletions terraform/environments/cdpt-chaps/application_variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
"accounts": {
"development": {
"region": "eu-west-2",
"docker_image_tag": "development"
"docker_image_tag": "development",
"app_count": 1,
"ec2_desired_capacity": 2,
"ec2_max_size": 3,
"ec2_min_size": 2
},
"preproduction": {
"region": "eu-west-2",
"docker_image_tag": "preproduction"
"docker_image_tag": "preproduction",
"app_count": 1,
"ec2_desired_capacity": 2,
"ec2_max_size": 3,
"ec2_min_size": 2
},
"production": {
"region": "eu-west-2",
"docker_image_tag": "production"
"docker_image_tag": "production",
"app_count": 1,
"ec2_desired_capacity": 2,
"ec2_max_size": 3,
"ec2_min_size": 2
}
}
}
52 changes: 48 additions & 4 deletions terraform/environments/cdpt-chaps/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resource "aws_cloudwatch_log_group" "deployment_logs" {

resource "aws_ecs_task_definition" "chaps_task_definition" {
family = "chapsFamily"
requires_compatibilities = ["FARGATE"]
requires_compatibilities = ["EC2"]
network_mode = "awsvpc"
execution_role_arn = aws_iam_role.app_execution.arn
task_role_arn = aws_iam_role.app_task.arn
Expand Down Expand Up @@ -64,11 +64,15 @@ resource "aws_ecs_service" "ecs_service" {
name = var.networking[0].application
cluster = aws_ecs_cluster.ecs_cluster.id
task_definition = aws_ecs_task_definition.chaps_task_definition.arn
launch_type = "FARGATE"
enable_execute_command = true
desired_count = 2
launch_type = "EC2"
desired_count = local.application_data.accounts[local.environment].app_count
health_check_grace_period_seconds = 180

capacity_provider_strategy {
capacity_provider = aws_ecs_capacity_provider.cdpt-chaps.name
weight = 1
}

network_configuration {
subnets = data.aws_subnets.shared-public.ids
security_groups = [aws_security_group.ecs_service.id]
Expand All @@ -82,6 +86,46 @@ resource "aws_ecs_service" "ecs_service" {
}
}

resource "aws_ecs_capacity_provider" "cdpt-chaps" {
name = "${local.application_name}-capacity-provider"

auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.cluster-scaling-group.arn
managed_termination_protection = "ENABLED"

managed_scaling {
status = "ENABLED"
target_capacity = 100
}
}
}

resource "aws_ecs_cluster_capacity_providers" "cdpt-chaps" {
cluster_name = aws_ecs_cluster.ecs_cluster.name

capacity_providers = [aws_ecs_capacity_provider.apex.name]
}

resource "aws_autoscaling_group" "cluster-scaling-group" {
vpc_zone_identifier = sort(data.aws_subnets.shared-private.ids)
name = "${local.application_name}-cluster-scaling-group"
desired_capacity = local.application_data.accounts[local.environment].ec2_desired_capacity
max_size = local.application_data.accounts[local.environment].ec2_max_size
min_size = local.application_data.accounts[local.environment].ec2_min_size
protect_from_scale_in = true

launch_template {
id = aws_launch_template.ec2-launch-template.id
version = "$Latest"
}

tag {
key = "AmazonECSManaged"
value = true
propagate_at_launch = true
}
}

resource "aws_iam_role" "app_execution" {
name = "execution-${var.networking[0].application}"

Expand Down

0 comments on commit a3d2223

Please sign in to comment.