简体   繁体   English

如何在使用 Terraform 创建 Azure NSG 时纠正此错误?

[英]How to correct this error in creating Azure NSG with Terraform?

I am trying to create a NSG in Azure with Terraform.我正在尝试使用 Terraform 在 Azure 中创建一个 NSG。

Terraform Version is v0.15.2 with provider version azurerm v2.61.0 Terraform 版本为v0.15.2 ,提供程序版本为 azurerm v2.61.0

Here's the piece of code in my TF file.这是我的 TF 文件中的一段代码。

  resource "azurerm_network_security_group" "nsg" {
  name                = "SG"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name


  security_rule = [{
    access                                     = "Allow"
    description                                = "SSH Rule"
    destination_address_prefix                 = "*"
    destination_address_prefixes               = ["*"]
    destination_port_range                     = "*"
    destination_port_ranges                    = ["22"]
    direction                                  = "Inbound"
    name                                       = "SSH Rule"
    priority                                   = 100
    protocol                                   = "Tcp"
    source_address_prefix                      = "*"
    source_address_prefixes                    = ["*"]
    source_port_range                          = "22"
    source_port_ranges                         = ["22"]
    source_application_security_group_ids      = [""]
    destination_application_security_group_ids = [""]

  }]
}

Now when I run terraform plan & terraform apply , I get the expected output as:现在,当我运行terraform planterraform apply时,我得到预期的 output 为:

* only one of "source_port_range" and "source_port_ranges" can be used per security rule
* only one of "destination_port_range" and "destination_port_ranges" can be used per security rule
* only one of "source_address_prefix" and "source_address_prefixes" can be used per security rule
* only one of "destination_address_prefix" and "destination_address_prefixes" can be used per security rule

Now when I keep only source_port_range, destination_port_range, source_address_prefix, destination_address_prefix fields & run terraform plan again, it gives me following error:现在,当我只保留source_port_range, destination_port_range, source_address_prefix, destination_address_prefix字段并再次运行terraform plan时,它给了我以下错误:

Inappropriate value for attribute "security_rule": element 0: attributes "destination_address_prefixes",
│ "destination_port_ranges", "source_address_prefixes", and "source_port_ranges" are required.

If I add those & remove the earlier ones, I get:如果我添加这些并删除早期的,我会得到:

│ Inappropriate value for attribute "security_rule": element 0: attributes "destination_address_prefix", "destination_port_range",  
│ "source_address_prefix", and "source_port_range" are required.

Why is this happening & how to get around with this issue?为什么会发生这种情况以及如何解决这个问题?

Update更新

Took into consideration the point mentioned in the comment & made few changes to get it working.考虑到评论中提到的观点并进行了一些更改以使其正常工作。

Points to note:注意事项:

  • Even when it's mentioned Optional in the doc, Terraform needs all the keys in the security_rule block.即使在文档中提到可选,Terraform 也需要 security_rule 块中的所有密钥。
  • Also, it doesn't allow Source/destination address_prefixes = ["*"] like this.此外,它不允许 Source/destination address_prefixes = ["*"] 像这样。 Source/Destination address_prefix did the job. Source/Destination address_prefix 完成了这项工作。
  • And where values are not mentioned, [] is expected instead of [""]在未提及值的情况下,应使用 [] 而不是 [""]
resource "azurerm_network_security_group" "nsg" {
  name                = "SG"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name


  security_rule = [{
    access                                     = "Allow"
    description                                = "SSH Rule"
    destination_address_prefix                 = "*"
    destination_address_prefixes               = []
    destination_port_range                     = ""
    destination_port_ranges                    = ["22"]
    direction                                  = "Inbound"
    name                                       = "SSH Rule"
    priority                                   = 100
    protocol                                   = "Tcp"
    source_address_prefix                      = "*"
    source_address_prefixes                    = []
    source_port_range                          = "*"
    source_port_ranges                         = []
    source_application_security_group_ids      = []
    destination_application_security_group_ids = []

  }]
}

Can you try this:你可以试试这个:

  security_rule = [{
    access                                     = "Allow"
    description                                = "SSH Rule"
    destination_address_prefix                 = ""
    destination_address_prefixes               = ["*"]
    destination_port_range                     = ""
    destination_port_ranges                    = ["22"]
    direction                                  = "Inbound"
    name                                       = "SSH Rule"
    priority                                   = 100
    protocol                                   = "Tcp"
    source_address_prefix                      = ""
    source_address_prefixes                    = ["*"]
    source_port_range                          = ""
    source_port_ranges                         = ["22"]
    source_application_security_group_ids      = []
    destination_application_security_group_ids = []

  }]
}

You're defining values in both fields - ie Terraform will only accept one of either destination_port_range or destination_port_ranges and not both.您在两个字段中定义值 - 即 Terraform 将只接受destination_port_rangedestination_port_ranges ,而不接受两者。 Same goes for the other attributes.其他属性也是如此。

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

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