Azure Terraform网络安全组与包含模块的子网的关联

qybjjes1  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(106)

我需要在Azure中使用Terraform创建一个基于模块的应用程序。
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association
基本上我需要上面的,但使用模块的子网和网络安全组如下:(网络安全组将包含关联)

main.tf (root)
  modules
     networksecuritygroup
         main.tf
         variable.tf
     subnet
         main.tf
         output.tf
         variable.tf

字符串
我不确定如何在NSG模块的网络安全组关联的subnet_id部分创建对现有子网模块的子网ID的引用。
这部分让我思考:

subnet_id = azurerm_subnet.example.id


看起来应该像这样?

subnet_id = var.subnetname.id?


我不能让它工作,我找不到类似的东西,只有一个资源组和存储帐户关联,但这是不同的,因为没有ID需要那里...

7eumitmz

7eumitmz1#

我不确定如何在NSG模块的网络安全组关联的subnet_id部分创建对现有子网模块的子网ID的引用。
或者,您可以使用terraform将NSG与现有子网ID关联,您可以使用数据块来使用现有Subnet

provider "azurerm" {
          features {}
        }
        
        data "azurerm_subnet" "venkatsubnet" {
          name                 = "samplesubnet"
          virtual_network_name = "Demosubnet"
          resource_group_name  = "<rg-name>"
        }
        
     # If you want to use an existing Virtual Network instead of creating a new Virtual Network, use the below data block for the Virtual Network.
    
   #data "azurerm_virtual_network" "example"    {
         # name                = "Demosubnet"
          #resource_group_name = "<rg-name>"
        #}
        
        resource "azurerm_virtual_network" "example" {
          name                = "example-network"
          address_space       = ["10.0.0.0/16"]
          location            = data.azurerm_resource_group.venkatrg.location
          resource_group_name = data.azurerm_resource_group.venkatrg.name
        }
        
        resource "azurerm_network_security_group" "samplensg" {
          name                = "example-nsg"
          location            = data.azurerm_resource_group.venkatrg.location
          resource_group_name = data.azurerm_resource_group.venkatrg.name
        
          security_rule {
            name                       = "test123"
            priority                   = 100
            direction                  = "Inbound"
            access                     = "Allow"
            protocol                   = "Tcp"
            source_port_range          = "*"
            destination_port_range     = "*"
            source_address_prefix      = "*"
            destination_address_prefix = "*"
          }
        }
        
        resource "azurerm_subnet_network_security_group_association" "example" {
          subnet_id                 = data.azurerm_subnet.venkatsubnet.id
          network_security_group_id = azurerm_network_security_group.samplensg.id
        }
        
        output "subnet_id" {
          value = data.azurerm_subnet.venkatsubnet.id
        }

字符串

地形应用:


的数据
运行上述代码后,子网与门户中的NSG关联,如下图所示。



参考:Azure network - Subnet

相关问题