Skip to content

Commit

Permalink
#82 add resources for blue green deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
kunduso committed Oct 3, 2024
1 parent 91361e4 commit fa1e73b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
50 changes: 50 additions & 0 deletions deploy/code_deploy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_app
resource "aws_codedeploy_app" "application_main" {
compute_platform = "ECS"
name = var.name
}

#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/codedeploy_deployment_group
resource "aws_codedeploy_deployment_group" "application_main" {
app_name = aws_codedeploy_app.application_main.name
deployment_group_name = "${var.name}-deploy-group"
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
service_role_arn = aws_iam_role.codedeploy.arn
blue_green_deployment_config {
deployment_ready_option {
action_on_timeout = "CONTINUE_DEPLOYMENT"
}
terminate_blue_instances_on_deployment_success {
action = "TERMINATE"
termination_wait_time_in_minutes = 1
}
}
ecs_service {
cluster_name = local.infra_output["aws_ecs_cluster_name"]
service_name = aws_ecs_service.service.name
}
deployment_style {
deployment_option = "WITH_TRAFFIC_CONTROL"
deployment_type = "BLUE_GREEN"
}
auto_rollback_configuration {
enabled = true
events = ["DEPLOYMENT_FAILURE"]
}
load_balancer_info {
target_group_pair_info {
prod_traffic_route {
listener_arns = [local.infra_output["aws_lb_listener"]]
}
target_group {
name = local.infra_output["aws_lb_blue_target_group"]
}
target_group {
name = local.infra_output["aws_lb_green_target_group"]
}
}
}
lifecycle {
ignore_changes = [blue_green_deployment_config]
}
}
7 changes: 4 additions & 3 deletions deploy/code_deploy_role.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document
data "aws_iam_policy_document" "assume_by_codedeploy" {
statement {
sid = ""
effect = "Allow"
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["codedeploy.amazonaws.com"]
}
}
}

#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role
resource "aws_iam_role" "codedeploy" {
name = "${var.name}-code-deploy-role"
assume_role_policy = data.aws_iam_policy_document.assume_by_codedeploy.json
}


#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document
data "aws_iam_policy_document" "codedeploy" {
statement {
sid = "AllowLoadBalancingAndECSModifications"
Expand Down Expand Up @@ -76,6 +76,7 @@ data "aws_iam_policy_document" "codedeploy" {
]
}
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy
resource "aws_iam_role_policy" "codedeploy" {
role = aws_iam_role.codedeploy.name
policy = data.aws_iam_policy_document.codedeploy.json
Expand Down
3 changes: 3 additions & 0 deletions deploy/ecs_service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ resource "aws_ecs_service" "service" {
subnets = local.infra_output["subnet_ids"]
assign_public_ip = false
}
deployment_controller {
type = "CODE_DEPLOY"
}
}

0 comments on commit fa1e73b

Please sign in to comment.