Get information about an instance template
Stay organized with collections
Save and categorize content based on your preferences.
Get the basic information and instance configuration defined in an instance template.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in Go, Java, Node.js, and Python to retrieve an instance template.\u003c/p\u003e\n"],["\u003cp\u003eInstance templates are used to define configurations for creating virtual machine (VM) instances and managed instance groups (MIGs).\u003c/p\u003e\n"],["\u003cp\u003eEach code sample includes instructions on setting up authentication using Application Default Credentials and provides a reference to the relevant Compute Engine API documentation.\u003c/p\u003e\n"],["\u003cp\u003eBefore running the provided code samples, users are instructed to follow the language-specific Compute Engine quickstart guides for client library setup.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples can be tested and adjusted by replacing placeholder variables such as project ID and template name.\u003c/p\u003e\n"]]],[],null,["# Get information about an instance template\n\nGet the basic information and instance configuration defined in an instance template.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Get, List, and Delete Instance Templates](/compute/docs/instance-templates/get-list-delete-instance-templates)\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/compute/latest/apiv1).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n\n \tcompute \"cloud.google.com/go/compute/apiv1\"\n \tcomputepb \"cloud.google.com/go/compute/apiv1/computepb\"\n )\n\n // getInstanceTemplate retrieves an instance template, which you can use to create virtual machine\n // (VM) instances and managed instance groups (MIGs).\n func getInstanceTemplate(projectID, templateName string) (*computepb.InstanceTemplate, error) {\n \t// projectID := \"your_project_id\"\n \t// templateName := \"your_template_name\"\n\n \tctx := context.Background()\n \tinstanceTemplatesClient, err := compute.https://cloud.google.com/go/docs/reference/cloud.google.com/go/compute/latest/apiv1.html#cloud_google_com_go_compute_apiv1_InstanceTemplatesClient_NewInstanceTemplatesRESTClient(ctx)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"NewInstanceTemplatesRESTClient: %w\", err)\n \t}\n \tdefer instanceTemplatesClient.Close()\n\n \treq := &computepb.GetInstanceTemplateRequest{\n \t\tProject: projectID,\n \t\tInstanceTemplate: templateName,\n \t}\n\n \treturn instanceTemplatesClient.Get(ctx, req)\n }\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Java API\nreference documentation](/java/docs/reference/google-cloud-compute/latest/overview).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetInstanceTemplateRequest.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplate.html;\n import com.google.cloud.compute.v1.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html;\n import java.io.IOException;\n\n public class GetInstanceTemplate {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // projectId: project ID or project number of the Cloud project you use.\n // templateName: name of the template to retrieve.\n String projectId = \"your-project-id\";\n String templateName = \"template-name\";\n getInstanceTemplate(projectId, templateName);\n }\n\n // Retrieve an instance template, which you can use to create virtual machine\n // (VM) instances and managed instance groups (MIGs).\n public static void getInstanceTemplate(String projectId, String templateName) throws IOException {\n try (https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html instanceTemplatesClient = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplatesClient.html.create()) {\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetInstanceTemplateRequest.html getInstanceTemplateRequest = https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.GetInstanceTemplateRequest.html\n .newBuilder()\n .setProject(projectId)\n .setInstanceTemplate(templateName).build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplate.html instanceTemplate = instanceTemplatesClient.get(getInstanceTemplateRequest);\n System.out.println(\"Instance Template retrieved: \" + instanceTemplate.https://cloud.google.com/java/docs/reference/google-cloud-compute/latest/com.google.cloud.compute.v1.InstanceTemplate.html#com_google_cloud_compute_v1_InstanceTemplate_getName__());\n }\n }\n }\n\n### Node.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Node.js API\nreference documentation](/nodejs/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment and replace these variables before running the sample.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n // const templateName = 'your_template_name';\n\n const compute = require('https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html');\n\n // Retrieve an instance template, which you can use to create\n // virtual machine (VM) instances and managed instance groups (MIGs).\n async function getInstanceTemplate() {\n const instanceTemplatesClient = new compute.https://cloud.google.com/nodejs/docs/reference/compute/latest/overview.html();\n\n const [instance] = await instanceTemplatesClient.get({\n project: projectId,\n instanceTemplate: templateName,\n });\n\n console.log('Instance template:', instance);\n }\n\n getInstanceTemplate();\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import compute_v1\n\n\n def get_instance_template(\n project_id: str, template_name: str\n ) -\u003e compute_v1.InstanceTemplate:\n \"\"\"\n Retrieve an instance template, which you can use to create virtual machine\n (VM) instances and managed instance groups (MIGs).\n\n Args:\n project_id: project ID or project number of the Cloud project you use.\n template_name: name of the template to retrieve.\n\n Returns:\n InstanceTemplate object that represents the retrieved template.\n \"\"\"\n template_client = compute_v1.InstanceTemplatesClient()\n return template_client.get(project=project_id, instance_template=template_name)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]