需要使用单一地形创建两个 ALB 模块

Need to create two ALB modules using single terraform

我是 Terraform 的新手,我有一种情况需要将一个源代码部署到两个不同的 EC2 中。

意思是我正在创建模块 ALB。如果我创建单个 ALB 模块,我很好。当我尝试尝试其他 ALB 时,我正在破坏问题并以“多个证书问题”结束

任何人都可以举例说明使用相同的证书创建两个不同名称的 ALb 吗?

下面是供参考的代码片段:

module "alb" {
  namespace                           = terraform.workspace
  name                                = join("-", [local.name, "asg"])
  vpc_cidr_code                       = lookup(var.awsacct_cidr_code, var.environment)
  autoscaling                         = true
  internal                            = true
  autoscale_inst_type                 = "m5.large"
  autoscale_min_size                  = 2
  autoscale_max_size                  = 3
  certificate_arn                     = data.aws_acm_certificate.xxx.arn
  backend_lb_port                     = 9443
  backend_lb_protocol                 = "HTTPS"
  health_check_monitor_path           = "/"
  autoscale_health_check_grace_period = 600
  health_check_interval               = 300
  health_check_timeout                = 120
  health_check_matcher                = "200-499"
  stickiness_enabled                  = false
  tags                                = module.namespace.tags
  autoscale_user_data = templatefile("{path.module}/user-data.tmpl",
    {
      environment         = var.ENV_PROFILE,
      keyarn              = module.aws_ssl.certificate.arn
      commitid            = var.COMMIT_ID
      secret              = var.nodekey_secret[var.environment]
      bucketname          = var.deploy_bucket[var.environment]
      bucketobject        = local.name
      ally_application_id = var.ally_application_id
    }
  )
}

module "alb-jobs" {
  namespace                           = terraform.workspace
 name                                = join("-", [local.name1, "asg"])
  vpc_cidr_code                       = lookup(var.awsacct_cidr_code, var.environment)
  autoscaling                         = true
  internal                            = true
  autoscale_inst_type                 = "m5.large"
  autoscale_min_size                  = 1
  autoscale_max_size                  = 1
  certificate_arn                     = data.aws_acm_certificate.xxx.arn
  backend_lb_port                     = 9443
  backend_lb_protocol                 = "HTTPS"
  health_check_monitor_path           = "/"
  autoscale_health_check_grace_period = 600
  health_check_interval               = 300
  health_check_timeout                = 120
  health_check_matcher                = "200-499"
  stickiness_enabled                  = false
 tags                                = module.namespace.tags
  autoscale_user_data = templatefile("{path.module}/user-data-jobs.tmpl",
    {
      environment         = var.ENV_PROFILE,
      keyarn              = module.aws_ssl.certificate.arn
      commitid            = var.COMMIT_ID
      secret              = var.nodekey_secret[var.environment]
      bucketname          = var.deploy_bucket[var.environment]
      bucketobject        = local.name1
      ally_application_id = var.ally_application_id
    }
  )
}

Eg:

data "aws_acm_certificate" "xxx" {
  domain   = lookup(var.awsacct_domain, var.environment)
  statuses = ["ISSUED"]
}

variable.tf file

awsacct_domain = {
  default       = "*.dev.xxx.com"
  dev           = "*.dev.xxx.com"
  non-prod-dev  = "*.dev.xxx.com"
  qa            = "*.qa.xxx.com"
  cap           = "*.cap.xxx.com"
  psp           = "*.psp.xxx.com"
  prod          = "*.prod.xxx.com"
}

Getting Error:

Error: Multiple certificates for domain "*.dev.xxx.com" found in this region
229 on main.tf line 5, in data "aws_acm_certificate" "xxx":
230 5: data "aws_acm_certificate" "xxx" { 

请帮助我了解更多。

提前致谢, 巴拉.

错误信息很清楚。您为该特定域拥有多个证书,因此由于歧义,它不能只选择一个。您可能必须传递额外的参数才能选择特定的证书。也许您也可以尝试通过 certificate type

或者您可以考虑删除任何未使用的证书以消除歧义。

我在上面的代码中犯了错误,我试图为数据源创建两个条目,因此出现多个证书错误。当我尝试重用数据源时,它开始正常工作。感谢大家让我明白。