Skip to main content
  1. Posts/

Cookiecutter template for Google Cloud Run

··429 words·3 mins
Author
Hairizuan Noorazman
Software engineering experiments, implementation notes, and lessons learned.

While working on a couple of projects that would be deployed on Google Cloud Run, I realized that a couple of them tend to have some sort of similar structure. Due to the number of repositories I would typically handle on a personal basis as well as the amount of context switch I would need to move between projects; it would ideal that all of such projects are automated as much as possible.

This is a small list of features that would great to be templatized across projects:

  • Cloud Build templates
    • Include the handling of secrets via Google KMS if necessary.
    • Handle cases between http based vs msg based Cloud Run deployments (they have slight differences which can easily trip you while you are rushing out a project)
    • Handle little issues when dealing with Cloud Build; Previously, I found out that Cloud Build would generate a .gcloudignore file from a .gitignore if it doesn’t exist. Let’s say you were to deploy it to Google Cloud Functions and you added *.json to .gitignore. That would mean that *.json would be added to .gcloudignore causing all json file to ignored (even though the files could have been decoded/generated during cloud build steps)
  • Convenience commands
    • Make commands to test locally
    • Make commands to fire pubsub messages
    • Make commands to alter topics/subscriptions
  • Able to templatize conveniently without resorting to using git hacks etc or libs. (Previous methods involved relying on git providing to set one of the projects that you own as a template. Gitlab used to be able to allow to do this but that suddenly became a paid feature - a painful lesson indeed)

From above, it seems that creating a template would be nice. And in order to aid with this, there is a tool called cookiecutter. Here is a link to the project: https://github.com/cookiecutter/cookiecutter

This is a template that can be generated via cookiecutter: https://github.com/hairizuanbinnoorazman/cookiecutter-cloud-run-go

To use the tool, one can run the following after installing cookiecutter on your computer:

cookiecutter https://github.com/hairizuanbinnoorazman/cookiecutter-cloud-run-go

It is a prompt based cli tool; it would provide options that you can alter accordingly. This is an example of what it would like when you run it now:

cookiecutter https://github.com/hairizuanbinnoorazman/cookiecutter-cloud-run-go
You've downloaded /Users/hairizuannoorazman/.cookiecutters/cookiecutter-cloud-run-go before. Is it okay to delete and re-download it? [yes]: no
Do you want to re-use the existing version? [yes]: yes
golang_mod_name [github.com/sample/sample]:
mod_name [sample]:
app_name [sample]:
Select type:
1 - http
2 - msg
Choose from 1, 2 [1]:

There might be more options in the future as more features would be added to this template to support more complex cases.

Related

SSH configurations for going into Google Cloud Instances

··386 words·2 mins
A classic move to reduce the attack surface of Google Cloud Instances is follow the advice below: If service on instance don’t need Public IPs, don’t attach Public IPs to such instances If instance requires Public IPs, ensure that only specific ports that are required are exposed. Clamp down on the rest of the ports and ensure no ingress on them With these basic principles, it would be simple to think how these would eventually lead to an architecture where users access the instances via a bastion host. A bastion host is a instance that would allow user to ssh in from the “outside” world. The more critical instances would linked together in a private network that is unaccessible from the outside (except for load balancers to receive traffic etc).

Introduction to Google Cloud Run

··731 words·4 mins
There are various serverless compute solutions on the Google Cloud Platfrom; initially it used to be only Appengine and Google Cloud Function. Google Appengine is a solution that allow you to focus on writing up apps and allow Google to take of deployment/scaling/operations. Google Cloud Functions take a step further and allow you as a developer to develop just plain old functions and allow Google to handle the rest of it, thereby making it easier to split your app functionality to parts that require to scale and parts that don’t need to.

Private Go Modules in Google Cloud Build

··1037 words·5 mins
So recently, I’ve been needing to automate my builds for my few Golang projects via Google Cloud Build. However, rather than building docker containers, I needed Golang binaries instead, which kind of meant that I would need to have the CI/CD pipeline have a Go environment/runtime to build them. However, when it comes to these CI/CD solutions, including private Golang packages/modules in siad projects is usually quite troublesome. Private Golang packages usually take the code from private Github/Bitbucket/Gitlab repos and getting the go get command to fetch them successful require a bit of hacks here and there to make it work successfully.