Jump to Content
Serverless

Extending Cloud Code with custom templates

March 17, 2023
Mete Atamel

Cloud Developer Advocate

Cloud Code is a set of IDE plugins for popular IDEs that make it easier to create, deploy and integrate applications with Google Cloud. Cloud Code provides an excellent extension mechanism through custom templates. In this post, I show you how you can create and use your own custom templates to add some features beyond those supported natively in Cloud Code, such as .NET functions, event triggered functions and more. 

As a recap, in my Introducing Cloud Functions support in Cloud Code post, I pointed out some limitations of the current Cloud Functions support in Cloud Code:

  • Only four languages are supported (Node.js, Python, Go, and Java) in Cloud Functions templates. I’ve especially missed the NET support.

  • Templates for Cloud Run and Cloud Functions are only for HTTP triggered services. No templates for event triggered services.

  • Testing only works against deployed HTTP triggered services. No testing support for locally running services or event triggered services.

Let’s see how we can add these features with custom templates!

Custom sample repositories

In Cloud Code, when you create a new application with Cloud Code → New Application, it asks you to choose the type of the application you want to create:

https://storage.googleapis.com/gweb-cloudblog-publish/images/image1_A0UTFc7.max-800x800.jpg
Create new application dialog

For Kubernetes, Cloud Run, and Cloud Functions applications, it uses the templates defined in the cloud-code-samples repo to give you starter projects in one of the supported languages for those application types. 

It gets more interesting when you choose the Custom application option. There, you can point to a GitHub repository with your own templates and Cloud Code will use those templates as starter projects. This is how you can extend Cloud Code – pretty cool! 

On the Manage custom sample repositories in Cloud Code for VS Code page, there’s a detailed description on how a custom templates repository should look. There’s also a cloud-code-custom-samples-example GitHub repo and a nice video explaining custom sample templates:

Video Thumbnail

Basically, it boils down to creating a public GitHub repository with your samples and having a .cctemplate file to catalog each template. That’s it! 

Our custom sample repository

We initially wanted to add support for only HTTP triggered .NET Cloud Functions, as this is currently missing from Cloud Code. However, we enjoyed creating these templates so much that we ended up completing a longer wish list:

  1. Added templates for HTTP triggered Cloud Functions and Cloud Run services in multiple languages (.NET, Java, Node.js, Python). 

  2. Added templates for CloudEvents triggered (Pub/Sub, Cloud Storage, AuditLogs) Cloud Functions and Cloud Run services in multiple languages (.NET, Java, Node.js, Python).

  3. Added lightweight gcloud based scripts to do local testing, deployment and cloud testing for each template. 

You can check out my cloud-code-custom-templates repository for the list of templates. 

To use these templates as starter projects:

  1. Click on Cloud Code in VS Code

  2. Select New Application → Custom Application → Import Sample from Repo

  3. Point to my cloud-code-custom-templates repository

Choose a template as a starter project and follow the README.md instructions of the template.

https://storage.googleapis.com/gweb-cloudblog-publish/original_images/install.gif
Import samples from a GitHub repository

Let’s take a look at some of these templates in more detail. 

HTTP triggered Cloud Functions templates

As an example, there’s a .NET: Cloud Functions - hello-http template. It’s an HTTP triggered .NET 6 Cloud Functions template. When you first install the template, the sample code is installed and a README.md guides you through how to use the template:

https://storage.googleapis.com/gweb-cloudblog-publish/images/image1b.max-2200x2200.jpg
HTTP triggered .NET Cloud Functions template

The code itself is a simple HelloWorld app that responds to HTTP GET requests. It’s not that interesting, but the template also comes with a scripts folder, which is more interesting. 

In that scripts folder, there’s a test_local.sh file to test the function running locally. This is possible because Cloud Functions code uses Functions Framework, which enables Cloud Functions to run locally. Testing that function is just a matter of sending an HTTP request with the right format. In this case, it’s simply an HTTP GET request but it gets more complicated with event triggered functions. More on that later.

There’s also setup.sh to enable the right APIs before deploying the function, deploy.sh to deploy the function, and test_cloud.sh to test the deployed function using gcloud. I had to add these scripts as there’s no support in Cloud Code right now to deploy and test a function for .NET. As you see, however, it’s very easy to do with scripts installed as part of the template. 

Event triggered Cloud Functions templates

As you might know, Cloud Functions also support various event triggered functions. These events are powered by Eventarc in Cloud Functions gen2. In Cloud Code, there are no templates right now to help with the code and setup of event triggered functions. 

In Eventarc, events come directly from sources (e.g. Cloud Storage, Pub/Sub, etc.) or they come via AuditLogs. We have some templates in various languages to showcase different event sources such as:

The event envelope is in CloudEvents format and the payload (the data field) contains the actual event. In .NET, the templates are based on the templates from the Google.Cloud.Functions.Templates package (which you can install and use with the dotnet command line tool to generate Cloud Function samples) and they handle the parsing of CloudEvents envelopes and payloads into strong types using the Functions Framework for various languages.

As before, each template includes scripts to test locally, deploy to the cloud, and test in the cloud. As an example, test_local.sh for the Cloud Storage template creates and sends the right CloudEvent for a Cloud Storage event:

Loading...

This is very useful for local testing. 

Cloud Run templates

We have similar templates for Cloud Run as well. Some examples are:

Since these are Cloud Run services, they can’t use the Functions Framework. That means it’s up to you to parse the CloudEvents format using the CloudEvents SDK and the payload (the actual event) using the Google CloudEvents library. The templates take care of all these details and include the right SDKs and libraries for you out of the box.

Using Cloud Code and templates from Cloud Shell

At this point, you might be wondering: This is all great but I don’t have .NET or Node.js installed locally, how do I try all these templates? 

I was pleased to learn that Cloud Code is available in Cloud Shell Editor. You can use Cloud Code and import these custom templates from your browser:

https://storage.googleapis.com/gweb-cloudblog-publish/images/image2_BdjSwyE.max-1700x1700.jpg
Using Cloud Code and templates from Cloud Shell

Moreover, since Cloud Shell already comes with .NET or Node.js pre-installed, you can build, run, test, and deploy all the samples using the scripts in the templates, right in your browser. This is pretty neat!

https://storage.googleapis.com/gweb-cloudblog-publish/images/image3_k6eee7Y.max-2200x2200.jpg
Running .NET 6 templates in Cloud Shell

What’s next?

I’m impressed at how easy it is to extend Cloud Code with custom templates. It’s also pretty cool that you can use Cloud Code and any custom templates you create right in your browser without having to install anything, thanks to Cloud Shell. 

If you’re interested in helping out with templates for other languages (I’d love Go support!), feel free to reach out to me on Twitter @meteatamel or simply send me a pull request in my cloud-code-custom-templates repo and I’ll be happy to collaborate. Thanks to Marc Cohen for contributing with Python templates and GitHub actions to share scripts between templates. 

To learn more about Cloud Functions support in Cloud Code, try our new Create and deploy a function with Cloud Code tutorial.

Posted in