A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://cloud.google.com/docs/terraform/create-vm-instance below:

Quickstart: Create a Compute Engine VM instance using Terraform

Skip to main content

Stay organized with collections Save and categorize content based on your preferences.

Quickstart: Create a VM instance using Terraform

In this quickstart, you learn how to use Terraform to create a Compute Engine Virtual Machine (VM) instance and connect to that VM instance.

Hashicorp Terraform is an Infrastructure as code (IaC) tool that lets you provision and manage cloud infrastructure. Terraform provider for Google Cloud (Google Cloud provider) lets you provision and manage Google Cloud infrastructure.

Before you begin
  1. To use an online terminal with the gcloud CLI and Terraform already set up, activate Cloud Shell:

    At the bottom of this page, a Cloud Shell session starts and displays a command-line prompt. It can take a few seconds for the session to initialize.

  2. Create or select a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.
  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API:

    gcloud services enable compute.googleapis.com
  5. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/compute.instanceAdmin.v1

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

Prepare the environment
  1. Clone the GitHub repository containing Terraform samples:

    git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
    
  2. Go to the directory that contains the quickstart sample:

    cd terraform-docs-samples/compute/quickstart/create_vm
    
Review the Terraform files

Review the main.tf file. This file defines the Google Cloud resources that you want to create.

cat main.tf

The output is similar to the following

This file describes the google_compute_instance resource, which is the Terraform resource for the Compute Engine VM instance. google_compute_instance is configured to have the following properties:

Create the Compute Engine VM instance
  1. In Cloud Shell, run the following command to verify that Terraform is available:

    terraform
    

    The output should be similar to the following:

    
    Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    
    
  2. Initialize Terraform by running the following command. This command prepares your workspace so Terraform can apply your configuration.

    terraform init
    

    The output should be similar to the following:

    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of hashicorp/google...
    - Installing hashicorp/google v5.35.0...
    - Installed hashicorp/google v5.35.0 (signed by HashiCorp)
    
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    Terraform has been successfully initialized!
    
    
  3. Validate the Terraform configuration by running the following command. This command takes the following actions:

    terraform plan
    

    The output should be similar to the following:

    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. Apply the configuration to provision resources described in the main.tf file:

    terraform apply
    

    When prompted, enter yes.

    Terraform calls Google Cloud APIs to create the VM instance defined in the main.tf file.

    The output should be similar to the following:

    Apply complete! Resources: 1 added, 0 changed, 0 destroyed
    
Connect to the VM instance

Connect to the VM instance you just created by running the following command:

gcloud compute ssh --zone=us-central1-a my-vm
Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, delete the Google Cloud project with the resources.

In Cloud Shell, run the following command to delete the Terraform resources:

terraform destroy

When prompted, enter yes.

The output should be similar to the following:

Destroy complete! Resources: 1 destroyed.
What's next

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.

Last updated 2025-08-07 UTC.

[[["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"]],["Last updated 2025-08-07 UTC."],[[["This quickstart guide demonstrates how to use Hashicorp Terraform, an Infrastructure as Code (IaC) tool, to create and manage a Compute Engine Virtual Machine (VM) instance on Google Cloud."],["The process involves setting up a Google Cloud project, enabling the Compute Engine API, granting necessary IAM roles, and preparing a local environment with Terraform installed."],["A `main.tf` file is used to define the desired VM instance properties, including name, machine type, zone, boot disk, and network interface."],["Terraform commands, including `terraform init`, `terraform plan`, and `terraform apply`, are utilized to initialize the environment, preview the changes, and provision the VM instance."],["Finally, the guide shows how to connect to the created VM using `gcloud compute ssh` and how to clean up resources using `terraform destroy`."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4