...
1/**
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17```hcl
18# [START functions_v2_full]
19locals {
20 project = "my-project-name" # Google Cloud Platform Project ID
21}
22
23resource "google_service_account" "account" {
24 account_id = "gcf-sa"
25 display_name = "Test Service Account"
26}
27
28resource "google_pubsub_topic" "topic" {
29 name = "functions2-topic"
30}
31
32resource "google_storage_bucket" "bucket" {
33 name = "${local.project}-gcf-source" # Every bucket name must be globally unique
34 location = "US"
35 uniform_bucket_level_access = true
36}
37
38resource "google_storage_bucket_object" "object" {
39 name = "function-source.zip"
40 bucket = google_storage_bucket.bucket.name
41 source = "function-source.zip" # Add path to the zipped function source code
42}
43
44resource "google_cloudfunctions2_function" "function" {
45 name = "gcf-function"
46 location = "us-central1"
47 description = "a new function"
48
49 build_config {
50 runtime = "nodejs16"
51 entry_point = "helloPubSub" # Set the entry point
52 environment_variables = {
53 BUILD_CONFIG_TEST = "build_test"
54 }
55 source {
56 storage_source {
57 bucket = google_storage_bucket.bucket.name
58 object = google_storage_bucket_object.object.name
59 }
60 }
61 }
62
63 service_config {
64 max_instance_count = 3
65 min_instance_count = 1
66 available_memory = "4Gi"
67 timeout_seconds = 60
68 max_instance_request_concurrency = 80
69 available_cpu = "4"
70 environment_variables = {
71 SERVICE_CONFIG_TEST = "config_test"
72 }
73 ingress_settings = "ALLOW_INTERNAL_ONLY"
74 all_traffic_on_latest_revision = true
75 service_account_email = google_service_account.account.email
76 }
77
78 event_trigger {
79 trigger_region = "us-central1"
80 event_type = "google.cloud.pubsub.topic.v1.messagePublished"
81 pubsub_topic = google_pubsub_topic.topic.id
82 retry_policy = "RETRY_POLICY_RETRY"
83 }
84}
85# [END functions_v2_full]
86```
View as plain text