使用
google_compute_firewall
. 你需要
tag
具有实例资源和集合的实例
target_tags
在防火墙资源上。您可以参考这些标签的工作原理。
here
。
例子
向实例添加标记
resource "google_compute_instance" "default" {
name = "website"
machine_type = "n1-standard-1"
zone = "europe-west1-b"
tags = ["web"]
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata {
sshKeys = "james:${file("website.pem.pub")}"
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-8"
}
}
}
添加防火墙资源
resource "google_compute_firewall" "default" {
name = "web-firewall"
network = "default"
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
}
您还需要定义
source_tags
或
source_ranges
,上面的示例使用的源范围为
0.0.0.0/0
什么都可以。这可能不适用于所有规则。