Fetch Azure Resources using Terraform - azure

I want to get a list of Azure resources, example, we have the command Get-AzResourceGroup to get list of resources. Or Get-AzSqlServer -ResourceGroupName "ResourceGroup01" to list SQL Servers from a particular Resource Group. How can we do this in Terraform ?

Actually, you can use the data source:azurerm_resources to list the resources with the type in Azure as you want. But as I test, when I add the resource group, it only returns an empty list. I'm not sure if there is something wrong with my code. If it works as it shows, then it's the thing which you want and the example would like this:
data "azurerm_resources" "example" {
resource_group_name = "example-resources"
type = "Microsoft.Sql/servers"
}

You could authenticate with Azure CLI with terraform and then use the CLI command to get the resources.
az resource list [--location]
[--name]
[--namespace]
[--resource-group]
[--resource-type]
[--subscription]
[--tag]

Related

Using Azure CLI, Is there a way to find the resource group a resource belongs to?

I need this to write a bash script. I have a bunch of resource IDs and I need to find the resource groups to which they belong. Is there someway to do this?
The RG name is included in the ID. The fully qualified ID of the resource, including the resource name and resource type. Use the format, /subscriptions/{guid}/resourceGroups/{resource-group-name}/{resource-provider-namespace}/{resource-type}/{resource-name} Part of the resource ID is the resource group name.
Use az resoure show, to get the values of each resource
az resource show --ids /subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyWebapp
If you have many ids, you could write a for loop
If you want to write a query you could do JMESPath like this:
az resource list --query [?id=='the-id'].resourceGroup
Or with az resource show --ids
az resource show --ids "the-id" --query properties.actionGroups.groupIds
You still have to loop through the ids though

Deploy azure resource in prebuilt resource group using terraform

I am having one resource group in my azure subscription name "demoterraform"
Now I would like to create one windows VM in this resource group, So I don't deploy new VM in existing resource group.
Use the azurerm_resource_group data source.
data "azurerm_resource_group" "demo" {
name = "demoterraform"
}
in the rest of the code you can refer to it with a similar expression data.azurerm_resource_group.demo.id.

How can I update ip_configuration of an azurerm_network_interface to add azurerm_public_ip?

I have created an HDInsight cluster with kafka using azurerm_hdinsight_kafka_cluster on a VNet (azurerm_virtual_network) with terraform. Due to which I get instances of Azure's network interface created implicitly by Azure Management Service.
I have also created azurerm_public_ip resources which I intend to associate with the network interfaces which are created implicitly as mentioned above.
This means, I need to update the implicitly created azurerm_network_interface resource with the azurerm_public_ip IP address via public_ip_address_id attribute.
I searched online for any documentation on updating the ip_configuration nested attribute of azurerm_network_interface (created implicitly), but unfortunately, did not find any.
Could anyone help me on this? I have not found any other resource in azurerm as well which might help me achieve this.
I would appreciate if someone could point me to an azurerm resource by which i can associate this or any other way possible.
Thanks in advance :)
How can I update ip_configuration of an azurerm_network_interface to add azurerm_public_ip?
I probably found a solution:
Using a null_resource from terraform for execute a local_exec with an Azure CLI command should do the trick.
Note: Make sure the local system has azure cli installed as prequisite.
e.g.:
resource "null_resource" "foo" {
count = 3
provisioner "local-exec" {
command = <<EOT
az --service-principal -u <USERNAME> -p '<PASSWORD>' --tenant <TENANT-ID>
az network nic ip-config update --resource-group ${azurerm_resource_group.foo.name} --name ${azurerm_network_interface.foo[count.index].ip_configuration[0].name} --nic-name ${azurerm_network_interface.foo[count.index].name} --public-ip-address ${azurerm_public_ip.foo[count.index].id}
EOT
}
}
The command can be referred from the Azure documentation here.

Why is Azure CLI reporting ResourceGroupNotFound when trying to run New-AzResourceGroupDeployment?

I'm trying to create an Application Insights resource following the directions provided here: https://learn.microsoft.com/en-us/azure/azure-monitor/app/powershell
However, when I execute the command from the docs:
New-AzResourceGroupDeployment -ResourceGroupName Fabrikam -TemplateFile .\template1.json -appName myNewApp
Replacing Fabrikam my Resource Group Name, it throws ResourceGroupNotFound. If I list the Resource Groups with:
az group list
I can clearly see the Resource Group in the list, so I know I'm in the right subscription context.
Any thing obvious that I'm missing?
I've uploaded my version of template1.json already to the CLI storage.
I can clearly see the Resource Group in the list, so I know I'm in the right subscription context.
No, if you can use az group list to see the group, it just means the azure CLI context is in the right subscription. The New-AzResourceGroupDeployment is azure powershell, they are different, you need to use Get-AzResourceGroup to list groups.
To check if you are in the correct subscription, just use Get-AzContext. If you want to set the subscription for the powershell context, just use Set-AzContext -Subscription "<subscription-id>".
I've uploaded my version of template1.json already to the CLI storage.
I suppose you mean you upload the template to the azure storage. If so, you could not use this parameter -TemplateFile, you need to use -TemplateUri and -TemplateParameterUri, you need to generate the SAS urls for your template files(if your container is not public), then specify the two parameters, see this link.
Actually, you can use New-AzResource to create the app insight directly, no need to use the template in the doc.
Sample:
New-AzResource -ResourceName "<appinsight-name>" -ResourceGroupName <resourcegroup-name> -ResourceType "Microsoft.Insights/components" -Location "East US" -PropertyObject #{"Application_Type"="web"}

List out all resource groups in cluster

I want to see all the resource groups in my cluster(s). Is there a way to use az aks to get all the credentials of the cluster(s) without mentioning the names of the resource-group/cluster?
I tried az aks get-credentials, az aks show, az aks list but they all require the resource group name and I don't know it.
Any help is greatly appreciated.
Update:
I have found a way to list all resources in my azure subscription: az resource list.
I can see some resources here that when i try to find using `az
After alot of digging, you can find the list of resource groups using this command - az group list.
Note: its useful to go through the -h of for az to see what it has to offer :)

Resources