terraformterraform-provider-awshcl

How to get IP address from of an RDS host?


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"

Solution

  • 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"