我有一个由一些Terraform代码定义的Google实例。
provider "google" {
credentials = "${file("auth.json")}"
project = "aqueous-depth-189023"
region = "europe-west2"
}
resource "google_project" "website" {
name = "Website"
project_id = "aqueous-depth-189023"
}
resource "google_compute_instance" "default" {
name = "website"
machine_type = "n1-standard-1"
zone = "europe-west1-b"
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
metadata {
sshKeys = "james:${file("website.pem.pub")}"
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-8"
}
}
}默认情况下,Google只为Google实例公开端口22和其他几个端口。我是否可以更新我的Terraform代码,以实现端口80和其他一些端口的公开,而不必使用web控制台?我需要添加或编辑什么Terraform资源?
发布于 2018-05-28 21:19:49
使用google_compute_firewall。您需要使用实例资源对实例进行tag,并在防火墙资源上设置target_tags。您可以参考这些标记是如何工作的这里。
示例
向实例添加一个标记
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的源范围,即“任意”。这可能不适合所有规则。
https://stackoverflow.com/questions/50573187
复制相似问题