创建LB侦听器时出现CertificateNotfound错误

时间:2018-07-10 17:12:33

标签: amazon-web-services amazon-ec2 terraform

我无法在LB侦听器中添加证书。这是用于执行此操作的代码(请注意,这些是代码段):

global / main.tf

  resource "aws_acm_certificate" "demo_cert_east" {
  provider          = "aws.east"
  domain_name       = "*.mydomain.com"
  validation_method = "DNS"

  tags {
    Name        = "demo certificate"
    Environment = "demo"
  }

  lifecycle {
    create_before_destroy = true
  }
}

stage / main.tf

data "aws_acm_certificate" "demo_cert" {
  domain   = "*.mydomain.com"
  statuses = ["ISSUED", "PENDING_VALIDATION"]
}

resource "aws_lb_listener" "wfe_demo_ssl" {
  load_balancer_arn = "${aws_lb.wfe_demo.arn}"
  port              = "443"
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-2016-08"
  certificate_arn   = "${data.aws_acm_certificate.demo_cert.arn}"

  default_action {
    target_group_arn = "${aws_lb_target_group.wfe_demo.arn}"
    type             = "forward"
  }
}

我确保这两种资源都在aws-east地区。我收到错误消息:

  

创建LB侦听器时出错:CertificateNotFound:未找到证书'arn:aws:acm:us-east-1:078395932517:certificate / b83ba534-ef9d-4a07-ae13-832695dc8b5a'。

因此,数据源已正确检索到证书,但是侦听器似乎找不到它。

2 个答案:

答案 0 :(得分:3)

因为我刚遇到问题:证书和资源必须位于同一区域。 (@ydaetskcoR已经提到了这一点。)

答案 1 :(得分:2)

要能够将ACM证书附加到负载均衡器或其他AWS资源(例如Cloudfront),必须先对其进行验证。

只需更改数据源以仅查找ISSUED证书,然后在没有与您的模式匹配的经过验证的证书的情况下,应将错误推送到数据源中:

data "aws_acm_certificate" "demo_cert" {
  domain   = "*.mydomain.com"
  statuses = ["ISSUED"]
}

要验证证书,您可以在请求时手动进行带外处理,使用其他工具自动为您验证,也可以在创建ACM证书请求时使用Terraform的aws_acm_certificate_validation resource

resource "aws_acm_certificate" "cert" {
  domain_name = "example.com"
  validation_method = "DNS"
}

data "aws_route53_zone" "zone" {
  name = "example.com."
  private_zone = false
}

resource "aws_route53_record" "cert_validation" {
  name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
  type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
  zone_id = "${data.aws_route53_zone.zone.id}"
  records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
  ttl = 60
}

resource "aws_acm_certificate_validation" "cert" {
  certificate_arn = "${aws_acm_certificate.cert.arn}"
  validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"]
}