Skip to content

Commit e10c18f

Browse files
authored
feat: code deploy integration, alb, tg, listener, force deployment auto (#4)
* refactor: dir service resource * feat: tg group foreach to set * test^: first deploy - blue green details * fix: target groups - fall test * featÃ: include logs in 120 seconds * fix: codedeploy application - deployment group * fix: s3 depend application codedeploy * fix: s3 depend application codedeploy * feat: health check in container * feat: codedeploy group * feat: codedeploy, canary deployment native | fix: ecs service, scaling group
1 parent b72dfdb commit e10c18f

22 files changed

+244
-30
lines changed

app/src/app/Main.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package app
21
import java.util.*
32
import java.util.concurrent.Executors
43
import java.util.concurrent.TimeUnit
5-
import app.startHealthServer
64

75
var total = 0
86

app/src/app/healthCheck.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ fun startHealthServer() {
1414
}
1515
server.executor = null
1616
server.start()
17-
println("Servidor iniciado na porta 80")
17+
println("Servidor iniciado na porta 8080")
1818
}

infra/application_load_balancer.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resource "aws_lb" "this" {
2+
depends_on = [aws_lb_target_group.this, aws_ecs_task_definition.this]
3+
4+
name = "alb-${var.service_name}"
5+
internal = true
6+
load_balancer_type = "application"
7+
security_groups = [data.aws_security_group.this.id]
8+
subnets = data.aws_subnets.this.ids
9+
10+
enable_deletion_protection = false
11+
}

