How to manage existing resource group in azure with terraform - azure

I am having my azure infrastructure created using terraform.
Now I want to add few resources to existing resource group.
When I did same it is giving error like resources group is already exists.
How can I refer existing resource and no changes to existing resources and tfstate file.

There is a couple of ways to refer existing resource in Azure without making changes.
Use Terraform import
Use Terraform data resource
Terraform import example:
resource "azurerm_resource_group" "example" {
# ...instance configuration...
name = "MyResourceGroup"
}
Run command: terraform import azurerm_resource_group.example \ /subscriptions/MySubscriptionNumber/resourceGroups/MyResourceGroup
Terraform data resource example:
data "azurerm_resource_group" "example" {
name = "MyResourceGroup"
}

Related

Azure : ResourceNotFound from az cli but resource exist in Portal

Facing this "ResourceNotFound" issue (JPG-1), but I can see the logicapp resource in Azure portal(JPG-2)
(ResourceNotFound) The Resource 'Microsoft.Web/sites/us-analytics-dev-dsvm-auto-deletion-logicapp-eastus2' under resource group 'us-analytics-dev-dsvm-auto-deletion-eastus2' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
Code: ResourceNotFound
Message: The Resource 'Microsoft.Web/sites/us-analytics-dev-dsvm-auto-deletion-logicapp-eastus2' under resource group 'us-analytics-dev-dsvm-auto-deletion-eastus2' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
Note :
Already configured (us-analytics-dev) as default subscription in
cli.
Logicapp resource created through Terraform (Code below : code-1)
Code-1 :(edited)
resource "azurerm_resource_group" "dsvm_auto_deletion_resource_group" {
name = "us-analytics-dev-dsvm-auto-deletion-logicapp-eastus2"
location = "East US 2"
}
resource "azurerm_logic_app_workflow" "dsvm_auto_deletion_logicapp" {
name = "us-analytics-dev-dsvm-auto-deletion-logicapp-eastus2"
location = "East US 2"
resource_group_name = "us-analytics-dev-dsvm-auto-deletion-eastus2"
}
First note I noticed your azurerm_resource_group resource name is the same as your azurerm_logic_app_workflow resource name but I think it is just a typo :)
I tried the same command as you and it didn't work for me, it kept returning empty list, I think something is wrong with the az logicapp command.
After some research I found that there is a package in preview that can be used instead which is logic workflow.
Just past in the following command and install the package and it should work, so for your case it would be something like :
az logic workflow show -g us-analytics-dev-dsvm-auto-deletion-eastus2 --name us-analytics-dev-dsvm-auto-deletion-logicapp-eastus2

terraform import existing azure resources

I want to create subnet in my existing vnet which is present in azure. I found a command to import resource terraform import . but how do i use the resource details example: vnet resource group in the code.
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
address_space = var.vnet_address_space
resource_group_name = var.subscriptionrg_name
location }
I ran the command and found that the dns server which we earlier present are now deleted. Does that means i have to define everything in my code and import. Or is there any other way to use existing resources.
First just define an empty block like this:
resource "azurerm_virtual_network" "vnet" {
}
Then, run terraform import command to import existing resource into your Terraform state. Afterward, execute the terraform show to print out the TF state into the screen. Finally, you can copy the content of printed "azurerm_virtual_network" "vnet" block into the actual block in .tf file.

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.

Fetch Azure Resources using Terraform

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]

Terraform fails using an Azure service principal for authentication

Problem
Terraform gives the following error when trying to use terraform plan or terraform apply after create a service principal in Azure:
provider.azurerm: No valid (unexpired) Azure CLI Auth Tokens found. Please run az login.
Steps to Reproduce
Create a service principal in Azure via az ad sp create-for-rbac.
Add the service principal configuration as a provider block to your .tf file:
provider "azurerm" {
alias = "tf_bootstrap"
client_id = "55708466-3686-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "88352837-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "129a861e-a703-xxxx-xxxx-xxxxxxxxxxxx"
subscription_id = "c2e9d518-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
resource "azurerm_resource_group" "dev" {
name = "dev-rg"
location = "East US"
}
Attempt to run terraform plan.
If using the alias key in a provider block, as shown in the question, a provider key must be specified in each data or resource blocks.
For example:
// When a provider alias has been defined.
resource "azurerm_resource_group" "dev" {
provider = "azurerm.tf_bootstrap"
name = "dev-rg"
location = "East US"
}
If you miss a provider for one of your resources or data blocks, authentication fails on that block.
Note however that is also valid to not specify an alias key in the original provider block. In that case, it is no longer necessary to specify a provider key in every resource and data block; the provider key can be omitted.
// When a provider alias has not been defined.
resource "azurerm_resource_group" "dev" {
name = "dev-rg"
location = "East US"
}

Resources