How can I retrieve Azure scale set's VM private IP Addresses via terraform?

Viewed 1660

I'm wondering how can I retrieve scale set VM's private IP address with Terraform-provider-azurerm. I think there are no resources nor data resources directly return VM IP's.

One option I tried was generate shellscript via template resource.

# get_vmss_privateip.tpl
#!/bin/bash

cap=`az vmss show \
    --resource-group ${resource_group} \
    --subscription ${subscription} \
    --name ${name} \
    --query 'sku.capacity'`
for i in `seq 1 $cap`
do
az resource show \
    --resource-group ${resource_group} \
    --resource-type Microsoft.Compute/virtualMachineScaleSets \
    --api-version 2017-03-30 \
    --name ${name}/virtualMachines/$i/networkInterfaces \
    --query 'value[0].properties.ipConfigurations[0].properties' \
| jq -c '{privateIPAddress}'
done

then run terraform to generate sh.

data "template_file" "private_ip_scripts" {
  template = "${file("templates/get_vmss_privateip.tpl")}"

  vars {
    resource_group = "${data.azurerm_resource_group.current.name}"
    subscription   = "${data.azurerm_subscription.current.subscription_id}"
    name           = "${azurerm_virtual_machine_scale_set.test.name}"
  }
}

resource "local_file" "test_private_ip_scripts" {
  filename = "scripts/get_vmss_instance_private_ip.sh"
  content  = "${data.template_file.manage_private_ip_scripts.rendered}"
}

But this approach is too far from goal, and I do want to use private IP's in the terraform, not outside terraform.

Do anyone have much better ideas?

EDIT 2018/11/2

I've done via external data resource.

data "external" "vmss_test_private_ip" {
  program = ["bash", "${local_file.test_private_ip_scripts.filename}"]
}

output hoge {
  value = "${data.external.vmss_test_private_ip.result}"
}
1 Answers

This type of resource can be imported into TF Scale Set. So this is one option, only hassle is there are a lot of attributes and importing introduces other issues. I've found one or two things that aren't exposed as data resources in the AzureRM provider. Might be worth adding a request to the GitHub repo?

Regards,

Related