Build your first event-driven app in Google Cloud Platform in less than five minutes
Event-driven architectures have made it easier to communicate and interact with applications, offering useful benefits like real-time updates from your stack, improved scalability, and system decoupling. The GCP stack allows you to create event-driven applications quickly. In this blog, we’ll look at the process of building your first event-driven app on GCP.
The app we will build is a simple event-driven application where a new file upload to a Google Cloud Storage bucket triggers a Cloud Function, which then processes the file.
Let’s get Started
Set Up Your Google Cloud Platform Account
First, you need a GCP account. If you don't already have one, you can create it here. Google provides a free tier that is suitable and reliable for this exercise.
Create a New Project
After logging in, create a new project.
- Click on the Project dropdown and then New Project.
- Give your project a name and click Create.
Enable Cloud Functions and Cloud Storage APIs
- Navigate to the API & Services section and select Library.
- Search for Cloud Functions API and Cloud Storage API.
- Enable both APIs for your project.
Create a Google Cloud Storage (GCS) Bucket
We’ll create a bucket where we’ll upload our files:
- Navigate to the Storage section and click on Create Bucket.
- Name your bucket and leave the other options as default.
Write and Deploy Your Cloud Function
Now, we’ll create a Cloud Function that will be triggered each time a file is uploaded to your Google Storage bucket.
- In your GCP Console, navigate to Cloud Functions and click on Create Function.
- Name your function, keep the memory allocated to the default 256 MiB, and set the trigger type to Cloud Storage.
- Choose Create event type and select the bucket you created. The bucket i created is in the picture above.
- In the Code section, select Node.js as the Runtime. The function to execute is
CTOWorkflows
which will be our function’s name. - In your
index.js
file, paste the following code:
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
exports.CTOWorkflows = async (event, context) => {
const file = event;
console.log(`Event Service: ${workflows.Service}`);
console.log(`Event ID: ${context.eventId}`);
console.log(`Event Type: ${context.eventType}`);
console.log(`Bucket: ${file.bucket}`);
console.log(`File: ${file.name}`);
console.log(`Metageneration: ${file.metageneration}`);
console.log(`Created: ${file.timeCreated}`);
console.log(`Updated: ${file.updated}`);
if (context.eventType === 'google.storage.object.finalize') {
const bucket = storage.bucket(file.bucket);
const fileObject = bucket.file(file.name);
const content = await fileObject.download();
console.log(`Content: ${content}`);
}
};
exports.githubEventChecker = (event) => {
const insights = event;
console.log(`GitHub Checker Event: ${insights.github.checker}`);
console.log(`Event Status: ${insights.context.status}`);
};
- Exporting the Function: The code exports a function
CTOWorkflows
that takes two parameters -event
andcontext
. These parameters usually contain information about your event that triggered the function and the context in which it is running. - Defining the File Object:
const file = event;
assigns theevent
object to a constant namedfile
. It references the event object contains information about a file, such as its name, bucket, and more. - Logging Information: The
console.log
statements are used to print various pieces of information about the event and file. The data includes the service related to theworkflow
,event ID
,event type
,bucket name
,file name
,metageneration
,time created
, and the time updated.
- Click Deploy. After deploying your code function, wait for some minutes for it to be deployed. Each time a new file is uploaded to the bucket, your function will be triggered, and the log details will be displayed in your bucket from the uploaded file.
Test Your Event-Driven Application
Back in your Storage section, navigate to your bucket, click Upload files and choose any file from your local machine.
- After uploading your file, navigate back to Cloud Functions and click on the function you created.
- Go to the LOGS section. You should see the logs and output attributes for the file you just uploaded.
Conclusion
That’s it!!! In this tutorial, you have seen how to build your first event-driven application on GCP. From here, you can expand your application by adding more Cloud Functions or integrating other Google Cloud Services into your application.
Happy Coding!!!