forked from quiccklabs/Labs_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloud Functions: 3 Ways: Challenge Lab
109 lines (76 loc) · 2.18 KB
/
Cloud Functions: 3 Ways: Challenge Lab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
gcloud services enable \
artifactregistry.googleapis.com \
cloudfunctions.googleapis.com \
cloudbuild.googleapis.com \
eventarc.googleapis.com \
run.googleapis.com \
logging.googleapis.com \
pubsub.googleapis.com
export HTTP_FUNCTION=
export FUNCTION_NAME=
export REGION=
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$DEVSHELL_PROJECT_ID" --format='value(project_number)')
SERVICE_ACCOUNT=$(gsutil kms serviceaccount -p $PROJECT_NUMBER)
gcloud projects add-iam-policy-binding $DEVSHELL_PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT \
--role roles/pubsub.publisher
gsutil mb -l $REGION gs://$DEVSHELL_PROJECT_ID
export BUCKET="gs://$DEVSHELL_PROJECT_ID"
mkdir ~/$FUNCTION_NAME && cd $_
touch index.js && touch package.json
cat > index.js <<EOF
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('$FUNCTION_NAME', (cloudevent) => {
console.log('A new event in your Cloud Storage bucket has been logged!');
console.log(cloudevent);
});
EOF
cat > package.json <<EOF
{
"name": "nodejs-functions-gen2-codelab",
"version": "0.0.1",
"main": "index.js",
"dependencies": {
"@google-cloud/functions-framework": "^2.0.0"
}
}
EOF
gcloud functions deploy $FUNCTION_NAME \
--gen2 \
--runtime nodejs16 \
--entry-point $FUNCTION_NAME \
--source . \
--region $REGION \
--trigger-bucket $BUCKET \
--trigger-location $REGION \
--max-instances 2
==================================================================================
cd ..
mkdir ~/HTTP_FUNCTION && cd $_
touch index.js && touch package.json
cat > index.js <<EOF
const functions = require('@google-cloud/functions-framework');
functions.http('$HTTP_FUNCTION', (req, res) => {
res.status(200).send('subscribe to quikclab');
});
EOF
cat > package.json <<EOF
{
"name": "nodejs-functions-gen2-codelab",
"version": "0.0.1",
"main": "index.js",
"dependencies": {
"@google-cloud/functions-framework": "^2.0.0"
}
}
EOF
gcloud functions deploy $HTTP_FUNCTION \
--gen2 \
--runtime nodejs16 \
--entry-point $HTTP_FUNCTION \
--source . \
--region $REGION \
--trigger-http \
--timeout 600s \
--max-instances 2 \
--min-instances 1