79694047

Date: 2025-07-08 10:12:50
Score: 2.5
Natty:
Report link

thank you so much for your solution, I follow your solution, but always get error when try to create deploy app

# AWS CodeDeploy blue/green application and deployment group

# IAM role for CodeDeploy
data "aws_iam_policy_document" "codedeploy_assume_role" {
  statement {
    effect = "Allow"
    principals {
      type        = "Service"
      identifiers = ["codedeploy.amazonaws.com"]
    }
    actions = ["sts:AssumeRole"]
  }
}

resource "aws_iam_role" "codedeploy" {
  name               = "${var.base_name}-codedeploy-role"
  assume_role_policy = data.aws_iam_policy_document.codedeploy_assume_role.json
}

resource "aws_iam_role_policy_attachment" "codedeploy_service" {
  role       = aws_iam_role.codedeploy.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole"
}

# CodeDeploy application
resource "aws_codedeploy_app" "bluegreen" {
  name             = "${var.base_name}-codedeploy-app"
  compute_platform = "Server"
}

# CodeDeploy deployment group
resource "aws_codedeploy_deployment_group" "bluegreen" {
  app_name              = aws_codedeploy_app.bluegreen.name
  deployment_group_name = "${var.base_name}-bluegreen-dg"
  service_role_arn      = aws_iam_role.codedeploy.arn
  deployment_config_name = "CodeDeployDefault.AllAtOnce"

  deployment_style {
    deployment_type   = "BLUE_GREEN"
    deployment_option = "WITH_TRAFFIC_CONTROL"
  }

  load_balancer_info {
    target_group_pair_info {
      prod_traffic_route {
        listener_arns = [var.prod_listener_arn]
      }

      test_traffic_route {
        listener_arns = [var.test_listener_arn]
      }
      
      target_group {
        name = data.aws_lb_target_group.blue.name
        # arn  = data.aws_lb_target_group.blue.arn
      }

      target_group {
        name = data.aws_lb_target_group.green.name
        # arn  = data.aws_lb_target_group.green.arn
      }
    }
}

  autoscaling_groups = [
    var.blue_asg_name,
    var.green_asg_name,
  ]

  blue_green_deployment_config {
    deployment_ready_option {
      action_on_timeout = "CONTINUE_DEPLOYMENT"
    }
    green_fleet_provisioning_option {
    #   action = "COPY_AUTO_SCALING_GROUP"
        action = "DISCOVER_EXISTING"
    }
    terminate_blue_instances_on_deployment_success {
      action                          = "TERMINATE"
      termination_wait_time_in_minutes = 5
    }
  }

  auto_rollback_configuration {
    enabled = true
    events  = ["DEPLOYMENT_FAILURE"]
  }

  depends_on = [aws_iam_role_policy_attachment.codedeploy_service]
}

# Data sources for the blue and green ALB target groups
data "aws_lb_target_group" "blue" {
  name = var.blue_tg_name
}

data "aws_lb_target_group" "green" {
  name = var.green_tg_name
}

# Debug outputs
output "blue_tg_info" {
  value = data.aws_lb_target_group.blue
}

output "green_tg_info" {
  value = data.aws_lb_target_group.green
}

output "asg_info" {
  value = var.green_asg_name
}

and the error

