I need to do a nslookup of the RDS instance address. What is the best way to do this via terraform?

98 Views Asked by At

I need to do a nslookup of the RDS instance address. What is the best way to do this via terraform?

2

There are 2 best solutions below

1
Paolo On

Use an external data source. Here's a demo which shows how to use a bash script that uses nslookup to retrieve the IP address for google.com.

Given util.sh:

#!/bin/bash
ip=$(nslookup "$1" | awk '/Server/ {print $2}')
jq -n --arg ip "$ip" '{"ip":$ip}'

and main.tf:

locals {
  host = "google.com"
}

data "external" "util" {
  program = ["bash","util.sh","${local.host}"]
}

output "ip" {
  value = data.external.util.result.ip
}
$ terraform apply -auto-approve
ip = "172.30.240.1"
3
Dmytro Sirant On

First of all, why do you need this information? You should never need to operate the IP address of your RDS because it's a managed service, and the IP address may change (service updates, failover), so it's safe to use its FQDN.

You can get FQDN from:
data "aws_db_instance" "database" {
  db_instance_identifier = "my-test-database"
}

as data.aws_db_instance.database.address