Skip to content

Commit 2077f43

Browse files
committed
feat: adds dedicated ces service
1 parent 6685e1e commit 2077f43

File tree

8 files changed

+133
-142
lines changed

8 files changed

+133
-142
lines changed

infrastructure/modules/developerTools/deployment/main.tf

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
resource "aws_codedeploy_app" "app" {
2-
compute_platform = "ECS"
3-
name = var.application_name
4-
}
5-
61
resource "aws_codedeploy_deployment_group" "app" {
7-
app_name = aws_codedeploy_app.app.name
2+
app_name = var.codedeploy_app_name
83
deployment_config_name = "CodeDeployDefault.ECSCanary10Percent5Minutes"
9-
deployment_group_name = "${var.application_name}-dpg"
4+
deployment_group_name = "${var.application_name}-${var.identifier}"
105
service_role_arn = aws_iam_role.code_deploy.arn
116

127
auto_rollback_configuration {
@@ -38,10 +33,7 @@ resource "aws_codedeploy_deployment_group" "app" {
3833
load_balancer_info {
3934
target_group_pair_info {
4035
prod_traffic_route {
41-
listener_arns = [var.production_listener_arn]
42-
}
43-
test_traffic_route {
44-
listener_arns = var.listener_arns
36+
listener_arns = var.listener_arn
4537
}
4638
target_group {
4739
name = var.primary_target_group
@@ -56,7 +48,7 @@ resource "aws_codedeploy_deployment_group" "app" {
5648

5749

5850
resource "aws_iam_role" "code_deploy" {
59-
name = "${var.application_name}-CodeDeploy"
51+
name = "${var.application_name}-${var.identifier}-CodeDeploy"
6052

6153
assume_role_policy = <<EOF
6254
{
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
output "deployment_application_name" {
2-
value = aws_codedeploy_app.app.name
3-
}
4-
51
output "deployment_group_name" {
6-
value = "${var.application_name}-dpg"
2+
value = "${var.application_name}-${var.identifier}"
73
}

infrastructure/modules/developerTools/deployment/variables.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ variable "application_name" {
22

33
}
44

5-
variable "listener_arns" {
5+
variable "listener_arn" {
66

77
}
88

@@ -22,4 +22,6 @@ variable "ecs_service" {
2222

2323
}
2424

25-
variable "production_listener_arn" {}
25+
variable "identifier" {}
26+
27+
variable "codedeploy_app_name" {}

infrastructure/modules/developerTools/pipeline/main.tf

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,37 @@ resource "aws_codepipeline" "deploy" {
101101
}
102102
}
103103

104-
105-
106104
stage {
107105
name = "Deploy"
108-
109-
action {
110-
name = "Deploy"
111-
category = "Deploy"
112-
owner = "AWS"
113-
provider = "CodeDeployToECS"
114-
input_artifacts = [
115-
"SourceArtifact",
116-
"MyImage"
117-
]
118-
version = "1"
119-
120-
configuration = {
121-
"AppSpecTemplateArtifact" = "SourceArtifact"
122-
"AppSpecTemplatePath" = "appspec.yml"
123-
"ApplicationName" = var.codedeploy_app_name
124-
"DeploymentGroupName" = var.codedeploy_group_name
125-
"Image1ArtifactName" = "MyImage"
126-
"Image1ContainerName" = "IMAGE_NAME"
127-
"TaskDefinitionTemplateArtifact" = "SourceArtifact"
128-
"TaskDefinitionTemplatePath" = "taskdef.json"
129-
}
106+
dynamic "action" {
107+
for_each = var.codedeploy_group_names
108+
content {
109+
name = "Deploy${index(var.codedeploy_group_names, action.value) + 1}"
110+
category = "Deploy"
111+
owner = "AWS"
112+
provider = "CodeDeployToECS"
113+
input_artifacts = [
114+
"SourceArtifact",
115+
"MyImage"
116+
]
117+
version = "1"
118+
119+
configuration = {
120+
"AppSpecTemplateArtifact" = "SourceArtifact"
121+
"AppSpecTemplatePath" = "appspec.yml"
122+
"ApplicationName" = var.codedeploy_app_name
123+
"DeploymentGroupName" = action.value
124+
"Image1ArtifactName" = "MyImage"
125+
"Image1ContainerName" = "IMAGE_NAME"
126+
"TaskDefinitionTemplateArtifact" = "SourceArtifact"
127+
"TaskDefinitionTemplatePath" = "taskdef.json"
128+
}
130129
}
131130
}
132131

132+
}
133+
134+
133135
}
134136

135137
resource "aws_iam_role" "codepipeline_role" {

infrastructure/modules/developerTools/pipeline/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ variable "codebuid_project_name" {
2323
variable "codedeploy_app_name" {
2424

2525
}
26-
variable "codedeploy_group_name" {
26+
variable "codedeploy_group_names" {
2727

2828
}
2929

infrastructure/modules/service/main.tf

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ data "aws_ecs_cluster" "service" {
55
resource "aws_lb_target_group" "primary" {
66
name = "${var.service_name}-main-tg"
77
port = var.container_port
8-
protocol = var.protocol
8+
protocol = "HTTP"
99
vpc_id = var.vpc_id
1010
target_type = "ip"
1111

@@ -33,7 +33,7 @@ resource "aws_lb_target_group" "primary" {
3333
resource "aws_lb_target_group" "secondary" {
3434
name = "${var.service_name}-scd-tg"
3535
port = var.container_port
36-
protocol = var.protocol
36+
protocol = "HTTP"
3737
vpc_id = var.vpc_id
3838
target_type = "ip"
3939

@@ -56,8 +56,8 @@ resource "aws_lb_target_group" "secondary" {
5656
}
5757
}
5858

59-
resource "aws_lb_listener_rule" "main_http" {
60-
listener_arn = var.http_alb_listener_arn
59+
resource "aws_lb_listener_rule" "main" {
60+
listener_arn = var.alb_listener_arn
6161

6262
action {
6363
type = "forward"
@@ -66,28 +66,7 @@ resource "aws_lb_listener_rule" "main_http" {
6666

6767
condition {
6868
host_header {
69-
values = ["${var.service_name}.dereedere.link"]
70-
}
71-
}
72-
73-
lifecycle {
74-
ignore_changes = [
75-
action
76-
]
77-
}
78-
}
79-
80-
resource "aws_lb_listener_rule" "main_https" {
81-
listener_arn = var.https_alb_listener_arn
82-
83-
action {
84-
type = "forward"
85-
target_group_arn = aws_lb_target_group.primary.arn
86-
}
87-
88-
condition {
89-
host_header {
90-
values = ["${var.service_name}.dereedere.link"]
69+
values = ["timeoff-app.dereedere.link"]
9170
}
9271
}
9372

@@ -117,12 +96,6 @@ resource "aws_ecs_service" "main" {
11796
container_port = var.container_port
11897
}
11998

120-
load_balancer {
121-
target_group_arn = aws_lb_target_group.secondary.arn
122-
container_name = var.container_name
123-
container_port = var.container_port
124-
}
125-
12699
network_configuration {
127100
security_groups = [module.sg.security_group_id]
128101
subnets = var.subnets

infrastructure/modules/service/variables.tf

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,10 @@ variable "container_name" {
2323
variable "container_port" {
2424
}
2525

26-
variable "http_alb_listener_arn" {
26+
variable "alb_listener_arn" {
2727

2828
}
2929

30-
variable "https_alb_listener_arn" {
31-
32-
}
33-
34-
variable "protocol" {
35-
}
36-
3730
variable "subnets" {
3831
}
3932

infrastructure/timeoff-app/main.tf

Lines changed: 91 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,98 @@
1-
module "timeoff_service" {
2-
source = "../modules/service"
3-
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
4-
cluster_name = "core"
5-
service_name = local.name
6-
task_definition_name = "timeoff:8"
7-
desired_count = 1
8-
container_name = local.name
9-
container_port = 3000
10-
protocol = "HTTP"
11-
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
12-
subnets = data.terraform_remote_state.global.outputs.private_subnets
13-
http_alb_listener_arn = data.terraform_remote_state.global.outputs.http_listener_arn
14-
https_alb_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
1+
module "timeoff_service_http" {
2+
source = "../modules/service"
3+
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
4+
cluster_name = "core"
5+
service_name ="${local.name}-http"
6+
task_definition_name = "timeoff:8"
7+
desired_count = 1
8+
container_name = local.name
9+
container_port = 3000
10+
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
11+
subnets = data.terraform_remote_state.global.outputs.private_subnets
12+
alb_listener_arn = data.terraform_remote_state.global.outputs.http_listener_arn
13+
}
1514

15+
module "timeoff_service_https" {
16+
source = "../modules/service"
17+
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
18+
cluster_name = "core"
19+
service_name = "${local.name}-https"
20+
task_definition_name = "timeoff:8"
21+
desired_count = 1
22+
container_name = local.name
23+
container_port = 3000
24+
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
25+
subnets = data.terraform_remote_state.global.outputs.private_subnets
26+
alb_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
1627
}
1728

1829
module "timeoff_build" {
19-
source = "../modules/developerTools/build"
20-
application_name = local.name
21-
environment_variables = [
30+
source = "../modules/developerTools/build"
31+
application_name = local.name
32+
environment_variables = local.build_variables
33+
source_version = "develop"
34+
github_repository_url = "https://github.com/jimenamorazu/timeoff-management-application.git"
35+
github_token_ssm_path = "/CodeBuild/Github/access_token"
36+
}
37+
38+
resource "aws_codedeploy_app" "app" {
39+
compute_platform = "ECS"
40+
name = local.name
41+
}
42+
43+
module "timeoff_deploy_http" {
44+
source = "../modules/developerTools/deployment"
45+
46+
application_name = local.name
47+
listener_arn = [data.terraform_remote_state.global.outputs.http_listener_arn]
48+
primary_target_group = module.timeoff_service_http.primary_target_group
49+
secondary_target_group = module.timeoff_service_http.secondary_target_group
50+
ecs_cluster = "core"
51+
ecs_service = module.timeoff_service_http.service_name
52+
codedeploy_app_name = aws_codedeploy_app.app.name
53+
identifier = "http"
54+
55+
}
56+
57+
module "timeoff_deploy_https" {
58+
source = "../modules/developerTools/deployment"
59+
60+
application_name = local.name
61+
listener_arn = [data.terraform_remote_state.global.outputs.http_listener_arn]
62+
primary_target_group = module.timeoff_service_https.primary_target_group
63+
secondary_target_group = module.timeoff_service_https.secondary_target_group
64+
ecs_cluster = "core"
65+
ecs_service = module.timeoff_service_https.service_name
66+
codedeploy_app_name = aws_codedeploy_app.app.name
67+
identifier = "https"
68+
}
69+
70+
module "timeoff_pipeline" {
71+
source = "../modules/developerTools/pipeline"
72+
73+
application_name = local.name
74+
codestar_connection = data.terraform_remote_state.global.outputs.codestar_connection_arn
75+
repository_id = "jimenamorazu/timeoff-management-application"
76+
source_branch = "develop"
77+
ecr_repository_name = data.terraform_remote_state.global.outputs.ecr_repo_name
78+
codebuid_project_name = module.timeoff_build.project_name
79+
codedeploy_app_name = aws_codedeploy_app.app.name
80+
codedeploy_group_names = [module.timeoff_deploy_http.deployment_group_name, module.timeoff_deploy_https.deployment_group_name]
81+
}
82+
83+
84+
data "terraform_remote_state" "global" {
85+
backend = "s3"
86+
config = {
87+
bucket = "global-terraform-state"
88+
key = "global/network/terraform.tfstate"
89+
region = "us-east-1"
90+
}
91+
}
92+
93+
locals {
94+
name = "timeoff-app"
95+
build_variables = [
2296
{
2397
name = "dockerhub_password"
2498
type = "PARAMETER_STORE"
@@ -45,45 +119,4 @@ module "timeoff_build" {
45119
value = "150068533141"
46120
}
47121
]
48-
source_version = "develop"
49-
github_repository_url = "https://github.com/jimenamorazu/timeoff-management-application.git"
50-
github_token_ssm_path = "/CodeBuild/Github/access_token"
51-
}
52-
53-
module "timeoff_deploy" {
54-
source = "../modules/developerTools/deployment"
55-
56-
application_name = local.name
57-
production_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
58-
listener_arns = [data.terraform_remote_state.global.outputs.http_listener_arn]
59-
primary_target_group = module.timeoff_service.primary_target_group
60-
secondary_target_group = module.timeoff_service.secondary_target_group
61-
ecs_cluster = "core"
62-
ecs_service = module.timeoff_service.service_name
63-
}
64-
65-
module "timeoff_pipeline" {
66-
source = "../modules/developerTools/pipeline"
67-
application_name = local.name
68-
codestar_connection = data.terraform_remote_state.global.outputs.codestar_connection_arn
69-
repository_id = "jimenamorazu/timeoff-management-application"
70-
source_branch = "develop"
71-
ecr_repository_name = data.terraform_remote_state.global.outputs.ecr_repo_name
72-
codebuid_project_name = module.timeoff_build.project_name
73-
codedeploy_app_name = module.timeoff_deploy.deployment_application_name
74-
codedeploy_group_name = module.timeoff_deploy.deployment_group_name
75-
}
76-
77-
78-
data "terraform_remote_state" "global" {
79-
backend = "s3"
80-
config = {
81-
bucket = "global-terraform-state"
82-
key = "global/network/terraform.tfstate"
83-
region = "us-east-1"
84-
}
85-
}
86-
87-
locals {
88-
name = "timeoff-app"
89122
}

0 commit comments

Comments
 (0)