使用 terraform 将 spring-boot jar 上传到 GCE 实例

Upload spring-boot jar into GCE Instance using terraform

将一些-spring-boot.jar 复制到 GCE 实例中的最佳方法是什么?

我认为这是一项很常见的任务,但在谷歌搜索后发现关于它的文档为零。

我已经在 GCP 中生成了服务帐户并将所有内容保存到 gcp-credentials.json :

{
  "type": "service_account",
  "project_id": "some project",
  "private_key_id": "someid",
  "private_key": "some key",
  "client_email": "xxx@developer.gserviceaccount.com",
  "client_id": "xxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxdeveloper.gserviceaccount.com"
}

这是我在谷歌上搜索了几个小时后所做的事情:

terraform {
    required_providers {
        google = {
            source = "hashicorp/google"
        }
    }
}

provider "google" {
    version = "3.5.0"
    credentials = file("gcp-credentials.json")
    project = "some project"
    zone = "us-central1-a"
}

resource "google_compute_instance" "vm_instance" {
    name = "my-instance"
    machine_type = "e2-medium"
    #metadata_startup_script = file("startup.sh")

    tags = [
        "http-server",
        "https-server"]

    boot_disk {
        initialize_params {
            image = "some image with jdk"
        }
    }

    network_interface {
        network = "default"
        access_config {}
    }

    provisioner "file" {
        source = "../../../target/my-jar-1.0.0.jar"
        destination = "/tmp/my-jar-1.0.0.jar"

        connection {
            type = "ssh"
            user = "root"
            private_key = file("gcp-credentials.json").private_key
            agent = "false"
        }
    }
}

每当我 运行 它说

[ERROR] Error: Missing required argument
[ERROR] 
[ERROR]   on main.tf line 43, in resource "google_compute_instance" "vm_instance":
[ERROR]   43:         connection {
[ERROR] 
[ERROR] The argument "host" is required, but no definition was found.

任何帮助将不胜感激

未测试(我也从未将 Terraform 用于 GCP),尝试:

host = self.network_interface.0.network_ip

思路:

  • host 确实似乎是必需的(根据 docs for connection
  • 相同的文档提示特殊 self 变量:

Expressions in connection blocks cannot refer to their parent resource by name. Instead, they can use the special self object.

The self object represents the connection's parent resource, and has all of that resource's attributes.

  • 剩下的是我猜测如何从 GCP 实例获取 IP 地址