google-cloud-platformgoogle-compute-enginegce-instance-group

Creating an instance template in Google Cloud Platform


Instance template is essential for creating managed instance group. In fact, Managed instance group is essential for creating an autoscaling group in GCP.

This question is a part of another question's answer, which is about building an autoscaled and load-balanced backend.

I have written the below answer that contains the steps to create an instance template.


Solution

  • Instance templates are global resources in GCP i.e the same template can be used to create managed instance groups in any regions in the GCP. However if a zone is specified in the template, then that template can be used only in that particular zone.

    Instance templates define the machine type, image, identity tags and other instance properties. This is done to maintain the identical instances in the managed instance group. Later, this instance group can be used for creating an autoscaling group and can also be load-balanced.

    Instance template can be created either in console or with gcloud like this:

    gcloud compute instance-templates \
    create sample-template \
    --image CentOS 6 \
    --tags http \
    --scopes=sql-admin,storage-ro,logging-write \
    --metadata startup-script-url=\
    gs://<bucket-name>/<startup-script>.sh,\
    <other-variable>=<value>
    

    The above command creates an instance template based on CentOS 6 image, with a tag, some scopes and an startup script.

    Best Practices: From my perspective, it is better to create a custom image with all your software installed than to use a startup script. As the time taken to launch new instances in the group should be as minimum as possible. This will increase the speed at which you scale your web app.

    This is part 1 of 3-part series about building an autoscaled, load-balanced backend.