Terraform (AWS):如何通过负载均衡器的 ARN 获取负载均衡器的 DNS 名称?

n0r*_*m4l 4 lambda amazon-web-services terraform

我创建了一个 Terraform 脚本,该脚本部署 Lambda 函数和目标组,并将其分配给变量中定义的现有负载均衡器:

variable "load_balancer_arn" {
    default = "arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxxx:loadbalancer/app/xx-test/xxxxxxxxxxx"
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以通过输出获取此负载均衡器的 DNS 名称吗?

Jam*_*den 8

aws_lbNLB 和 ALB lb 数据资源的数据源采用arn:

data "aws_lb" "test" {
  arn  = "${var.lb_arn}"
}
Run Code Online (Sandbox Code Playgroud)

这将返回以下负载均衡器属性:

Attributes Reference
The following attributes are exported in addition to the arguments listed above:

id - The ARN of the load balancer (matches arn).
arn - The ARN of the load balancer (matches id).
arn_suffix - The ARN suffix for use with CloudWatch Metrics.
dns_name - The DNS name of the load balancer.
zone_id - The canonical hosted zone ID of the load balancer (to be used in a Route 53 Alias record).
Run Code Online (Sandbox Code Playgroud)

这包括 dns 名称,要使用它:

${data.aws_lb.test.dns_name}
Run Code Online (Sandbox Code Playgroud)

对于最新的 terraform(此时为 tf 0.13),您不使用大括号,因此它变为:

data "aws_lb" "test" {
  arn  = var.lb_arn
}

data.aws_lb.test.dns_name
Run Code Online (Sandbox Code Playgroud)