Does Terraform have an easy way to get the IP address from the hostname?
Something like this
data "some_data" "fetch_ip" {
url = "https://google.com"
}
resource "null_resource" "temp" {
google_ip = data.some_data.fetch_ip.ip // ipv4: 123.123.123.123
}
Terraform's dns provider provides data sources for reading DNS records for a given host.
If you are looking for the IPv4 addresses for eg google.com then you would need to use the dns_a_record_set data source. In fact, the data source's documentation gives an example that looks up the A record for google.com:
data "dns_a_record_set" "google" {
host = "google.com"
}
output "google_addrs" {
value = "${join(",", data.dns_a_record_set.google.addrs)}"
}
or for HCL2/Terraform 0.12+ syntax:
data "dns_a_record_set" "google" {
host = "google.com"
}
output "google_addrs" {
value = join(",", data.dns_a_record_set.google.addrs)
}
Related
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"
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"
The documentation for OCI terraform says that I can add a search domain to resolv.conf but I cannot get this working.
https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_dhcp_options
resource "oci_core_dhcp_options" "dhcp_options" {
compartment_id = oci_identity_compartment.tf-compartment.id
options {
type = "DomainNameServer"
server_type = "VcnLocalPlusInternet"
}
options {
type = "SearchDomain"
search_domain_names = [ "mydomain.co.uk" ]
}
vcn_id = module.vcn.vcn_id
}
My domain is not appearing in the instances' resolv.conf:
$ cat /etc/resolv.conf
; Any changes made to this file will be overwritten whenever the
; DHCP lease is renewed. To persist changes you must update the
; /etc/oci-hostname.conf file. For more information see
:[https://docs.cloud.oracle.com/iaas/Content/Network/Tasks/managingDHCP.htm#notes]
;
# Generated by NetworkManager
search mydomain.oraclevcn.com public.mydomain.oraclevcn.com
nameserver 169.254.169.254
How can I get this working?
In an aks managed slb for standard sku, azure assigns a public ip automatically.
The name of this public ip is auto generated but has the following tags
"tags": {
"aks-managed-type": "aks-slb-managed-outbound-ip"
},
Im unable to retrieve this ip after its created.
The name is also auto generated
"name": "[parameters('publicIPAddresses_837ca1c7_1817_43b7_8f4d_34b750419d4b_name')]",
I tried to filter using the azurerm_public_ip data source and use tags for filtering but this is not working.
data "azurerm_public_ip" "example" {
resource_group_name = "rg-sample-004"
filter {
name = "tag:aks-managed-type"
values = [ "aks-slb-managed-outbound-ip" ]
}
}
This above code is incorrect as the name parameter is not provided, but I don't know the name until its created.
I want to whitelist this IP for the Azure MySQL database i create at apply stage.
Is there any other way to retrieve this public ip during terraform apply?
Here you go, we use this to whitelist access from AKS to key vaults etc:
data "azurerm_public_ip" "aks_outgoing" {
name = join("", (regex("([^/]+)$", join("", azurerm_kubernetes_cluster.aks.network_profile[0].load_balancer_profile[0].effective_outbound_ips))))
resource_group_name = "YOUR_RG"
}
I have a script that I need help with. I need to get the output value from remote-exec in order to write it to the inventory file.I leave here a part of the code that is needed to understand the problem and why it appeared Thanks in advance for your help
The container gets a dynamic IP address
From PVE you need to get the address of the container
resource "proxmox_lxc" "ct_name" {
....
network {
name = "eth0"
bridge = "vmbr0"
ip = "dhcp"
ip6 = "dhcp"
}
provisioner "remote-exec" {
inline = [
"lxc-info -n ${var.ct_vmid} -iH ", # In this place I need help
]
}
connection {
type = "ssh"
user = var.pm_admin
host = var.pm_ip
private_key = "${file("~/.ssh/id_rsa")}"
}
}
resource "local_file" "inventory" {
filename = "./hosts"
content = <<-EOT
[prx]
[websrv]
[dbsrv]
EOT
}
I am using Helm chart provisioned by Terraform which creates Network Load Balancer, but I do not know how to get DNS name of this balancer so I can create Route53 records in Terraform for it.
If I can get it's ARN, I can call it over data block and read dns_name, however there is nothing like thit that Helm can return for me.
Do you have any suggestions?
I would like to keep it as IaC as possible
PS: I am passing some values to Helm chart so it's creating NLB, native functionality of this Chart is to create Classic LB.
service.beta.kubernetes.io/aws-load-balancer-type: nlb
I just found and answer, it's simple using:
Note: I had to specify namespace, otherwise was service null (not found).
data "kubernetes_service" "ingress_nginx" {
metadata {
name = "ingress-nginx-controller"
namespace = "kube-system"
}
}
output "k8s_service_ingress" {
description = "External DN name of load balancer"
value = data.kubernetes_service.ingress_nginx.status.0.load_balancer.0.ingress.0.hostname
}
It can be found in official docs too - https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service
I had to use kubernetes_ingress_v1 so to create a Route 53 entry for the ingress hostname:
data "kubernetes_ingress_v1" "this" {
metadata {
name = "ingress-myservice"
namespace = "myservice"
}
depends_on = [
module.myservice-eks
]
}
resource "aws_route53_record" "this" {
zone_id = local.route53_zone_id
name = "whatever.myservice.com"
type = "CNAME"
ttl = "300"
records = [data.kubernetes_ingress_v1.this.status.0.load_balancer.0.ingress.0.hostname]
}