Google Cloud Platform provides awesome tools to help engineers perform automation with ease.
In this article, we will build and deploy a serverless application that sends messages to Slack by leveraging on Google Cloud Function. We will also use Google Cloud Scheduler to periodically run our application at an interval of 3hours.
Our final result.
Google Cloud Functions
Google Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment.
Google Cloud Functions can be written in Node.js, Python, and Go, and are executed in language-specific runtimes.
Cloud Functions can be associated with a specific trigger. The trigger type determines how and when your function executes. Cloud Functions supports the following native trigger mechanisms:
- HTTP Triggers
- Cloud Pub/Sub Triggers
- Cloud Storage Triggers
- Direct Triggers
- Cloud Firestore
- Analytics for Firebase
- Firebase Realtime Database
- Firebase Authentication
Google Stackdriver provides a suite of monitoring tools that help you understand what's going on in your Cloud Functions. Logs for Cloud Functions are viewable in the Stackdriver Logging UI.
Cloud Function can be used for multiple cases such as Serverless application backends, Real-time data processing systems or Artificial Intelligence applications.
Google Cloud Scheduler is a fully managed, scalable, and fault-tolerant cron job scheduler that allows engineers to automate all their scheduled tasks in one place.
Google Cloud Scheduler allows you set up fully managed scheduled units of work to be executed at defined times or regular intervals with support for Unix cron format.
Cloud Scheduler can be associated with a target which can be either of the following:
- HTTP/S endpoints
- Cloud Pub/Sub topics
- App Engine applications
In addition, Stackdriver integrates with Cloud Scheduler providing powerful logging for greater transparency into job execution and performance.
Cloud Scheduler can be used for multiple cases such as sending out a report email on a daily basis, updating some cached data every 10 minutes, or making requests to an endpoint.
Setting up Slack
Sign in to your Slack workspace and Create a new Slack app as follows:
- Choose the app's name and your Slack team. Click Create.
- Click Incoming Webhooks.
- Enable incoming webhooks.
- Click Add New Webhook to Team. An authorization page opens.
- From the drop-down menu, select the channel to which you would like notifications sent. We'll be using the #random channel
- Click Authorize.
- A webhook for your Slack application has been created. Copy the webhook and save it for later use.
Setting up Cloud Function
Visit Cloud Functions and Create Function
- Enter your function's name
- Set Memory Allocated : 256MB
- Set Trigger : HTTP
- Authentication : Check - Allow unauthenticated invocations
- Source code : Select - Inline editor
- Runtime : Node.js 8 (Feel free to change this to suit your choice in future)
- Function to execute : sendToSlack
Paste the following code snippet into the Inline editor and replace the value of url to your Webhook from Slack and Deploy your function.
Once done, you can visit the URL on your Cloud Function page to test.
Setting up Cloud Scheduler
Visit Cloud Scheduler and Create Job
- Enter job name
- Set Frequency : every 3 hours
- Target : Select HTTP
- Set URL : (Use your deployed Cloud Function URL)
- HTTP Method : Select GET
Great! Your should get the following message from the RestReminder Bot on your Slack Channel every 3 hours.
- Google Cloud Function
- Google Cloud Scheduler
- Cloud Functions for Firebase
- Scheduled Cloud Functions for Firebase
- Awesome List for Google Cloud Platform
Thanks for reading through! Let me know if I missed any step, if something didn’t work out quite right for you or if this guide was helpful.