|
6 | 6 | import database
|
7 | 7 | import git
|
8 | 8 | import pulumi
|
9 |
| -from pulumi_gcp import cloudfunctions, cloudtasks, projects, serviceaccount, storage |
| 9 | +from pulumi_gcp import ( |
| 10 | + cloudfunctionsv2, |
| 11 | + cloudrun, |
| 12 | + cloudtasks, |
| 13 | + projects, |
| 14 | + serviceaccount, |
| 15 | + storage, |
| 16 | +) |
10 | 17 | from utils import construct_name, pulumi_create_zip
|
| 18 | +from vpc_connector import vpc_connector |
11 | 19 |
|
12 | 20 | stack = pulumi.get_stack()
|
13 | 21 | # We will store the source code to the Cloud Function in a Google Cloud Storage bucket.
|
|
57 | 65 |
|
58 | 66 | function_name = construct_name("cf-asa")
|
59 | 67 | config_values = {
|
60 |
| - "DB_URL": database.sql_instance_url_with_asyncpg, |
| 68 | + "DB_URL": database.sql_instance_url_with_ip_asyncpg, |
61 | 69 | "GIT_HASH": git_sha,
|
62 | 70 | "GIT_TAG": git_tag,
|
63 | 71 | }
|
|
72 | 80 | archive = package.apply(lambda x: pulumi.FileAsset(x))
|
73 | 81 |
|
74 | 82 | # Create the single Cloud Storage object, which contains all of the function's
|
75 |
| -# source code. ("main.py" and "requirements.txt".) |
| 83 | +# source code. |
76 | 84 | source_archive_object = storage.BucketObject(
|
77 | 85 | construct_name("source-cf-asa"),
|
78 | 86 | name=f"handler.py-asa-{time.time():f}",
|
|
96 | 104 | ),
|
97 | 105 | )
|
98 | 106 |
|
99 |
| -gfw_credentials = cloudfunctions.FunctionSecretEnvironmentVariableArgs( |
100 |
| - key="GOOGLE_APPLICATION_CREDENTIALS", |
101 |
| - secret=pulumi.Config("ais").require("credentials"), |
102 |
| - version="latest", |
103 |
| - project_id=pulumi.Config("gcp").require("project"), |
104 |
| -) |
| 107 | +# Define secret environment variables |
| 108 | +gfw_credentials = { |
| 109 | + "key": "GOOGLE_APPLICATION_CREDENTIALS", |
| 110 | + "secret": pulumi.Config("ais").require("credentials"), |
| 111 | + "version": "latest", |
| 112 | + "project_id": pulumi.Config("gcp").require("project"), |
| 113 | +} |
| 114 | +infra_api_key = { |
| 115 | + "key": "INFRA_API_TOKEN", |
| 116 | + "secret": pulumi.Config("cerulean-cloud").require("infra_keyname"), |
| 117 | + "version": "latest", |
| 118 | + "project_id": pulumi.Config("gcp").require("project"), |
| 119 | +} |
| 120 | +api_key = { |
| 121 | + "key": "API_KEY", |
| 122 | + "secret": pulumi.Config("cerulean-cloud").require("keyname"), |
| 123 | + "version": "latest", |
| 124 | + "project_id": pulumi.Config("gcp").require("project"), |
| 125 | +} |
105 | 126 |
|
106 |
| -infra_api_key = cloudfunctions.FunctionSecretEnvironmentVariableArgs( |
107 |
| - key="INFRA_API_TOKEN", |
108 |
| - secret=pulumi.Config("cerulean-cloud").require("infra_keyname"), |
109 |
| - version="latest", |
110 |
| - project_id=pulumi.Config("gcp").require("project"), |
111 |
| -) |
112 | 127 |
|
113 |
| -api_key = cloudfunctions.FunctionSecretEnvironmentVariableArgs( |
114 |
| - key="API_KEY", |
115 |
| - secret=pulumi.Config("cerulean-cloud").require("keyname"), |
116 |
| - version="latest", |
117 |
| - project_id=pulumi.Config("gcp").require("project"), |
118 |
| -) |
119 |
| - |
120 |
| -fxn = cloudfunctions.Function( |
| 128 | +# Create the Cloud Function (Gen2) |
| 129 | +fxn = cloudfunctionsv2.Function( |
121 | 130 | function_name,
|
122 | 131 | name=function_name,
|
123 |
| - entry_point="main", |
124 |
| - environment_variables=config_values, |
125 |
| - region=pulumi.Config("gcp").require("region"), |
126 |
| - runtime="python39", |
127 |
| - source_archive_bucket=bucket.name, |
128 |
| - source_archive_object=source_archive_object.name, |
129 |
| - trigger_http=True, |
130 |
| - service_account_email=cloud_function_service_account.email, |
131 |
| - available_memory_mb=4096, |
132 |
| - timeout=540, |
133 |
| - secret_environment_variables=[ |
134 |
| - gfw_credentials, |
135 |
| - infra_api_key, |
136 |
| - api_key, |
137 |
| - ], |
| 132 | + location=pulumi.Config("gcp").require("region"), |
| 133 | + description="Cloud Function for ASA", |
| 134 | + build_config={ |
| 135 | + "runtime": "python39", |
| 136 | + "entry_point": "main", |
| 137 | + "source": { |
| 138 | + "storage_source": { |
| 139 | + "bucket": bucket.name, |
| 140 | + "object": source_archive_object.name, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + service_config={ |
| 145 | + "environment_variables": config_values, |
| 146 | + "available_memory": "4096M", |
| 147 | + "timeout_seconds": 540, |
| 148 | + "service_account_email": cloud_function_service_account.email, |
| 149 | + "secret_environment_variables": [gfw_credentials, infra_api_key, api_key], |
| 150 | + "vpc_connector": vpc_connector.id, |
| 151 | + }, |
138 | 152 | opts=pulumi.ResourceOptions(
|
139 | 153 | depends_on=[cloud_function_service_account_iam],
|
140 | 154 | ),
|
141 | 155 | )
|
142 | 156 |
|
143 |
| -invoker = cloudfunctions.FunctionIamMember( |
| 157 | +invoker = cloudfunctionsv2.FunctionIamMember( |
144 | 158 | construct_name("cf-asa-invoker"),
|
145 | 159 | project=fxn.project,
|
146 |
| - region=fxn.region, |
| 160 | + location=fxn.location, |
147 | 161 | cloud_function=fxn.name,
|
148 | 162 | role="roles/cloudfunctions.invoker",
|
149 | 163 | member="allUsers",
|
150 | 164 | )
|
| 165 | + |
| 166 | +cloud_run_invoker = cloudrun.IamMember( |
| 167 | + "cf-asa-run-invoker", |
| 168 | + project=fxn.project, |
| 169 | + location=fxn.location, |
| 170 | + service=fxn.name, |
| 171 | + role="roles/run.invoker", |
| 172 | + member="allUsers", |
| 173 | +) |
0 commit comments