0
0
GCPcloud~5 mins

Instance templates in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to create many virtual machines with the same settings, instance templates help by saving those settings in one place. This makes it easy to launch new machines quickly without repeating the setup steps.
When you need to launch multiple virtual machines with identical configurations.
When you want to create a managed group of virtual machines that can scale automatically.
When you want to update the machine setup and apply it to new machines easily.
When you want to keep your virtual machine setup consistent across your project.
When you want to save time by not configuring each virtual machine manually.
Config File - instance-template.yaml
instance-template.yaml
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeInstanceTemplate
metadata:
  name: example-instance-template
spec:
  machineType: e2-medium
  disks:
    - autoDelete: true
      boot: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
  networkInterfaces:
    - network: default
      accessConfigs:
        - name: External NAT
          type: ONE_TO_ONE_NAT
  tags:
    items:
      - http-server
      - https-server
  metadata:
    items:
      - key: startup-script
        value: |
          #!/bin/bash
          echo Hello, world! > /var/log/startup-script.log

This file defines an instance template named example-instance-template. It sets the machine type to e2-medium, uses a Debian 11 image for the boot disk, and attaches the default network with an external IP. It also adds tags for HTTP and HTTPS servers and includes a startup script that writes a message to a log file.

Commands
This command creates an instance template named 'example-instance-template' with the specified machine type, image, network tags, and a startup script. It saves the configuration for future virtual machine creation.
Terminal
gcloud compute instance-templates create example-instance-template --machine-type=e2-medium --image-family=debian-11 --image-project=debian-cloud --tags=http-server,https-server --metadata=startup-script='#!/bin/bash\necho Hello, world! > /var/log/startup-script.log'
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/instanceTemplates/example-instance-template].
--machine-type - Sets the type of virtual machine to use.
--image-family - Specifies the OS image family for the boot disk.
--metadata - Adds startup scripts or other metadata to the instance.
This command lists all instance templates in your project so you can verify that your template was created successfully.
Terminal
gcloud compute instance-templates list
Expected OutputExpected
NAME MACHINE_TYPE PREEMPTIBLE CREATION_TIMESTAMP example-instance-template e2-medium False 2024-06-01T12:00:00.000-07:00
This command creates a new virtual machine named 'example-instance-1' using the settings saved in the 'example-instance-template'.
Terminal
gcloud compute instances create example-instance-1 --source-instance-template=example-instance-template
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-instance-1].
--source-instance-template - Specifies which instance template to use for creating the VM.
This command lists all virtual machines in your project so you can check that your new instance is running.
Terminal
gcloud compute instances list
Expected OutputExpected
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance-1 us-central1-a e2-medium False 10.128.0.5 34.68.123.45 RUNNING
Key Concept

If you remember nothing else from this pattern, remember: instance templates save your virtual machine setup so you can create many identical machines quickly and easily.

Common Mistakes
Trying to create a VM without specifying the instance template name correctly.
The VM creation fails because it cannot find the template to copy the settings from.
Use the exact instance template name with the --source-instance-template flag when creating the VM.
Not including a startup script or metadata when needed.
The VM might not perform necessary setup tasks automatically after launch.
Add startup scripts or metadata using the --metadata flag in the instance template creation.
Forgetting to check the list of instance templates after creation.
You might think the template was created when it was not, leading to errors later.
Always run 'gcloud compute instance-templates list' to confirm your template exists.
Summary
Create an instance template to save VM settings with 'gcloud compute instance-templates create'.
List templates with 'gcloud compute instance-templates list' to verify creation.
Create VMs from the template using 'gcloud compute instances create --source-instance-template'.
List running VMs with 'gcloud compute instances list' to check your instances.