...
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
18resource "google_cloud_run_v2_service" "default" {
19 name = "cloudrun-service"
20 location = "us-central1"
21 ingress = "INGRESS_TRAFFIC_ALL"
22
23 template {
24 scaling {
25 max_instance_count = 2
26 }
27
28 volumes {
29 name = "cloudsql"
30 cloud_sql_instance {
31 instances = [google_sql_database_instance.instance.connection_name]
32 }
33 }
34
35 containers {
36 image = "us-docker.pkg.dev/cloudrun/container/hello"
37
38 env {
39 name = "FOO"
40 value = "bar"
41 }
42 env {
43 name = "SECRET_ENV_VAR"
44 value_source {
45 secret_key_ref {
46 secret = google_secret_manager_secret.secret.secret_id
47 version = "1"
48 }
49 }
50 }
51 volume_mounts {
52 name = "cloudsql"
53 mount_path = "/cloudsql"
54 }
55 }
56 }
57
58 traffic {
59 type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
60 percent = 100
61 }
62 depends_on = [google_secret_manager_secret_version.secret-version-data]
63}
64
65data "google_project" "project" {
66}
67
68resource "google_secret_manager_secret" "secret" {
69 secret_id = "secret-1"
70 replication {
71 automatic = true
72 }
73}
74
75resource "google_secret_manager_secret_version" "secret-version-data" {
76 secret = google_secret_manager_secret.secret.name
77 secret_data = "secret-data"
78}
79
80resource "google_secret_manager_secret_iam_member" "secret-access" {
81 secret_id = google_secret_manager_secret.secret.id
82 role = "roles/secretmanager.secretAccessor"
83 member = "serviceAccount:${data.google_project.project.number}-compute@developer.gserviceaccount.com"
84 depends_on = [google_secret_manager_secret.secret]
85}
86
87resource "google_sql_database_instance" "instance" {
88 name = "cloudrun-sql"
89 region = "us-central1"
90 database_version = "MYSQL_5_7"
91 settings {
92 tier = "db-f1-micro"
93 }
94
95 deletion_protection = "true"
96}
97```
View as plain text