Create a Google Cloud Compute Engine Instance Using Terraform
- Create a directory to store our Terraform code:
mkdir -p ~/code
- Create a Terraform configuration file and save the file as main.tf with the following configuration:
provider "google" {
project = "<ADD YOUR PROJECT ID HERE>"
region = "us-central1"
zone = "us-central1-a"
}
resource "google_compute_instance" "my_instance" {
name = "my-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral IP
}
}
}
Note: Replace " ADD YOUR PROJECT ID HERE " with your Google Cloud project ID. You can also customize other parameters like the instance name, machine type, zone, and image.
- Initialize Terraform, navigate to the directory containing your Terraform configuration file (main.tf). Initialize the directory which will download the necessary providers.
cd ~/code
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v5.19.0...
- Installed hashicorp/google v5.19.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!
- Plan the infrastructure; run terraform plan to see the execution plan. Terraform will show you what resources it will create, update, or delete based on your configuration.
terraform plan
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_compute_instance.my_instance will be created
+ resource "google_compute_instance" "my_instance" {
+ can_ip_forward = false
+ cpu_platform = (known after apply)
+ current_status = (known after apply)
+ deletion_protection = false
+ effective_labels = (known after apply)
+ guest_accelerator = (known after apply)
<OUTPUT Truncated>
- Apply the Configuration: If the plan looks good, apply the configuration by running terraform apply. Terraform will create the Compute Engine instance as per your configuration.
terraform apply
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_compute_instance.my_instance will be created
+ resource "google_compute_instance" "my_instance" {
+ can_ip_forward = false
+ cpu_platform = (known after apply)
+ current_status = (known after apply)
+ deletion_protection = false
.
.
.
.
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
google_compute_instance.my_instance: Creating...
google_compute_instance.my_instance: Still creating... [10s elapsed]
google_compute_instance.my_instance: Creation complete after 12s
[id=projects/calm-bliss-375608/zones/us-central1-a/instances/my-instance]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Verify the Instance: After Terraform finishes applying the configuration, go to the Google Cloud Console to verify that the Compute Engine instance was created successfully. That’s it! You’ve successfully created a Google Cloud Compute Engine instance using Terraform. You can further extend your configuration to include additional resources or customize the instance properties as needed.
Cleanup by deleting the resource with a simple command:
terraform destroy
google_compute_instance.my_instance: Refreshing state...
[id=projects/calm-bliss-375608/zones/us-central1-a/instances/my-instance]
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# google_compute_instance.my_instance will be destroyed
- resource "google_compute_instance" "my_instance" {
- can_ip_forward = false -> null
- cpu_platform = "Intel Haswell" -> null
- current_status = "RUNNING" -> null
- deletion_protection = false -> null
.
.
..
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
google_compute_instance.my_instance: Destroying...
[id=projects/calm-bliss-375608/zones/us-central1-a/instances/my-instance]
google_compute_instance.my_instance: Still destroying...
[id=projects/calm-bliss-375608/zones/us-central1-a/instances/my-instance, 10s
elapsed]
google_compute_instance.my_instance: Still destroying...
[id=projects/calm-bliss-375608/zones/us-central1-a/instances/my-instance, 20s
elapsed]
google_compute_instance.my_instance: Destruction complete after 21s