Skip to content

Commit bfd3868

Browse files
authored
Feat/gen2 attempt2 (#145)
* try with all the docs available * update url parameter * Add more perms * try adding instance connection name * Revert "try adding instance connection name" This reverts commit 740ac55. * add vpc connector? * names * try common vpc connector * change cidr block * import instance * stack names for vpc connector * too many characters * add ip address connection string * try again * oops * correct the connection string * missing psycopg2-binary? * cloudfunctions gen 2 updated???
1 parent 46efc82 commit bfd3868

9 files changed

+208
-96
lines changed

cerulean_cloud/cloud_function_asa/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ pandas
1919
requests
2020
pyproj
2121
google-auth
22-
pydantic<2.0
22+
pydantic<2.0
23+
psycopg2-binary

stack/__main__.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
pulumi.export("database_url", database.sql_instance_url)
2424
pulumi.export("database_instance_name", database.instance.connection_name)
2525
pulumi.export("database_url_alembic", database.sql_instance_url_alembic)
26-
pulumi.export(
27-
"scene_relevancy_url", cloud_function_scene_relevancy.fxn.https_trigger_url
28-
)
29-
pulumi.export("historical_run_url", cloud_function_historical_run.fxn.https_trigger_url)
30-
pulumi.export("asa_url", cloud_function_asa.fxn.https_trigger_url)
26+
pulumi.export("scene_relevancy_url", cloud_function_scene_relevancy.fxn.url)
27+
pulumi.export("historical_run_url", cloud_function_historical_run.fxn.url)
28+
pulumi.export("asa_url", cloud_function_asa.fxn.url)
3129
pulumi.export("sns_topic_subscription", sns_subscription.sentinel1_sqs_target.arn)

stack/cloud_function_asa.py

+63-40
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
import database
77
import git
88
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+
)
1017
from utils import construct_name, pulumi_create_zip
18+
from vpc_connector import vpc_connector
1119

1220
stack = pulumi.get_stack()
1321
# We will store the source code to the Cloud Function in a Google Cloud Storage bucket.
@@ -57,7 +65,7 @@
5765

5866
function_name = construct_name("cf-asa")
5967
config_values = {
60-
"DB_URL": database.sql_instance_url_with_asyncpg,
68+
"DB_URL": database.sql_instance_url_with_ip_asyncpg,
6169
"GIT_HASH": git_sha,
6270
"GIT_TAG": git_tag,
6371
}
@@ -72,7 +80,7 @@
7280
archive = package.apply(lambda x: pulumi.FileAsset(x))
7381

7482
# Create the single Cloud Storage object, which contains all of the function's
75-
# source code. ("main.py" and "requirements.txt".)
83+
# source code.
7684
source_archive_object = storage.BucketObject(
7785
construct_name("source-cf-asa"),
7886
name=f"handler.py-asa-{time.time():f}",
@@ -96,55 +104,70 @@
96104
),
97105
)
98106

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+
}
105126

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-
)
112127

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(
121130
function_name,
122131
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+
},
138152
opts=pulumi.ResourceOptions(
139153
depends_on=[cloud_function_service_account_iam],
140154
),
141155
)
142156

143-
invoker = cloudfunctions.FunctionIamMember(
157+
invoker = cloudfunctionsv2.FunctionIamMember(
144158
construct_name("cf-asa-invoker"),
145159
project=fxn.project,
146-
region=fxn.region,
160+
location=fxn.location,
147161
cloud_function=fxn.name,
148162
role="roles/cloudfunctions.invoker",
149163
member="allUsers",
150164
)
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+
)

stack/cloud_function_historical_run.py

+47-24
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
import cloud_run_orchestrator
77
import database
88
import pulumi
9-
from pulumi_gcp import cloudfunctions, storage
9+
from pulumi_gcp import (
10+
cloudfunctionsv2,
11+
cloudrun,
12+
storage,
13+
)
1014
from utils import construct_name, pulumi_create_zip
15+
from vpc_connector import vpc_connector
1116

1217
stack = pulumi.get_stack()
1318

