0
0
GCPcloud~7 mins

Cloud Functions with Pub/Sub triggers in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your code to run automatically when a message is sent to a topic. Cloud Functions with Pub/Sub triggers let you do this by running your code whenever a new message arrives on a Pub/Sub topic.
When you want to process data as soon as it is published to a messaging topic without manual intervention.
When you need to run small pieces of code in response to events from other services or applications.
When you want to decouple your application components so they communicate through messages.
When you want to automatically trigger workflows based on messages sent by other systems.
When you want to scale your processing automatically based on the number of messages.
Config File - function-source/main.py
function-source/main.py
def hello_pubsub(event, context):
    import base64
    if 'data' in event:
        message = base64.b64decode(event['data']).decode('utf-8')
        print(f'Received message: {message}')
    else:
        print('No data in message')

This Python file contains the Cloud Function code. The hello_pubsub function runs when a message arrives. It decodes the message data from base64 and prints it. If no data is present, it prints a different message.

Commands
Create a Pub/Sub topic named 'example-topic' where messages will be sent to trigger the function.
Terminal
gcloud pubsub topics create example-topic
Expected OutputExpected
Created topic [projects/your-project-id/topics/example-topic].
Deploy the Cloud Function named 'helloPubSubFunction' using Python 3.10. It triggers on messages published to 'example-topic'. The function entry point is 'hello_pubsub'.
Terminal
gcloud functions deploy helloPubSubFunction --runtime python310 --trigger-topic example-topic --entry-point hello_pubsub --region us-central1 --quiet
Expected OutputExpected
Deploying function (may take a while)... Deploying function (helloPubSubFunction)...done. availableMemoryMb: 256 entryPoint: hello_pubsub name: projects/your-project-id/locations/us-central1/functions/helloPubSubFunction runtime: python310 status: ACTIVE trigger: eventType: google.pubsub.topic.publish resource: projects/your-project-id/topics/example-topic
--runtime - Specifies the language runtime version.
--trigger-topic - Sets the Pub/Sub topic that triggers the function.
--entry-point - Defines the function name in the source code.
Send a message 'Hello, Cloud Functions!' to the 'example-topic' to trigger the Cloud Function.
Terminal
gcloud pubsub topics publish example-topic --message "Hello, Cloud Functions!"
Expected OutputExpected
messageIds: - "1234567890123456"
--message - Specifies the message content to publish.
Check the logs of the Cloud Function to see the output from the triggered function.
Terminal
gcloud functions logs read helloPubSubFunction --region us-central1 --limit 5
Expected OutputExpected
2024-06-01 12:00:00 helloPubSubFunction: Received message: Hello, Cloud Functions! 2024-06-01 11:59:00 helloPubSubFunction: Function execution started 2024-06-01 11:59:01 helloPubSubFunction: Function execution finished
--limit - Limits the number of log entries shown.
Key Concept

If you remember nothing else from this pattern, remember: Cloud Functions run your code automatically whenever a message is sent to a Pub/Sub topic you choose.

Common Mistakes
Deploying the function without specifying the correct trigger topic.
The function will not run because it is not connected to any Pub/Sub topic.
Always use the --trigger-topic flag with the exact topic name when deploying the function.
Publishing messages to a different topic than the one the function listens to.
The function will not be triggered because it listens only to its configured topic.
Publish messages only to the topic that triggers your function.
Not decoding the base64 message data inside the function.
The function will print unreadable data instead of the actual message content.
Decode the message data from base64 before using it.
Summary
Create a Pub/Sub topic to send messages.
Deploy a Cloud Function triggered by that topic with your code.
Publish messages to the topic to automatically run your function.
Check function logs to see the output and confirm it ran.