forked from lavishsheth/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB Cloud Functions 2nd Gen: Qwik Start
228 lines (156 loc) · 4.5 KB
/
LAB Cloud Functions 2nd Gen: Qwik Start
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
Command 1:-
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 PROJECT_ID=$(gcloud config get-value project)
export REGION=
export ZONE=
gcloud config set compute/region $REGION
mkdir ~/hello-http && cd $_
touch index.js && touch package.json
cat > index.js <<EOF
const functions = require('@google-cloud/functions-framework');
functions.http('helloWorld', (req, res) => {
res.status(200).send('HTTP with Node.js in GCF 2nd gen!');
});
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 nodejs-http-function \
--gen2 \
--runtime nodejs16 \
--entry-point helloWorld \
--source . \
--region $REGION \
--trigger-http \
--timeout 600s \
--max-instances 1
Command 2:-
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:$PROJECT_ID" --format='value(project_number)')
SERVICE_ACCOUNT=$(gsutil kms serviceaccount -p $PROJECT_NUMBER)
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SERVICE_ACCOUNT \
--role roles/pubsub.publisher
mkdir ~/hello-storage && cd $_
touch index.js && touch package.json
cat > index.js <<EOF
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('helloStorage', (cloudevent) => {
console.log('Cloud Storage event with Node.js in GCF 2nd gen!');
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
BUCKET="gs://gcf-gen2-storage-$PROJECT_ID"
gsutil mb -l $REGION $BUCKET
gcloud functions deploy nodejs-storage-function \
--gen2 \
--runtime nodejs16 \
--entry-point helloStorage \
--source . \
--region $REGION \
--trigger-bucket $BUCKET \
--trigger-location $REGION \
--max-instances 1
Command 3:-
From the Navigation Menu, go to IAM & Admin > Audit Logs
Find the Compute Engine API and click the check box next to it.
On the info pane on the right, check Admin Read, Data Read, and Data Write log types and click Save.
Command 4:-
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:[email protected] \
--role roles/eventarc.eventReceiver
cd ~
git clone https://github.com/GoogleCloudPlatform/eventarc-samples.git
cd ~/eventarc-samples/gce-vm-labeler/gcf/nodejs
gcloud functions deploy gce-vm-labeler \
--gen2 \
--runtime nodejs16 \
--entry-point labelVmCreation \
--source . \
--region $REGION \
--trigger-event-filters="type=google.cloud.audit.log.v1.written,serviceName=compute.googleapis.com,methodName=beta.compute.instances.insert" \
--trigger-location $REGION \
--max-instances 1
gcloud compute instances create instance-1 --zone=$ZONE
mkdir ~/hello-world-colored && cd $_
touch main.py
cat > main.py <<EOF
import os
color = os.environ.get('COLOR')
def hello_world(request):
return f'<body style="background-color:{color}"><h1>Hello World!</h1></body>'
EOF
COLOR=yellow
gcloud functions deploy hello-world-colored \
--gen2 \
--runtime python39 \
--entry-point hello_world \
--source . \
--region $REGION \
--trigger-http \
--allow-unauthenticated \
--update-env-vars COLOR=$COLOR \
--max-instances 1
mkdir ~/min-instances && cd $_
touch main.go
cat > main.go <<EOF
package p
import (
"fmt"
"net/http"
"time"
)
func init() {
time.Sleep(10 * time.Second)
}
func HelloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Slow HTTP Go in GCF 2nd gen!")
}
EOF
gcloud functions deploy slow-function \
--gen2 \
--runtime go116 \
--entry-point HelloWorld \
--source . \
--region $REGION \
--trigger-http \
--allow-unauthenticated \
--min-instances 1 \
--max-instances 4
gcloud functions call slow-function \
--gen2 --region $REGION
SLOW_URL=$(gcloud functions describe slow-function --region $REGION --gen2 --format="value(serviceConfig.uri)")
hey -n 10 -c 10 $SLOW_URL
gcloud functions deploy slow-concurrent-function \
--gen2 \
--runtime go116 \
--entry-point HelloWorld \
--source . \
--region $REGION \
--trigger-http \
--allow-unauthenticated \
--min-instances 1 \
--max-instances 4