0
votes

I want to create a new WebApp resource into existing resource group. this question and this post explains how we can import existing resource ( instead of creating new one every time)

I was able to import my existing resource group using below command

terraform import azurerm_resource_group.rg-myResourceGroup /subscriptions/00000-my-subscription-id-0000000/resourceGroups/rg-myResourceGroup

After executing this command I can see new file is created named 'terraform.tfstate' Below is content of the file.

{
    "version": 3,
    "terraform_version": "0.11.11",
    "serial": 1,
    "lineage": "-----------------------------",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {
                "azurerm_resource_group.rg-ResourceGroupName": {
                    "type": "azurerm_resource_group",
                    "depends_on": [],
                    "primary": {
                        "id": "/subscriptions/subscription-id-00000000000/resourceGroups/rg-hemant",
                        "attributes": {
                            "id": "/subscriptions/subscription-id-00000000000/resourceGroups/rg-hemant",
                            "location": "australiaeast",
                            "name": "rg-ResourceGroupName",
                            "tags.%": "0"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.azurerm"
                }
            },
            "depends_on": []
        }
    ]
}

Now my question is how can I access/refer/include terraform.tfstate in my main.tf

resource "azurerm_resource_group" "rg-hemant" {
  #name = it should be rg-ResourceGroupName 
  #location = it should be australiaeast
}

UPDATE 1

  1. Assume that in my subscription 'mysubscription1' there is a resource group 'rg-exising'
  2. This resource group already have few resources e.g. webapp1 , storageaccount1
  3. Now I want to write a terraform script which will add new resource ( e.g. newWebapp1 ) to existing resource group 'rg-existing'
  4. so after terraform apply operation rg-exising should have below resources

    • webapp1
    • storageaccount1
    • newWebapp1 ( added by new terraform apply script )

4) Note that I don't want terraform to create ( in case of apply ) OR delete ( in case of destroy ) my existing resources which belongs to rg-exising

2
What do you mean by access? Are you saying you want Terraform to start managing the resource group so it can modify it and delete it? Or because you want to be able to refer to it with other resources?ydaetskcoR
@ydaetskcoR , please look at Update 1user2243747

2 Answers

1
votes

you dont really, you just need to map your resource to the state in tfstate, so just do:

resource "azurerm_resource_group" "rg-hemant" {
  name = 'rg-ResourceGroupName'
  location = 'australiaeast'
}

and tf should recognize this resource as the one you have in the state file

0
votes

Dug more through the posts and found a solution here.

We can use additional parameters to terraform destroy to specifically mentioned which resource we want to destroy

terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME

Note: What I have learnt is that, in this case there is no need to use terraform import command