An instance template in Compute Engine is a resource that defines the configuration of VM instances. It is used to create managed instance groups, which enable you to create and manage a group of identical VM instances for load balancing and autoscaling. Instance templates include settings such as machine type, boot disk image, network settings, and metadata.
Here’s how to create an instance template using GCP Console, gcloud CLI, and Terraform:
1. Using GCP Console:
a. Go to the GCP Console: https://console.cloud.google.com/
b. Navigate to Compute Engine > Instance templates.
c. Click on the “Create instance template” button.
d. Fill in the required fields and customize the settings for your instance template.
e. Click the “Create” button to create the instance template.
2. Using gcloud CLI:
a. Run the following command to create an instance template:
gcloud compute instance-templates create TEMPLATE_NAME \
--machine-type MACHINE_TYPE \
--image-family IMAGE_FAMILY \
--image-project IMAGE_PROJECT \
--boot-disk-size BOOT_DISK_SIZE \
--network NETWORK \
--subnet SUBNET
Replace TEMPLATE_NAME
, MACHINE_TYPE
, IMAGE_FAMILY
, IMAGE_PROJECT
, BOOT_DISK_SIZE
, NETWORK
, and SUBNET
with appropriate values.
3. Using Terraform:
a. Create a main.tf
file with the following resource block:
resource "google_compute_instance_template" "example" {
name_prefix = "example-instance-template-"
machine_type = "MACHINE_TYPE"
disk {
boot = true
source_image = "projects/IMAGE_PROJECT/global/images/family/IMAGE_FAMILY"
disk_size_gb = BOOT_DISK_SIZE
}
network_interface {
network = "NETWORK"
subnetwork = "SUBNET"
}
}
Replace MACHINE_TYPE
, IMAGE_FAMILY
, IMAGE_PROJECT
, BOOT_DISK_SIZE
, NETWORK
, and SUBNET
with appropriate values.
b. Run the following commands to apply the changes:
terraform init
terraform plan
terraform apply
This will create a new instance template with the specified configuration. Once you have an instance template, you can use it to create managed instance groups for autoscaling and load balancing.
Leave a Reply