正在访问.tf文件-地形中的.tfstate文件



我想在现有资源组中创建一个新的WebApp资源。这个问题和这篇文章解释了我们如何导入现有的资源(而不是每次都创建新的资源(

我能够使用下面的命令导入我现有的资源组

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

执行完这个命令后,我可以看到新文件被创建为'terraform.tfstate'。下面是文件的内容。

{
"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": []
}
]
}

现在我的问题是如何在我的main.tf中访问/引用/包括terraform.tfstate

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

更新1

  1. 假设在我的订阅"mysubscription1"中有资源组"rg-exising">
  2. 该资源组已经具有很少的资源,例如CCD_,storageaccount1
  3. 现在我想写一个地形脚本,它将添加新的资源(例如newWebapp1(到现有资源组'rg现有'
  4. 所以在CCD_ 8操作之后CCD_低于资源

    • webapp1
    • 存储帐户1
    • newWebapp1(由新的terraform apply脚本添加(

4(注意,我不希望terraform创建(在apply的情况下(或删除(在destroy的情况下,(属于rg-exising的现有资源

你真的不需要,你只需要将你的资源映射到tfstate中的状态,所以只需要:

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

tf应该将该资源识别为状态文件中的资源

在帖子中挖掘更多内容,并在这里找到了解决方案。

我们可以使用额外的参数来地形化摧毁,特别提到我们想要摧毁的资源

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

注意:我学到的是,在这种情况下,不需要使用terraform import命令

最新更新