【问题标题】:Deploy google function with terraform使用 terraform 部署 google 功能
【发布时间】:2019-05-02 15:54:25
【问题描述】:

我正在尝试使用 terraform 部署谷歌云功能。该函数需要对该函数进行压缩。我需要一个带有 terraform 和 nodejs 的 hello world 项目。过去几天我一直在尝试设置它,但没有成功。

【问题讨论】:

  • 您到底尝试了什么,遇到了什么问题?如果您可以展示您的代码、您采取的步骤以及您遇到的错误,这将有助于人们回答您的问题。

标签: google-cloud-platform terraform


【解决方案1】:

Terraform 示例

resource "google_cloudfunctions_function" "test" {
    name                      = "[FunctionName]"
    entry_point               = "helloGET"
    available_memory_mb       = 128
    timeout                   = 61
    project                   = "[GCPProjectName]"
    region                    = "us-central1"
    trigger_http              = true
    trigger_topic             = "[PubSubTopic]"
    trigger_bucket            = "[StorageBucketName]"
    source_archive_bucket     = "${google_storage_bucket.bucket.name}"
    source_archive_object     = "${google_storage_bucket_object.archive.name}"
    labels {
    deployment_name           = "test"
    }
}

resource "google_storage_bucket" "bucket" {
  name = "cloudfunction-deploy-test1"
}

data "archive_file" "http_trigger" {
  type        = "zip"
  output_path = "${path.module}/files/http_trigger.zip"
  source {
    content  = "${file("${path.module}/files/http_trigger.js")}"
    filename = "index.js"
  }
}

resource "google_storage_bucket_object" "archive" {
  name   = "http_trigger.zip"
  bucket = "${google_storage_bucket.bucket.name}"
  source = "${path.module}/files/http_trigger.zip"
  depends_on = ["data.archive_file.http_trigger"]
}

示例 nodejs

/**
 * HTTP Cloud Function.
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.helloGET = function helloGET (req, res) {
    res.send(`Hello ${req.body.name || 'World'}!`);
};

【讨论】:

  • 您好,谢谢您的回答。你如何压缩捆绑包?只是构建然后压缩它?
  • @aphex 如果 nodejs 源文件在正确的相对路径中(参见 archive_file data 块中的 source),terraform 将为您压缩!
  • 是的,但是文件叫什么,如何在 terraform 模板中引用它???
  • 在定义模块的同一目录中,模板正在寻找files/http_trigger.js(参见archive_file.http_triggercontent。然后该文件在index.js内重命名为zip,然后部署到存储桶中
猜你喜欢
  • 2020-10-16
  • 2019-10-20
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2021-12-24
相关资源
最近更新 更多