首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Terraform在Google实例上公开额外的端口?

如何使用Terraform在Google实例上公开额外的端口?
EN

Stack Overflow用户
提问于 2018-05-28 20:38:50
回答 1查看 1.7K关注 0票数 4

我有一个由一些Terraform代码定义的Google实例。

代码语言:javascript
复制
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资源?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 21:19:49

使用google_compute_firewall。您需要使用实例资源对实例进行tag,并在防火墙资源上设置target_tags。您可以参考这些标记是如何工作的这里

示例

向实例添加一个标记

代码语言:javascript
复制
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"
    }
  }
}

添加防火墙资源

代码语言:javascript
复制
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_tagssource_ranges,上面的示例使用了0.0.0.0/0的源范围,即“任意”。这可能不适合所有规则。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50573187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档