...
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
18locals {
19 project = "my-project-name" # Google Cloud Platform Project ID
20}
21
22resource "google_storage_bucket" "bucket" {
23 name = "${local.project}-gcf-source" # Every bucket name must be globally unique
24 location = "US"
25 uniform_bucket_level_access = true
26}
27
28resource "google_storage_bucket_object" "object" {
29 name = "function-source.zip"
30 bucket = google_storage_bucket.bucket.name
31 source = "function-source.zip" # Add path to the zipped function source code
32}
33
34resource "google_cloudfunctions2_function" "function" {
35 name = "function-secret"
36 location = "us-central1"
37 description = "a new function"
38
39 build_config {
40 runtime = "nodejs16"
41 entry_point = "helloHttp" # Set the entry point
42 source {
43 storage_source {
44 bucket = google_storage_bucket.bucket.name
45 object = google_storage_bucket_object.object.name
46 }
47 }
48 }
49
50 service_config {
51 max_instance_count = 1
52 available_memory = "256M"
53 timeout_seconds = 60
54
55 secret_volumes {
56 mount_path = "/etc/secrets"
57 project_id = local.project
58 secret = google_secret_manager_secret.secret.secret_id
59 }
60 }
61 depends_on = [google_secret_manager_secret_version.secret]
62}
63
64resource "google_secret_manager_secret" "secret" {
65 secret_id = "secret"
66
67 replication {
68 user_managed {
69 replicas {
70 location = "us-central1"
71 }
72 }
73 }
74}
75
76resource "google_secret_manager_secret_version" "secret" {
77 secret = google_secret_manager_secret.secret.name
78
79 secret_data = "secret"
80 enabled = true
81}
82```
View as plain text