n0rm4l
n0rm4l

Reputation: 103

Terraform (AWS): How to get DNS name of Load Balancer by its ARN?

I've created a Terraform script which deploys Lambda Function and Target Group, and assigning it to existing Load Balancer which is defined in variables:

variable "load_balancer_arn" {
    default = "arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxxx:loadbalancer/app/xx-test/xxxxxxxxxxx"
}

Is there any way to get DNS name of this Load Balancer with Output?

Upvotes: 4

Views: 10338

Answers (1)

James Woolfenden
James Woolfenden

Reputation: 6671

There is the aws_lb data source for NLBs and ALBs lb data resource that takes the arn:

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

This returns the following load balancer attributes:

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).

This include the dns name, to use it:

${data.aws_lb.test.dns_name}

For the latest terraform (tf 0.13 at this time) you don't use curly brackets, so it becomes:

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

data.aws_lb.test.dns_name

Upvotes: 8

Related Questions