[英]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 plan
和terraform 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:注意事项:
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_range
或destination_port_ranges
,而不接受两者。 Same goes for the other attributes.其他属性也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.