简体   繁体   English

更新 Terraform 中的现有 azure 子网

[英]Update existing azure subnet in Terraform

i wanted to update my exsiting subnet in Terraform, firstly i did this command我想更新我在 Terraform 中的现有子网,首先我做了这个命令

terraform import 'azurerm_subnet.example' /subscriptions/id-id-id-id/resourceGroups/tfeastus/providers/Microsoft.Network/virtualNetworks/myVnetTF/subnets/SUBNET-0

the import was successfull, so i made my terraform template导入成功,所以我制作了我的 terraform 模板

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.0"
    }
  }
}
provider "azurerm" {
  features {}
}
variable "vnetName" {
  type = string
}
variable "location" {
  type = string
}
variable "resource_group_name" {
  type = string
}
variable "subnetName" {
  type = string
}
data "azurerm_virtual_network" "vnet" {
  name                = var.vnetName
  location            = var.location
  resource_group_name = var.resource_group_name

}
resource "azurerm_subnet" "example" {
  name                 = var.subnetName
  resource_group_name  = var.resource_group_name
  virtual_network_name = var.vnetName

  delegation {
    name = "delegation"
    service_delegation {
      name = "Microsoft.Web/serverFarms"
    }
  }
}

Although, after terraform apply i get an error saying that │ "address_prefix": one of address_prefix,address_prefixes must be specified .虽然,在 terraform 应用之后,我收到一条错误消息,说│ "address_prefix": one of address_prefix,address_prefixes must be specified If i already imported the subnet, and it is in the terraform.tfstate why do i have to provide an already existing address_prefix?如果我已经导入了子网,并且它位于terraform.tfstate中,为什么我必须提供已经存在的地址前缀?

address_prefixes is a required attribute. address_prefixes必需的属性。 You have to specify it.你必须指定它。

You successfully imported the code from azure portal to local system by using **terraform import command**
Present in your local system you have **terraform tfstate file** right.
Now you wrote infrastructure code like this in your local system

provider "azurerm" {
  features {}
  version = "=3.0.0"
}
variable "vnetName" {
}
variable "location" {
}
variable "resource_group_name" {
}
variable "subnetName" {
}
data "azurerm_virtual_network" "vnet" {
  name                = var.vnetName
  location            = var.location
  resource_group_name = var.resource_group_name

}
resource "azurerm_subnet" "example" {
  name                 = var.subnetName
  resource_group_name  = var.resource_group_name
  virtual_network_name = var.vnetName

  delegation {
    name = "delegation"
    service_delegation {
      name = "Microsoft.Web/serverFarms"
    }
  }
}

now need to use terraform commands, there are six commands in terraform
1.**terraform init**  - When you use init command it will download the azurerm module and terraform plugins
2.**terrafrom fmt** - when you use fmt command it will do allign the text or code in proper way or proper format
3.**terraform validate** - when you use **validate command** it will do check all parameters or argument available or not in resource block
Here it will give error for you why because
In tfstate file we have already **address_prefix** for subnet but when you come to infra code we didn't mention any **address_prefix** parameter or argument
when you **terraform validate** command it will check all parameters are available or not in infra code and also it will refresh tfstate file as well as .
Now, in **tfstatefile** we have all parameters and now it will check infra code, in infa code **address_prefix** parameter or argument missing so it will give error for you
If you won't or don't use **terraform validate** commands means, when you use terraform plan/apply it will give error for you.
So, definitely you need to mention address_prefix parameter or argument in infra code also .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM