Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] Lesson 6: Google Cloud Function with Terraform

Posted on April 12, 2022April 18, 2022 By nim No Comments on [Terraform] Lesson 6: Google Cloud Function with Terraform

Contents

Toggle
  • 1) Deploy Cloud Function – Console
  • 2) Cloud Function – Terraform
    • 2.1) Preparing
    • 2.2) Create Bucket
    • 2.3) Deploy Function
    • 2.4) allow public access

1) Deploy Cloud Function – Console

Chọn 1 tên cho đặt cho Func
bạn có thể đổi region nếu muốn.
Cấu hình chi tiết hơn
bạn set RAM, thời gian timeout, auto scale luôn.
KHi xong thì anh em nhấn NEXT
Giờ chúng ta được 1 màn hình có sẵn như trên.
mình sài Inline Code có sẵn
Ngoài ra còn hỗ trợ rất nhiều loại ngôn ngữ
Bạn cũng có thể upload code lên đây

file: index.js

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorldNimtechnology = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World From Nimtechnology!';
  res.status(200).send(message);
};

file package.json

{
  "name": "sample-http",
  "version": "0.0.1"
}

Giờ các bạn ấn deploy luôn nhé

Waiting for GCP
Giờ khám phá thôi
Khám phát testing
Đã truy cập được

Chúng ta sẽ kiểm tra permissions

chúng ta thấy để access được là nhờ allUsers và role là Cloud Functions Invoker

2) Cloud Function – Terraform

Sau đây mình cung cấp full code

#Create Bucket
#Upload index.zip
#deploy function
#policy binding

resource "google_storage_bucket" "fun_bucket" {
  name = "fun_bucket_tf"
}

resource "google_storage_bucket_object" "srccode" {
  name = "index.zip"
  bucket = google_storage_bucket.fun_bucket.name
  source = "index.zip"
}

resource "google_cloudfunctions_function" "fun_from_tf" {
  name = "fun-from-tf"
  runtime = "nodejs14"
  description = "This is my first function from terraform script."

  available_memory_mb = 128
  source_archive_bucket = google_storage_bucket.fun_bucket.name
  source_archive_object = google_storage_bucket_object.srccode.name

  trigger_http = true
  entry_point = "helloWorldtf"

}

resource "google_cloudfunctions_function_iam_member" "allowaccess" {
  region = google_cloudfunctions_function.fun_from_tf.region
  cloud_function = google_cloudfunctions_function.fun_from_tf.name

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

}

2.1) Preparing

Bạn cần tạo folder tên là index và trong đó có 2 file

File index.js

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorldtf = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World! from terraform';
  res.status(200).send(message);
};

Tiếp theo là file package.json

{
  "name": "sample-http",
  "version": "0.0.1"
}

Bạn sẽ cần nén nó lại dưới duôi .zip

2.2) Create Bucket

Giờ file main.tf như dứoi:

resource "google_storage_bucket" "fun_bucket" {
  name = "fun_bucket_nimtechnology_tf"
}

resource "google_storage_bucket_object" "srccode" {
  name = "index.zip"
  bucket = google_storage_bucket.fun_bucket.name
  source = "index@local.zip"
}

source = "index@local.zip" : là file dưới local của bạn
name = "index.zip": là name khi bạn đẩy lên bucket gcp

Console đã báo done
Đã thấy tạo bucket
Chúng ta đã upload file .zip thành công

2.3) Deploy Function

Giờ chúng ta đã có file trên bucket rồi thì deploy function thôi.

resource "google_storage_bucket" "fun_bucket" {
  name = "fun_bucket_nimtechnology_tf"
}

resource "google_storage_bucket_object" "srccode" {
  name = "index.zip"
  bucket = google_storage_bucket.fun_bucket.name
  source = "index@local.zip"
}

resource "google_cloudfunctions_function" "fun_from_tf" {
  name = "fun-from-tf"
  runtime = "nodejs14"
  description = "This is my first function from terraform script."

  available_memory_mb = 128
  source_archive_bucket = google_storage_bucket.fun_bucket.name
  source_archive_object = google_storage_bucket_object.srccode.name

  trigger_http = true
  entry_point = "helloWorldtf"

}

Nhìn thế này các bạn cũng sẽ đoán đoán được chức năng của từng dòng.
Hiểu sâu ta lại lên terraform.
Giờ thì apply thôi!

Error: googleapi: Error 403: Permission ‘cloudfunctions.functions.create‘ denied on resource ‘projects/terraform-gcp-346216/locations/us-central1/functions/fun-from-tf’ (or resource may not exist)., forbidden

Bạn sẽ bị thiếu quyền giờ chúng ta vào role search

rồi chạy terraform apply lại

Console như này là ngon
Bạn thấy cloud func đã lên và lấy đúng code.
giờ chúng ta thử truy cập
chặn ngay.

2.4) allow public access

resource "google_storage_bucket" "fun_bucket" {
  name = "fun_bucket_nimtechnology_tf"
}

resource "google_storage_bucket_object" "srccode" {
  name = "index.zip"
  bucket = google_storage_bucket.fun_bucket.name
  source = "index@local.zip"
}

resource "google_cloudfunctions_function" "fun_from_tf" {
  name = "fun-from-tf"
  runtime = "nodejs14"
  description = "This is my first function from terraform script."

  available_memory_mb = 128
  source_archive_bucket = google_storage_bucket.fun_bucket.name
  source_archive_object = google_storage_bucket_object.srccode.name

  trigger_http = true
  entry_point = "helloWorldtf"

}

resource "google_cloudfunctions_function_iam_member" "allowaccess" {
  region = google_cloudfunctions_function.fun_from_tf.region
  cloud_function = google_cloudfunctions_function.fun_from_tf.name

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

}

google_cloudfunctions_function_iam

Three different resources help you manage your IAM policy for Cloud Functions CloudFunction. Each of these resources serves a different use case:

  • google_cloudfunctions_function_iam_policy: Authoritative. Sets the IAM policy for the cloudfunction and replaces any existing policy already attached.
  • google_cloudfunctions_function_iam_binding: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the cloudfunction are preserved.
  • google_cloudfunctions_function_iam_member: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the cloudfunction are preserved.

Trước khi apply thì show permission để chúng ta cũng so sánh

Sau khi apply

OK chưa nà
Đã truy cập được rồi nà
GCP - Google Cloud, Terraform

Post navigation

Previous Post: [Terraform] Lesson 5: Google Cloud Run with Terraform
Next Post: [Javascipt] Get class or id of select – option with JQuery

More Related Articles

[Terraform ] Using Terragrunt to provision AWS base on Terraform Module Terraform
[Terraform] Lesson 10: NoSQL Database in GCP with Terraform GCP - Google Cloud
[Terraform] Lesson 4: Google Compute Engine (VM – Virtual Machine) with Terraform GCP - Google Cloud
[Terraform/GCP/K8s] How can we build/install the Kubernetes clusters (GKE) on Google Cloud. GCP - Google Cloud
[Terraform] How to create or public a module terraform on the public registry Terraform
[Terraform] Error: Error acquiring the state lock AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025
  • [Teamcity] How to transfer the value from pipeline A to pipeline B June 9, 2025
  • [Windows] Remove the process that consumes too much CPU. June 3, 2025
  • Deploying Web-Based File Managers: File Browser and KubeFileBrowser with Docker and Kubernetes June 3, 2025

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.