1419
function_name = construct_name("cf-historical-run")
1520
config_values = {
16-
"DB_URL": database.sql_instance_url,
21+
"DB_URL": database.sql_instance_url_with_ip,
1722
"GCPPROJECT": pulumi.Config("gcp").require("project"),
1823
"GCPREGION": pulumi.Config("gcp").require("region"),
1924
"QUEUE": cloud_function_scene_relevancy.queue.name,
@@ -25,53 +30,71 @@
2530
}
2631

2732
# The Cloud Function source code itself needs to be zipped up into an
28-
# archive, which we create using the pulumi.AssetArchive primitive.
33+
# archive.
2934
PATH_TO_SOURCE_CODE = "../cerulean_cloud/cloud_function_historical_run"
3035
package = pulumi_create_zip(
3136
dir_to_zip=PATH_TO_SOURCE_CODE,
3237
zip_filepath="../cloud_function_historical_run.zip",
3338
)
3439
archive = package.apply(lambda x: pulumi.FileAsset(x))
3540

36-
# Create the single Cloud Storage object, which contains all of the function's
37-
# source code. ("main.py" and "requirements.txt".)
41+
# Create the Cloud Storage object containing the function's source code.
3842
source_archive_object = storage.BucketObject(
3943
construct_name("source-cf-historical-run"),
4044
name=f"handler.py-hr-{time.time():f}",
4145
bucket=cloud_function_scene_relevancy.bucket.name,
4246
source=archive,
4347
)
4448

45-
apikey = cloudfunctions.FunctionSecretEnvironmentVariableArgs(
46-
key="API_KEY",
47-
secret=pulumi.Config("cerulean-cloud").require("keyname"),
48-
version="latest",
49-
project_id=pulumi.Config("gcp").require("project"),
50-
)
49+
apikey = {
50+
"key": "API_KEY",
51+
"secret": pulumi.Config("cerulean-cloud").require("keyname"),
52+
"version": "latest",
53+
"project_id": pulumi.Config("gcp").require("project"),
54+
}
5155

52-
fxn = cloudfunctions.Function(
56+
# Create the Cloud Function (Gen2)
57+
fxn = cloudfunctionsv2.Function(
5358
function_name,
5459
name=function_name,
55-
entry_point="main",
56-
environment_variables=config_values,
57-
region=pulumi.Config("gcp").require("region"),
58-
runtime="python39",
59-
source_archive_bucket=cloud_function_scene_relevancy.bucket.name,
60-
source_archive_object=source_archive_object.name,
61-
trigger_http=True,
62-
service_account_email=cloud_function_scene_relevancy.cloud_function_service_account.email,
63-
timeout=500,
64-
secret_environment_variables=[apikey],
60+
location=pulumi.Config("gcp").require("region"),
61+
description="Cloud Function for Historical Run",
62+
build_config={
63+
"runtime": "python39",
64+
"entry_point": "main",
65+
"source": {
66+
"storage_source": {
67+
"bucket": cloud_function_scene_relevancy.bucket.name,
68+
"object": source_archive_object.name,
69+
},
70+
},
71+
},
72+
service_config={
73+
"environment_variables": config_values,
74+
"timeout_seconds": 500,
75+
"service_account_email": cloud_function_scene_relevancy.cloud_function_service_account.email,
76+
"secret_environment_variables": [apikey],
77+
"vpc_connector": vpc_connector.id,
78+
},
6579
opts=pulumi.ResourceOptions(
6680
depends_on=[cloud_function_scene_relevancy.cloud_function_service_account_iam],
6781
),
6882
)
6983

70-
invoker = cloudfunctions.FunctionIamMember(
84+
invoker = cloudfunctionsv2.FunctionIamMember(
7185
construct_name("cf-historical-run-invoker"),
7286
project=fxn.project,
73-
region=fxn.region,
87+
location=fxn.location,
7488
cloud_function=fxn.name,
7589
role="roles/cloudfunctions.invoker",
7690
member="allUsers",
7791
)
92+
93+
cloud_run_invoker = cloudrun.IamMember(
94+
"cf-historical-run-run-invoker",
95+
project=fxn.project,
96+
location=fxn.location,
97+
service=fxn.name,
98+
role="roles/run.invoker",
99+
member="allUsers",
100+
)

0 commit comments

Comments
 (0)