Skip to content

Commit

Permalink
feat: adds dedicated ces service
Browse files Browse the repository at this point in the history
  • Loading branch information
jimenamorazu committed May 30, 2022
1 parent 6685e1e commit 2077f43
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 142 deletions.
16 changes: 4 additions & 12 deletions infrastructure/modules/developerTools/deployment/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
resource "aws_codedeploy_app" "app" {
compute_platform = "ECS"
name = var.application_name
}

resource "aws_codedeploy_deployment_group" "app" {
app_name = aws_codedeploy_app.app.name
app_name = var.codedeploy_app_name
deployment_config_name = "CodeDeployDefault.ECSCanary10Percent5Minutes"
deployment_group_name = "${var.application_name}-dpg"
deployment_group_name = "${var.application_name}-${var.identifier}"
service_role_arn = aws_iam_role.code_deploy.arn

auto_rollback_configuration {
Expand Down Expand Up @@ -38,10 +33,7 @@ resource "aws_codedeploy_deployment_group" "app" {
load_balancer_info {
target_group_pair_info {
prod_traffic_route {
listener_arns = [var.production_listener_arn]
}
test_traffic_route {
listener_arns = var.listener_arns
listener_arns = var.listener_arn
}
target_group {
name = var.primary_target_group
Expand All @@ -56,7 +48,7 @@ resource "aws_codedeploy_deployment_group" "app" {


resource "aws_iam_role" "code_deploy" {
name = "${var.application_name}-CodeDeploy"
name = "${var.application_name}-${var.identifier}-CodeDeploy"

assume_role_policy = <<EOF
{
Expand Down
6 changes: 1 addition & 5 deletions infrastructure/modules/developerTools/deployment/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
output "deployment_application_name" {
value = aws_codedeploy_app.app.name
}

output "deployment_group_name" {
value = "${var.application_name}-dpg"
value = "${var.application_name}-${var.identifier}"
}
6 changes: 4 additions & 2 deletions infrastructure/modules/developerTools/deployment/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ variable "application_name" {

}

variable "listener_arns" {
variable "listener_arn" {

}

Expand All @@ -22,4 +22,6 @@ variable "ecs_service" {

}

variable "production_listener_arn" {}
variable "identifier" {}

variable "codedeploy_app_name" {}
50 changes: 26 additions & 24 deletions infrastructure/modules/developerTools/pipeline/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,37 @@ resource "aws_codepipeline" "deploy" {
}
}



stage {
name = "Deploy"

action {
name = "Deploy"
category = "Deploy"
owner = "AWS"
provider = "CodeDeployToECS"
input_artifacts = [
"SourceArtifact",
"MyImage"
]
version = "1"

configuration = {
"AppSpecTemplateArtifact" = "SourceArtifact"
"AppSpecTemplatePath" = "appspec.yml"
"ApplicationName" = var.codedeploy_app_name
"DeploymentGroupName" = var.codedeploy_group_name
"Image1ArtifactName" = "MyImage"
"Image1ContainerName" = "IMAGE_NAME"
"TaskDefinitionTemplateArtifact" = "SourceArtifact"
"TaskDefinitionTemplatePath" = "taskdef.json"
}
dynamic "action" {
for_each = var.codedeploy_group_names
content {
name = "Deploy${index(var.codedeploy_group_names, action.value) + 1}"
category = "Deploy"
owner = "AWS"
provider = "CodeDeployToECS"
input_artifacts = [
"SourceArtifact",
"MyImage"
]
version = "1"

configuration = {
"AppSpecTemplateArtifact" = "SourceArtifact"
"AppSpecTemplatePath" = "appspec.yml"
"ApplicationName" = var.codedeploy_app_name
"DeploymentGroupName" = action.value
"Image1ArtifactName" = "MyImage"
"Image1ContainerName" = "IMAGE_NAME"
"TaskDefinitionTemplateArtifact" = "SourceArtifact"
"TaskDefinitionTemplatePath" = "taskdef.json"
}
}
}

}


}

resource "aws_iam_role" "codepipeline_role" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ variable "codebuid_project_name" {
variable "codedeploy_app_name" {

}
variable "codedeploy_group_name" {
variable "codedeploy_group_names" {

}

Expand Down
37 changes: 5 additions & 32 deletions infrastructure/modules/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data "aws_ecs_cluster" "service" {
resource "aws_lb_target_group" "primary" {
name = "${var.service_name}-main-tg"
port = var.container_port
protocol = var.protocol
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"

Expand Down Expand Up @@ -33,7 +33,7 @@ resource "aws_lb_target_group" "primary" {
resource "aws_lb_target_group" "secondary" {
name = "${var.service_name}-scd-tg"
port = var.container_port
protocol = var.protocol
protocol = "HTTP"
vpc_id = var.vpc_id
target_type = "ip"

Expand All @@ -56,8 +56,8 @@ resource "aws_lb_target_group" "secondary" {
}
}

resource "aws_lb_listener_rule" "main_http" {
listener_arn = var.http_alb_listener_arn
resource "aws_lb_listener_rule" "main" {
listener_arn = var.alb_listener_arn

action {
type = "forward"
Expand All @@ -66,28 +66,7 @@ resource "aws_lb_listener_rule" "main_http" {

condition {
host_header {
values = ["${var.service_name}.dereedere.link"]
}
}

lifecycle {
ignore_changes = [
action
]
}
}

resource "aws_lb_listener_rule" "main_https" {
listener_arn = var.https_alb_listener_arn

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

condition {
host_header {
values = ["${var.service_name}.dereedere.link"]
values = ["timeoff-app.dereedere.link"]
}
}

Expand Down Expand Up @@ -117,12 +96,6 @@ resource "aws_ecs_service" "main" {
container_port = var.container_port
}

load_balancer {
target_group_arn = aws_lb_target_group.secondary.arn
container_name = var.container_name
container_port = var.container_port
}

network_configuration {
security_groups = [module.sg.security_group_id]
subnets = var.subnets
Expand Down
9 changes: 1 addition & 8 deletions infrastructure/modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@ variable "container_name" {
variable "container_port" {
}

variable "http_alb_listener_arn" {
variable "alb_listener_arn" {

}

variable "https_alb_listener_arn" {

}

variable "protocol" {
}

variable "subnets" {
}

Expand Down
149 changes: 91 additions & 58 deletions infrastructure/timeoff-app/main.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,98 @@
module "timeoff_service" {
source = "../modules/service"
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
cluster_name = "core"
service_name = local.name
task_definition_name = "timeoff:8"
desired_count = 1
container_name = local.name
container_port = 3000
protocol = "HTTP"
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
subnets = data.terraform_remote_state.global.outputs.private_subnets
http_alb_listener_arn = data.terraform_remote_state.global.outputs.http_listener_arn
https_alb_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
module "timeoff_service_http" {
source = "../modules/service"
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
cluster_name = "core"
service_name ="${local.name}-http"
task_definition_name = "timeoff:8"
desired_count = 1
container_name = local.name
container_port = 3000
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
subnets = data.terraform_remote_state.global.outputs.private_subnets
alb_listener_arn = data.terraform_remote_state.global.outputs.http_listener_arn
}

module "timeoff_service_https" {
source = "../modules/service"
vpc_id = data.terraform_remote_state.global.outputs.vpc_id
cluster_name = "core"
service_name = "${local.name}-https"
task_definition_name = "timeoff:8"
desired_count = 1
container_name = local.name
container_port = 3000
alb_security_group_id = data.terraform_remote_state.global.outputs.alb_security_group
subnets = data.terraform_remote_state.global.outputs.private_subnets
alb_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
}

module "timeoff_build" {
source = "../modules/developerTools/build"
application_name = local.name
environment_variables = [
source = "../modules/developerTools/build"
application_name = local.name
environment_variables = local.build_variables
source_version = "develop"
github_repository_url = "https://github.com/jimenamorazu/timeoff-management-application.git"
github_token_ssm_path = "/CodeBuild/Github/access_token"
}

resource "aws_codedeploy_app" "app" {
compute_platform = "ECS"
name = local.name
}

module "timeoff_deploy_http" {
source = "../modules/developerTools/deployment"

application_name = local.name
listener_arn = [data.terraform_remote_state.global.outputs.http_listener_arn]
primary_target_group = module.timeoff_service_http.primary_target_group
secondary_target_group = module.timeoff_service_http.secondary_target_group
ecs_cluster = "core"
ecs_service = module.timeoff_service_http.service_name
codedeploy_app_name = aws_codedeploy_app.app.name
identifier = "http"

}

module "timeoff_deploy_https" {
source = "../modules/developerTools/deployment"

application_name = local.name
listener_arn = [data.terraform_remote_state.global.outputs.http_listener_arn]
primary_target_group = module.timeoff_service_https.primary_target_group
secondary_target_group = module.timeoff_service_https.secondary_target_group
ecs_cluster = "core"
ecs_service = module.timeoff_service_https.service_name
codedeploy_app_name = aws_codedeploy_app.app.name
identifier = "https"
}

module "timeoff_pipeline" {
source = "../modules/developerTools/pipeline"

application_name = local.name
codestar_connection = data.terraform_remote_state.global.outputs.codestar_connection_arn
repository_id = "jimenamorazu/timeoff-management-application"
source_branch = "develop"
ecr_repository_name = data.terraform_remote_state.global.outputs.ecr_repo_name
codebuid_project_name = module.timeoff_build.project_name
codedeploy_app_name = aws_codedeploy_app.app.name
codedeploy_group_names = [module.timeoff_deploy_http.deployment_group_name, module.timeoff_deploy_https.deployment_group_name]
}


data "terraform_remote_state" "global" {
backend = "s3"
config = {
bucket = "global-terraform-state"
key = "global/network/terraform.tfstate"
region = "us-east-1"
}
}

locals {
name = "timeoff-app"
build_variables = [
{
name = "dockerhub_password"
type = "PARAMETER_STORE"
Expand All @@ -45,45 +119,4 @@ module "timeoff_build" {
value = "150068533141"
}
]
source_version = "develop"
github_repository_url = "https://github.com/jimenamorazu/timeoff-management-application.git"
github_token_ssm_path = "/CodeBuild/Github/access_token"
}

module "timeoff_deploy" {
source = "../modules/developerTools/deployment"

application_name = local.name
production_listener_arn = data.terraform_remote_state.global.outputs.https_listener_arn
listener_arns = [data.terraform_remote_state.global.outputs.http_listener_arn]
primary_target_group = module.timeoff_service.primary_target_group
secondary_target_group = module.timeoff_service.secondary_target_group
ecs_cluster = "core"
ecs_service = module.timeoff_service.service_name
}

module "timeoff_pipeline" {
source = "../modules/developerTools/pipeline"
application_name = local.name
codestar_connection = data.terraform_remote_state.global.outputs.codestar_connection_arn
repository_id = "jimenamorazu/timeoff-management-application"
source_branch = "develop"
ecr_repository_name = data.terraform_remote_state.global.outputs.ecr_repo_name
codebuid_project_name = module.timeoff_build.project_name
codedeploy_app_name = module.timeoff_deploy.deployment_application_name
codedeploy_group_name = module.timeoff_deploy.deployment_group_name
}


data "terraform_remote_state" "global" {
backend = "s3"
config = {
bucket = "global-terraform-state"
key = "global/network/terraform.tfstate"
region = "us-east-1"
}
}

locals {
name = "timeoff-app"
}

0 comments on commit 2077f43

Please sign in to comment.