google-cloud-platform - Terraform 配置未在 GCP 上公开 http 端口

标签 google-cloud-platform terraform-provider-gcp

我正在使用以下 terraform 代码来公开在端口 80 上运行的 http 服务器应用程序。 应用 terraform 配置后,当我尝试 curl 或访问公共(public) IP e iget 连接超时。 如果我 curl 本地主机,它工作正常。所以问题出在配置上。我缺少任何配置吗?

// Configure the Google Cloud provider
provider "google" {
 credentials = file("xxxxxx-13a189a9c1c7.json")
 project     = "xxxx-xxxx"
 region      = "us-west1"
}


// Terraform plugin for creating random ids
resource "random_id" "instance_id" {
 byte_length = 8
}

// A single Compute Engine instance
resource "google_compute_instance" "default" {
 name         = "bkps-314318-${random_id.instance_id.hex}"
 machine_type = "f1-micro"
 zone         = "us-west1-a"

 tags = ["web","http-server"]
 
 boot_disk {
   initialize_params {
     image = "debian-cloud/debian-9"
   }
 }

metadata = {
   ssh-keys = "joao:${file("/home/gc/projetos/gcp/terraform/joaossh.pub")}"
 }
 
 metadata_startup_script = file("${path.module}/startup.sh")

 network_interface {
   network = "default"


   access_config {
     // Include this section to give the VM an external ip address
      // A variable for extracting the external IP address of the instance


   }
 }
}

output "ip" {
 value = google_compute_instance.default.network_interface.0.access_config.0.nat_ip
}



resource "google_compute_firewall" "allow-http" {
  name    = "http-firewall"
  network = google_compute_network.default.name

  source_ranges = ["0.0.0.0/0"]

  allow {
    protocol = "tcp"
    ports    = ["80", "443", "8080", "1000-4000"]
  }

  source_tags = ["web"]
}

resource "google_compute_network" "default" {
  name = "test-network"
}

enter image description here

enter image description here

enter image description here

最佳答案

在资源部分

resource "google_compute_firewall" "allow-http" {

您定义了要附加防火墙规则的实例:

source_tags = ["web']

解决方案:

在资源部分

resource "google_compute_instance" "default" {

添加以下行:

tags = ["web"]

关于google-cloud-platform - Terraform 配置未在 GCP 上公开 http 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68614677/

相关文章:

google-cloud-platform - 对于带有 Beta 参数的 Google Cloud Platform 资源,正确的 Terraform 提供程序配置是什么?

terraform - 有没有办法防止 Terraform google_container_cluster 在没有任何变化时被破坏和重新创建?

node.js - 如何部署具有本地 npm 依赖项的 GAE 服务?

java - 有没有办法在虚拟机上开发Android

google-cloud-platform - (gcloud.run.deploy)Cloud Run错误: Container failed to start.无法启动然后监听端口

postgresql - 使用 Terraform 为 Google Cloud SQL PostgreSQL 数据库创建架构

kubernetes - 无法在 Terraform 中部署 Helm Chart(Kubernetes 集群无法访问)

google-cloud-platform - 如何解决 : Error when reading or editing Project Service Foo/container. googleapis.com: googleapi: Error 403

google-cloud-storage - 授权 Google Cloud Vision API 访问 Google Storage 镜像

google-cloud-platform - Google Cloud CDN 以存储桶为后端签署 cookie