Home > OS >  Bind Service Bus Namespace to a Virtual Network using Terraform
Bind Service Bus Namespace to a Virtual Network using Terraform

Time:01-03

Bind Service Bus Namespace to a VNet using Terraform.

I am implemented the same via ARM template and now as per my requirement i would like to implement the same via terraform. Setup service bus endpoint to our vnet so no other networks can t service bus namespace.

here is the ARM form Microsoft

how to do this using terraform ?

Bind Service Bus Namespace to a VNet using Terraform.

CodePudding user response:

I have replicated the same via terraform. In order to manages a ServiceBus Namespace Network Rule Set, please find below repo code from - hashicorp

main tf file as follows:

provider "azurerm" {
    features {}
}
resource "azurerm_resource_group" "example" {
  name     = "*******"
  location = "East US"
}
resource "azurerm_servicebus_namespace" "example" {
  name                = "exam-sb-namespace"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Premium"

  capacity = 1
}

resource "azurerm_virtual_network" "example" {
  name                = "exam-vnet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["172.17.0.0/16"]
  dns_servers         = ["10.0.0.4", "10.0.0.5"]
}

resource "azurerm_subnet" "example" {
  name                 = "default"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["172.17.0.0/24"]

  service_endpoints = ["Microsoft.ServiceBus"]
}

resource "azurerm_servicebus_namespace_network_rule_set" "example" {
  namespace_id = azurerm_servicebus_namespace.example.id

  default_action                = "Deny"
  public_network_access_enabled = true

  network_rules {
    subnet_id                            = azurerm_subnet.example.id
    ignore_missing_vnet_service_endpoint = false
  }

  ip_rules = ["1.1.1.1"]
}

provider tf file as follows:

terraform {
  
      required_version = "~>1.3.3"
      required_providers {
        azurerm = {
           source = "hashicorp/azurerm"
           version = ">=3.5.0"
             }
           }
 }

OutPut as follows: upon running plan enter image description here

upon apply enter image description here

From Portal enter image description here

CodePudding user response:

Use azurerm_servicebus_namespace_network_rule_set as per the official example:

resource "azurerm_subnet" "example" {
  name                 = "default"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["172.17.0.0/24"]

  service_endpoints = ["Microsoft.ServiceBus"]
}

resource "azurerm_servicebus_namespace_network_rule_set" "example" {
  namespace_id = azurerm_servicebus_namespace.example.id

  default_action                = "Deny"
  public_network_access_enabled = true

  network_rules {
    subnet_id                            = azurerm_subnet.example.id
    ignore_missing_vnet_service_endpoint = false
  }
}
  • Related