# How to modify existing Azure Subnet and add new service endpoints using Terraform script

**URL:** <https://discuss.hashicorp.com/t/how-to-modify-existing-azure-subnet-and-add-new-service-endpoints-using-terraform-script/64906>\
**Category:** Azure\
**Created:** [April 10, 2024, 3:00pm UTC](https://discuss.hashicorp.com/t/how-to-modify-existing-azure-subnet-and-add-new-service-endpoints-using-terraform-script/64906 "2024-04-10T15:00:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjayaraman](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jjayaraman/32/41164_2.png) [@jjayaraman](https://discuss.hashicorp.com/u/jjayaraman)\
**Post date:** [April 10, 2024, 3:00pm UTC](https://discuss.hashicorp.com/t/how-to-modify-existing-azure-subnet-and-add-new-service-endpoints-using-terraform-script/64906/1 "2024-04-10T15:00:01Z")

</div>

How to modify existing Azure Subnet and add new service endpoints using Terraform script?

I tried this option… but seems this is not available.

```auto

# Service endpoints to associate with the existing subnet
locals {
  service_endpoints = [
    "Microsoft.EventHub",
    "Microsoft.KeyVault",
    "Microsoft.ServiceBus",
    "Microsoft.Sql",
    "Microsoft.Storage"
  ]
}
 
# Create service endpoint associations for each service
resource "azurerm_subnet_service_endpoint" "subnet1_association" {
  for_each = { for idx, endpoint in local.service_endpoints : idx => endpoint }
 
  subnet_id = data.azurerm_subnet.subnet1.id
  service_endpoint_policy = each.value
}

```

But it complained about azurerm\_subnet\_service\_endpoint… seems its not available

User  
$ terraform plan  
╷  
│ Error: Invalid resource type  
│  
│ on subnet\_serviceendpoint\_association.tf line 13, in resource “azurerm\_subnet\_service\_endpoint” “subnet1\_association”:  
│ 13: resource “azurerm\_subnet\_service\_endpoint” “subnet1\_association” {  
│  
│ The provider hashicorp/azurerm does not support resource type “azurerm\_subnet\_service\_endpoint”.

As second option i tried this, but terraform plan command says it will create a new resoruce instead of updating existing subnet.

```auto
data "azurerm_subnet" "subnet1" {
  name = var.subnet1
  resource_group_name = data.azurerm_resource_group.rg_ss.name
  virtual_network_name = data.azurerm_virtual_network.vnet1.name
}

resource "azurerm_subnet" "existing_subnet" {
  # id = data.azurerm_subnet.subnet1.id
  name = data.azurerm_subnet.subnet1.name
  resource_group_name = data.azurerm_subnet.subnet1.resource_group_name
  virtual_network_name = data.azurerm_subnet.subnet1.virtual_network_name
  address_prefixes = data.azurerm_subnet.subnet1.address_prefixes

  service_endpoints = [
    "Microsoft.EventHub",
    "Microsoft.KeyVault",
    "Microsoft.ServiceBus",
    "Microsoft.Sql",
    "Microsoft.Storage"
  ]
}

```

terraform plan

```auto
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_subnet.example will be created
  + resource "azurerm_subnet" "example" {
      + address_prefixes = [
          + "10.0.0.0/24",
        ]
      + enforce_private_link_endpoint_network_policies = (known after apply)
      + enforce_private_link_service_network_policies = (known after apply)
      + id = (known after apply)
      + name = "default"
      + private_endpoint_network_policies_enabled = (known after apply)
      + private_link_service_network_policies_enabled = (known after apply)
      + resource_group_name = "rgxxxx"
      + service_endpoints = [
          + "Microsoft.EventHub",
          + "Microsoft.KeyVault",
          + "Microsoft.ServiceBus",
          + "Microsoft.Sql",
          + "Microsoft.Storage",
        ]
      + virtual_network_name = "vnetxxxxx"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [October 7, 2024, 3:00pm UTC](https://discuss.hashicorp.com/t/how-to-modify-existing-azure-subnet-and-add-new-service-endpoints-using-terraform-script/64906/2 "2024-10-07T15:00:17Z")

</div>

Closed on October 7, 2024, 3:00pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
