使用 Terraform 实现 Azure 应用程序安全组



应用程序安全组功能刚刚在四月份发布。我们正在尝试实现这一点,因为我们有大量服务器,因此网络安全组很快就会变得难以管理。

我找不到任何示例 Terraform 代码。我已经修改了 https://learn.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm 中的示例 Terraform 代码,以实现快速 POC。场景是我们有一组堡垒服务器(目前只有 1 个(,我们保护它们,所有进入重要服务器的 SSH 都将来自这些堡垒服务器。因此,我创建了一个bastion_asg应用程序安全组,并将DL2staging_rtb_nsg设置为仅允许来自bastion_asg的服务器进行 SSH 访问。但是,一旦它运行并创建了服务器,我就无法通过ssh进入DL2staging_rtb_vm。我已经附加了我的代码。

非常感谢关于我的 POC 可能出现问题的任何和所有指示。

谢谢

德里克

** 以下是 https://learn.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm 中示例 Terraform 代码的主要代码添加:

resource "azurerm_network_security_group" "DL2staging_rtb_nsg" {
...
security_rule {
name                        = "AllowSSHInbound"
...
source_application_security_group_ids = ["${azurerm_application_security_group.bastion_asg.id}"]
destination_address_prefix  = "*"
}
# Create network interface
resource "azurerm_network_interface" "DL2staging_rtb_nic" {
...
ip_configuration {
name                          = "DL2NicConfiguration"
...
application_security_group_ids = ["${azurerm_application_security_group.staging_sellsidertb_asg.id}"]
}

完整的代码在 https://github.com/dl888888/azure-terraform-application-security-group/blob/master/vm3.tf

事实证明,我的代码有效。我遇到的问题是假设 ASG(应用程序安全组(将使用我拥有的 VM 的公共 IP 地址。我发现 Azure 产品经理 ASG 仅适用于专用 IP 地址。这是ASG档案的一大遗漏。

德里克

如我所见,你将 NSG 和 ASG 与每个网络接口相关联,并且只允许流量通过 ASG,而不是 NSG。

我建议您再次阅读文档应用程序安全组,我认为它的示例是一个很好的网络体系结构。

对于你的问题,我建议一个具有子网的 NSG 和一个与每个网络接口关联的 ASG。然后允许具有明确源和目的地的流量,在Terraform中,这意味着明确的source_application_security_group_idsdestination_application_security_group_ids

最新更新