首页 > 解决方案 > 使用 terraform 部署 google 功能

问题描述

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

标签: google-cloud-platformterraform

解决方案


样本 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'}!`);
};

推荐阅读