$ terragrunt apply
INFO[0005] Downloading Terraform configurations from file:///home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC into /home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC/non-prod/ap-northeast-1/cloud_qc/codedeploy/.terragrunt-cache/8oSkZEgW4QC-Cp76Tua2Cl8nT2U/gGv3eEtvBft_C1hxVM5RhtucZMg 
Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 6.0.0"...
- Installing hashicorp/aws v6.0.0...
- Installed hashicorp/aws v6.0.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
data.aws_lb_target_group.green: Reading...
data.aws_iam_policy_document.codedeploy_assume_role: Reading...
data.aws_lb_target_group.blue: Reading...
aws_codedeploy_app.bluegreen: Refreshing state... [id=48d7cc00-af33-4443-872d-0eebdb0aeba5:cloud-cloud-qc-codedeploy-app]
data.aws_iam_policy_document.codedeploy_assume_role: Read complete after 0s [id=4250039221]
aws_iam_role.codedeploy: Refreshing state... [id=cloud-cloud-qc-codedeploy-role]
data.aws_lb_target_group.blue: Read complete after 0s [id=arn:aws:elasticloadbalancing:ap-northeast-1:553137501913:targetgroup/cloud-cloud-qc-blue-tg/6cd5ba0e31e504a9]
data.aws_lb_target_group.green: Read complete after 0s [id=arn:aws:elasticloadbalancing:ap-northeast-1:553137501913:targetgroup/cloud-cloud-qc-green-tg/f02e16da413ba528]
aws_iam_role_policy_attachment.codedeploy_service: Refreshing state... [id=cloud-cloud-qc-codedeploy-role-20250708032614888900000001]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_codedeploy_deployment_group.bluegreen will be created
  + resource "aws_codedeploy_deployment_group" "bluegreen" {
      + app_name                    = "cloud-cloud-qc-codedeploy-app"
      + arn                         = (known after apply)
      + autoscaling_groups          = [
          + "cloud-cloud-qc-blue-asg",
          + "cloud-cloud-qc-green-asg",
        ]
      + compute_platform            = (known after apply)
      + deployment_config_name      = "CodeDeployDefault.AllAtOnce"
      + deployment_group_id         = (known after apply)
      + deployment_group_name       = "cloud-cloud-qc-bluegreen-dg"
      + id                          = (known after apply)
      + outdated_instances_strategy = "UPDATE"
      + region                      = "ap-northeast-1"
      + service_role_arn            = "arn:aws:iam::553137501913:role/cloud-cloud-qc-codedeploy-role"
      + tags_all                    = (known after apply)
      + termination_hook_enabled    = false

      + auto_rollback_configuration {
          + enabled = true
          + events  = [
              + "DEPLOYMENT_FAILURE",
            ]
        }

      + blue_green_deployment_config {
          + deployment_ready_option {
              + action_on_timeout = "CONTINUE_DEPLOYMENT"
            }
          + green_fleet_provisioning_option {
              + action = "DISCOVER_EXISTING"
            }
          + terminate_blue_instances_on_deployment_success {
              + action                           = "TERMINATE"
              + termination_wait_time_in_minutes = 5
            }
        }

      + deployment_style {
          + deployment_option = "WITH_TRAFFIC_CONTROL"
          + deployment_type   = "BLUE_GREEN"
        }

      + load_balancer_info {
          + target_group_pair_info {
              + prod_traffic_route {
                  + listener_arns = [
                      + "arn:aws:elasticloadbalancing:ap-northeast-1:553137501913:listener/app/cloud-cloud-qc-alb/9314f6ccb72ed9a4/204a8b3c82c99e93",
                    ]
                }
              + target_group {
                  + name = "cloud-cloud-qc-blue-tg"
                }
              + target_group {
                  + name = "cloud-cloud-qc-green-tg"
                }
              + test_traffic_route {
                  + listener_arns = [
                      + "arn:aws:elasticloadbalancing:ap-northeast-1:553137501913:listener/app/cloud-cloud-qc-alb/9314f6ccb72ed9a4/a12459070bc8e21d",
                    ]
                }
            }
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_codedeploy_deployment_group.bluegreen: Creating...
╷
│ Error: creating CodeDeploy Deployment Group (cloud-cloud-qc-bluegreen-dg): operation error CodeDeploy: CreateDeploymentGroup, https response error StatusCode: 400, RequestID: 0ef49bcc-06db-49e2-b579-d24e99d1cad4, InvalidLoadBalancerInfoException: The specification for load balancing in the deployment group is invalid. The deploymentOption value is set to WITH_TRAFFIC_CONTROL, but either no load balancer was specified in elbInfoList or no target group was specified in targetGroupInfoList.
│ 
│   with aws_codedeploy_deployment_group.bluegreen,
│   on main.tf line 32, in resource "aws_codedeploy_deployment_group" "bluegreen":
│   32: resource "aws_codedeploy_deployment_group" "bluegreen" {
│ 
╵
ERRO[0031] terraform invocation failed in /home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC/non-prod/ap-northeast-1/cloud_qc/codedeploy/.terragrunt-cache/8oSkZEgW4QC-Cp76Tua2Cl8nT2U/gGv3eEtvBft_C1hxVM5RhtucZMg/modules/cloud/codedeploy  error=[/home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC/non-prod/ap-northeast-1/cloud_qc/codedeploy/.terragrunt-cache/8oSkZEgW4QC-Cp76Tua2Cl8nT2U/gGv3eEtvBft_C1hxVM5RhtucZMg/modules/cloud/codedeploy] exit status 1 prefix=[/home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC/non-prod/ap-northeast-1/cloud_qc/codedeploy] 
ERRO[0031] 1 error occurred:
        * [/home/freedom/00_work/biz/Cloud-VMS-Auto-Deploy_vscode/IASecurityIaC/non-prod/ap-northeast-1/cloud_qc/codedeploy/.terragrunt-cache/8oSkZEgW4QC-Cp76Tua2Cl8nT2U/gGv3eEtvBft_C1hxVM5RhtucZMg/modules/cloud/codedeploy] exit status 1

could you share your aws_codedeploy_deployment_group terraform code

aws_codedeploy_deployment_group
aws_codedeploy_deployment_group
Reasons:
  • Blacklisted phrase (0.5): thank you
  • RegEx Blacklisted phrase (2.5): could you share your
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Nguyễn Sơn