0
0
GCPcloud~10 mins

Cloud Functions with Pub/Sub triggers in GCP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the trigger type for a Cloud Function.

GCP
gcloud functions deploy myFunction --gen2 --trigger-[1] my-topic --runtime python312
Drag options to blanks, or click blank then click option'
Ahttp
Beventarc
Cpubsub
Dstorage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'pubsub' for Pub/Sub triggers.
Confusing storage triggers with Pub/Sub triggers.
2fill in blank
medium

Complete the code to specify the Pub/Sub topic name for the trigger.

GCP
gcloud functions deploy myFunction --gen2 --trigger-pubsub [1] --runtime python312
Drag options to blanks, or click blank then click option'
Aprojects/my-project/topics/my-topic
Bmy-topic
Cprojects/my-project/my-topic
Dtopics/my-topic
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the topic name without the full path.
Omitting the 'topics' part in the path.
3fill in blank
hard

Fix the error in the function code to correctly decode the Pub/Sub message data.

GCP
def my_function(event, context):
    import base64
    message = base64.b64decode(event['message']['[1]']).decode('utf-8')
    print(f"Received message: {message}")
Drag options to blanks, or click blank then click option'
Amessage
Battributes
Cpayload
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' or 'payload' instead of 'data'.
Trying to decode a non-existent key.
4fill in blank
hard

Fill both blanks to create a Cloud Function that logs the message ID and publish time.

GCP
def my_function(event, context):
    message_id = event['message']['[1]']
    publish_time = event['message']['[2]']
    print(f"Message ID: {message_id}, Published at: {publish_time}")
Drag options to blanks, or click blank then click option'
Aevent_id
Btimestamp
CpublishTime
DmessageId
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'event_id' instead of 'messageId'.
Using 'timestamp' instead of 'publishTime'.
5fill in blank
hard

Fill all three blanks to deploy a Cloud Function with a Pub/Sub trigger, specifying the runtime and entry point.

GCP
gcloud functions deploy [1] --gen2 --runtime [2] --trigger-pubsub [3] --entry-point my_function
Drag options to blanks, or click blank then click option'
AmyFunction
Bpython312
Cprojects/my-project/topics/my-topic
Dnodejs20
Attempts:
3 left
💡 Hint
Common Mistakes
Using a runtime that does not match the language.
Omitting the full topic path.