Create cloud-function using terraform

Опубликовано: 14 Октябрь 2024
на канале: VeryLazyCoders
3,790
37

sTEPS TO FOLLOW #GCP #gcp #cloud
Install Terraform
https://learn.hashicorp.com/tutorials...
Set $path once installed

Create a Service Account in GCP and download JSON file

Add Credentials in the Provider config

========CODE========
exports.helloWorld = (req, res)  {
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};
======== MAIN.CF FILE
provider "google" {
project = "noted-hangout-293809"
region = "us-central1"
zone = "us-central1-c“
credentials = "noted-hangout-293809-fcb581e85dc6.json"
}
resource "google_storage_bucket" "bucket" {
name = "terraform-gcp-verylazycoder"
}

resource "google_storage_bucket_object" "archive" {
name = "index.zip"
bucket = google_storage_bucket.bucket.name
source = "function.zip"
}

resource "google_cloudfunctions_function" "function" {
name = "function-test"
description = "My function"
runtime = "nodejs10"

available_memory_mb = 128
source_archive_bucket = google_storage_bucket.bucket.name
source_archive_object = google_storage_bucket_object.archive.name
trigger_http = true
timeout = 60
entry_point = "helloWorld"
labels = {
my-label = "my-label-value"
}

environment_variables = {
MY_ENV_VAR = "my-env-var-value"
}
}

IAM entry for a single user to invoke the function
resource "google_cloudfunctions_function_iam_member" "invoker" {
cloud_function = google_cloudfunctions_function.function.name

role = "roles/cloudfunctions.invoker"
member = "allUsers"