Using Golang to create a Compute Engine Instance in GCP

André Passos
4 min readJan 27, 2021

Golang provides a package to access to the Compute Engine API. Using this package you can create virtual machines, attach disks, set network properties, add a start-up script and so on. In this example we will create a VM and add a start-up script to it.

First thing to do is create a client that allows to interact with the Compute Engine API. In the following example we called it computeService.

In order to create a compute instance there is a minimal set of information required. Information like the Project ID, the Zone where the machine is going to be created, the Disk Name, the Disk Type, and the Network and Subnetwork you would like to use. For privacy reason, some of this info will not be shown in this example.

Another important information required is the machine configuration. GCP has 3 machine types (General-purpose, Compute-Optimized, and GPU). For this example we are going to create a General-purpose of Series N1 (Powered by Intel Skylake CPU platform or one of its predecessors) and the machine type is n1-standard-4 (4 vCPU, 15 GB memory)

As you can see by this example you can specify a relative link referring the project and zone.

With this information you are ready to create your compute instance. In the following example we decided to call it myComputeInstance.

A compute instance has a Name, Description and Machine Type like explained before. Then it has an attached disk. In this example the disk will auto delete and boot. The Source Image is a debian-9 machine. Another attribute that deserves to mention in the Disk Type. It specifies the disk type to use to create the instance. If not specified, the default is pd-standard. In our case we have chosen an ssd disk (pd-ssd).

The next section in the Network Interfaces. In our case we decided to use our own Network (created in VPC) and subnetwork.

The final section is the metadata section. In this section you can use key/value pairs to set labels on your compute instances. On the top of that you can also specify some properties like for instance the start-up script. Compute Engine lets you create and run your own startup scripts on your virtual machine (VM) instances to perform automated tasks every time your instance boots up. Startup scripts can perform actions such as installing software, performing updates, turning on services, and any other tasks defined in the script. In our example we only execute an apt update.

If you remember we created a client called computeService, now it is the time to use it.

From the computeService client we need to call the Instances.Insert function. This function requires a project ID, a Zone, and a Compute Instance (the one we created before). It returns an operation and an error. Exactly one of *Operation or error will be non-nil. Any non-2xx status code is an error. Response headers are in either *Operation.ServerResponse.Header or (if a response was returned at all) in (*googleapi.Error).Header. From my experience errors in the operations are not that frequent. However is always a good practice to check the operation afterwards.

--

--