infra/code-deploy_application.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "aws_codedeploy_app" "this" {
2+
compute_platform = "ECS"
3+
name = local.project_name
4+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "aws_codedeploy_deployment_group" "this" {
2+
depends_on = [aws_ecs_task_definition.this, aws_s3_bucket.this]
3+
app_name = aws_codedeploy_app.this.name
4+
deployment_config_name = "CodeDeployDefault.ECSCanary10Percent5Minutes"
5+
deployment_group_name = local.project_name
6+
service_role_arn = data.aws_iam_role.this.arn
7+
8+
deployment_style {
9+
deployment_option = "WITH_TRAFFIC_CONTROL"
10+
deployment_type = "BLUE_GREEN"
11+
}
12+
13+
blue_green_deployment_config {
14+
terminate_blue_instances_on_deployment_success {
15+
action = "TERMINATE"
16+
termination_wait_time_in_minutes = 5
17+
}
18+
19+
deployment_ready_option {
20+
action_on_timeout = "CONTINUE_DEPLOYMENT"
21+
}
22+
}
23+
24+
auto_rollback_configuration {
25+
enabled = true
26+
events = ["DEPLOYMENT_FAILURE"]
27+
}
28+
29+
ecs_service {
30+
cluster_name = aws_ecs_cluster.this.name
31+
service_name = aws_ecs_service.this.name
32+
}
33+
34+
load_balancer_info {
35+
target_group_pair_info {
36+
prod_traffic_route {
37+
listener_arns = [aws_lb_listener.this.arn]
38+
}
39+
40+
target_group {
41+
name =aws_lb_target_group.this["blue"].name
42+
}
43+
44+
target_group {
45+
name =aws_lb_target_group.this["green"].name
46+
}
47+
}
48+
}
49+
50+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 0.0
2+
Resources:
3+
- TargetService:
4+
Type: AWS::ECS::Service
5+
Properties:
6+
TaskDefinition: "${TASK_DEFINITION_ARN}"
7+
LoadBalancerInfo:
8+
ContainerName: "${CONTAINER_NAME}"
9+
ContainerPort: "${CONTAINER_PORT}"

infra/data.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
data "aws_region" "current" {}
2+
13
data "aws_iam_role" "this" {
24
name = "ecs-tasks-demo"
35
}
46

57
data "aws_ecr_image" "this" {
6-
repository_name = "demo/kotlin-app-canary"
8+
repository_name = var.ecr_repository
79
most_recent = true
810
}
911

infra/ecs_cluster.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
resource "aws_ecs_cluster" "this" {
2-
depends_on = [aws_cloudwatch_log_group.this]
32
name = var.ecs_cluster_name
43

54
setting {
65
name = "containerInsights"
76
value = var.env == "dev" ? "disabled" : "enabled"
87
}
8+
}
99

10+
resource "aws_ecs_cluster_capacity_providers" "this" {
11+
cluster_name = aws_ecs_cluster.this.name
12+
13+
capacity_providers = ["FARGATE"]
14+
15+
default_capacity_provider_strategy {
16+
base = 1
17+
weight = 100
18+
capacity_provider = "FARGATE"
19+
}
1020
}

infra/ecs_service.tf

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
resource "aws_ecs_service" "this" {
2-
depends_on = [aws_ecs_cluster.this, aws_ecs_task_definition.this]
2+
depends_on = [aws_ecs_cluster.this, aws_ecs_task_definition.this, aws_lb.this]
33

4-
name = var.service_name
5-
cluster = aws_ecs_cluster.this.id
4+
name = var.service_name
5+
cluster = aws_ecs_cluster.this.id
66
task_definition = aws_ecs_task_definition.this.arn
7-
desired_count = var.env == "dev" ? 1 : 0
7+
desired_count = 1
88
launch_type = null
99
scheduling_strategy = "REPLICA"
10-
platform_version = "LATEST"
1110
enable_ecs_managed_tags = true
11+
health_check_grace_period_seconds = 60
1212

1313
capacity_provider_strategy {
1414
capacity_provider = "FARGATE_SPOT"
@@ -17,7 +17,7 @@ resource "aws_ecs_service" "this" {
1717
}
1818

1919
deployment_controller {
20-
type = "ECS"
20+
type = "CODE_DEPLOY"
2121
}
2222

2323
deployment_circuit_breaker {
@@ -31,4 +31,9 @@ resource "aws_ecs_service" "this" {
3131
assign_public_ip = true
3232
}
3333

34-
}
34+
load_balancer {
35+
target_group_arn = aws_lb_target_group.this["blue"].arn
36+
container_name = "container-${local.project_name}"
37+
container_port = 8080
38+
}
39+
}

infra/force-deploy.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/env bash
2+
3+
echo "Iniciando verificação de status de deployment..."
4+
5+
DEPLOYMENT_ID=$(aws deploy list-deployments --application-name $APPLICATION_NAME --deployment-group-name $DEPLOYMENT_GROUP_NAME --query 'deployments[0]' --output text)
6+
7+
if [ "$DEPLOYMENT_ID" != "None" ]; then
8+
STATUS=$(aws deploy get-deployment --deployment-id $DEPLOYMENT_ID --query 'deploymentInfo.status' --output text)
9+
10+
if [ "$STATUS" == "InProgress" ]; then
11+
echo "Já existe um deployment em andamento (ID: $DEPLOYMENT_ID). Um novo deployment não será iniciado."
12+
exit 0
13+
else
14+
echo "Sem deploy em andamento. Iniciando novo deployment..."
15+
fi
16+
else
17+
echo "Sem deploy em andamento. Iniciando novo deployment..."
18+
fi
19+
20+
DEPLOYMENT_ID=$(aws deploy create-deployment \
21+
--application-name $APPLICATION_NAME \
22+
--deployment-group-name $DEPLOYMENT_GROUP_NAME \
23+
--revision "{\"revisionType\": \"S3\", \"s3Location\": {\"bucket\": \"$S3_APPSPEC\", \"key\": \"appspec.yaml\", \"bundleType\": \"yaml\"}}" \
24+
--output text --query deploymentId)
25+
26+
echo "ID do deployment: $DEPLOYMENT_ID"
27+
28+
rm -rf ./codedeploy/appspec.yaml

0 commit comments

Comments
 (0)