-2

I am using aws_db_instance resource from Terraform AWS provider. Could you please let me know how to get IPv4 address of this provisioned RDS resource?

I could only see endpoint in attribute reference which is not returning IP address.

endpoint = "akshaylokur-read.comhtyf8iplf.us-west-2.rds.amazonaws.com:3306"
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
  • 1
    And why do you need the IP address? – Marko E Feb 10 '23 at 12:56
  • @MarkoE I am dealing with some weird configuration which depends on IP address of RDS (I know it is not ideal as IP addresses can change). But it is what it is for now and there is a remediation mechanism in place to handle such IP address changes. But the explanation of this everything is out of scope for this question. – Akshay Lokur Feb 10 '23 at 13:00
  • 1
    I don't think it's out of the scope, because depending on how you have deployed the RDS instance will also depend on how you can get the IP addresses (or if you can get them easily): https://aws.amazon.com/premiumsupport/knowledge-center/rds-ip-address-issues/. – Marko E Feb 10 '23 at 13:37
  • 2
    yeah you may have to create a script or run a cmd after your RDS instance is deployed such as `dig akshaylokur-read.comhtyf8iplf.us-west-2.rds.amazonaws.com` to grab the dynamic IP & include it with the remediation mechanism you mentioned. However I think it would be best if the 'weird configuration' was changed to use the RDS endpoint instead. – paulg Feb 10 '23 at 15:39

1 Answers1

3

I could get RDS dynamic IP address using hashicorp/dns provider as shown below:

main.tf:

terraform {
  required_providers {
    dns = {
      source = "hashicorp/dns"
      version = "3.2.4"
    }
  }
}

data "dns_a_record_set" "rds_dynamic_ip" {
  host = "akshaylokur-read.comhtyf8iplf.us-west-2.rds.amazonaws.com"
}

output "rds_ip_addrs" {
  value = join(",", data.dns_a_record_set.rds_dynamic_ip.addrs)
}

Output (deliberately masked o/p IP with x below):

❯ terraform plan
data.dns_a_record_set.rds_dynamic_ip: Reading...
data.dns_a_record_set.rds_dynamic_ip: Read complete after 0s [id=akshaylokur-read.comhtyf8iplf.us-west-2.rds.amazonaws.com]

Changes to Outputs:
  + rds_ip_addrs = "xxx.xx.242.117"
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62