skip to Main Content

i wanted to update my exsiting subnet in Terraform, firstly i did this command

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 {
  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. If i already imported the subnet, and it is in the terraform.tfstate why do i have to provide an already existing address_prefix?

2

Answers


  1. address_prefixes is a required attribute. You have to specify it.

    Login or Signup to reply.
  2. 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 .
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search