0

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

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
user2243747
  • 2,767
  • 6
  • 41
  • 61
  • 1
    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 Dec 15 '18 at 09:59
  • @ydaetskcoR , please look at Update 1 – user2243747 Dec 15 '18 at 10:45

2 Answers2

1

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

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thanks, specifying name and location did add new resource to my existing resource group when I execute 'terraform apply' command. But, when I execute terraform destroy' command it also destroyed my entire existing resource group. Please have a look at my UPDATE 1 – user2243747 Dec 15 '18 at 10:47
  • yeah, destroy will destroy the tf resource (resource group in this case), so everything in it. – 4c74356b41 Dec 15 '18 at 10:56
  • Now onward we want to write terraform script to manage the new resources but we don't want to touch existing resources. New resources that we want to create will be part of existing resource group and we don't want new terraform script to touch the old resources... – user2243747 Dec 15 '18 at 10:59
  • i'm not sure about this part, tbh (it might still complain about resources in resource group, but not in the tf configuration). but you should exclude resource group resource, if you want to destroy resources – 4c74356b41 Dec 15 '18 at 11:02
  • honestly, you should just put those new resources into a separate resource group – 4c74356b41 Dec 15 '18 at 15:10
0

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

user2243747
  • 2,767
  • 6
  • 41
  • 61