Terraform for_each azurerm_firewall_policy_rule_collection_group动态规则

rkue9o1l  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(265)

我有一个terraform文件nonfunctionalvms.tf,它可以创建多个虚拟机

  1. module "nonfunctional" {
  2. source = "../../modules/additionalvms"
  3. resource_group = var.resource_group
  4. vmname = "nf-add-vm"
  5. instances = {
  6. TICKET-F2345 = { name = "F2345" }
  7. TICKET-F2471 = { name = "F2371" }
  8. }
  9. }

字符串
创建虚拟机,操作系统磁盘,NIC和配置操作系统-所有工作正常。在该模块中,我需要(尝试)动态允许http/s访问。我试图在防火墙策略下创建动态部分,但没有成功。

modules/additionalvms/firewall_policy.tf文件内容:

  1. resource "azurerm_firewall_policy_rule_collection_group" "policy-additionalvms" {
  2. name = "policy-additionalvms"
  3. firewall_policy_id = data.azurerm_firewall_policy.nonfunctional.id
  4. priority = 2300
  5. application_rule_collection {
  6. name = "policy-additionalvms"
  7. priority = 2312
  8. action = "Allow"
  9. dynamic "rule" {
  10. for_each = var.instances
  11. content {
  12. name = allow-web-out[each.key]
  13. protocols {
  14. type = "Http"
  15. port = 80
  16. }
  17. protocols {
  18. type = "Https"
  19. port = 443
  20. }
  21. source_addresses = [azurerm_network_interface.additionalvms[each.key].private_ip_address]
  22. destination_fqdns = ["*"]
  23. }
  24. }
  25. }
  26. }


我只需要将VM的其他IP添加到现有规则集合中我错在哪里?这是我得到的错误:

  1. Error: Invalid reference
  2. on ../../modules/jenkins/firewall_policy.tf line 12, in resource "azurerm_firewall_policy_rule_collection_group" "Policy-additionalvms":
  3. 12: name = allow-web-out[each.key]
  4. A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
  5. Error: Reference to "each" in context without for_each
  6. on ../../modules/jenkins/firewall_policy.tf line 12, in resource "azurerm_firewall_policy_rule_collection_group" "Policy-additionalvms":
  7. 12: name = allow-web-out[each.key]
  8. The "each" object can be used only in "module" or "resource" blocks, and only when the "for_each" argument is set.
  9. Error: Reference to "each" in context without for_each
  10. on ../../modules/jenkins/firewall_policy.tf line 21, in resource "azurerm_firewall_policy_rule_collection_group" "Policy-additionalvms":
  11. 21: source_addresses = [azurerm_network_interface.additionalvms-nic[each.key].private_ip_address]
  12. The "each" object can be used only in "module" or "resource" blocks, and only when the "for_each" argument is set.

h43kikqp

h43kikqp1#

答案是:

  1. module "nonfunctional" {
  2. source = "../../modules/additionalvms"
  3. resource_group = var.resource_group
  4. vmname = "nf-add-vm"
  5. instances = {
  6. TICKET-F2345 = { name = "F2345" }
  7. TICKET-F2471 = { name = "F2371" }
  8. }
  9. }

字符串
防火墙策略是:

  1. resource "azurerm_firewall_policy_rule_collection_group" "policy-additionalvms" {
  2. name = "policy-additionalvms"
  3. firewall_policy_id = data.azurerm_firewall_policy.nonfunctional.id
  4. priority = 2300
  5. application_rule_collection {
  6. name = "policy-additionalvms"
  7. priority = 2312
  8. action = "Allow"
  9. dynamic "rule" {
  10. for_each = var.instances
  11. content {
  12. name = "allow-web-out-${rule.key}"
  13. protocols {
  14. type = "Http"
  15. port = 80
  16. }
  17. protocols {
  18. type = "Https"
  19. port = 443
  20. }
  21. source_addresses = [azurerm_network_interface.additionalvms[rule.key].private_ip_address]
  22. destination_fqdns = ["*"]
  23. }
  24. }
  25. }
  26. }

展开查看全部

相关问题