Stay organized with collections Save and categorize content based on your preferences.
In this tutorial, you learn how to get started with Terraform by using Terraform to create a basic web server on Compute Engine.
In this tutorial, you do the following:
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator.
New Google Cloud users might be eligible for a
free trial.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you beginPrepare to start the tutorial.
Select or create a projectIn the Google Cloud console, go to the project selector page.
Select or create 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.Make sure that you have the necessary Compute Engine permissions on your user account:
compute.instances.*
compute.firewalls.*
Learn more about roles and permissions.
Enable the APIEnable the Compute Engine API.
Cloud Shell is a Compute Engine virtual machine.
The service credentials associated with this virtual machine are automatic, so there is no need to set up or download a service account key.
Terraform is integrated with Cloud Shell, and Cloud Shell automatically authenticates Terraform, letting you get started with less setup.
Create the Compute Engine VMFirst, you define the VM's settings in a Terraform configuration file. Then, you run Terraform commands to create the VM in your project.
Create the directoryCreate a new directory. In your new directory, create a main.tf
file for the Terraform configuration. The contents of this file describe all of the Google Cloud resources to be created in the project.
In Cloud Shell:
mkdir tf-tutorial && cd tf-tutorial
nano main.tfCreate the Virtual Private Cloud network and subnet
In this section, you create a Virtual Private Cloud (VPC) network and subnet for the VM's network interface.
Add the following Terraform resources to the main.tf
file that you created:
In this section, you create a single Compute Engine instance running Debian. In this tutorial, you use the smallest machine type that's available. Later, you can upgrade to a larger machine type.
Add the following google_compute_instance
Terraform resource to the main.tf
file that you created.
The sample code sets the Google Cloud zone to us-west1-a
. You can change this to a different zone.
At this point, you can run terraform init
to add the necessary plugins and build the .terraform
directory.
terraform init
Output:
Initializing the backend... Initializing provider plugins... ... Terraform has been successfully initialized!Validate the Terraform configuration
Optionally, you can validate the Terraform code that you've built so far. Run terraform plan
, which does the following:
main.tf
is correctterraform plan
Output:
... 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.Apply the configuration
To create the VM, run terraform apply
.
terraform apply
When prompted, enter yes
.
Terraform calls Google Cloud APIs to set up the new VM. Check the VM instances page to see the new VM.
Run a web server on Google CloudYour next steps are getting a web application created, deploying it to the VM, and creating a firewall rule to allow client requests to the web application.
Add a custom SSH firewall ruleThe default-allow-ssh
firewall rule in the default
network lets you use SSH to connect to the VM. If you'd rather use your own custom firewall rule, you can add the following resource at the end of your main.tf
file:
Run terraform apply
to create the firewall rule.
Validate that everything is set up correctly at this point by connecting to the VM with SSH.
Go to the VM Instances page.
Find the VM with the name flask-vm
.
In Connect column, click SSH.
An SSH-in-browser terminal window opens for the running VM.
For more information, see Connecting to VMs.
Build the Flask appYou build a Python Flask app for this tutorial so that you can have a single file describing your web server and test endpoints.
In the SSH-in-browser terminal, create a file called app.py
.
nano app.py
Add the following to the app.py
file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_cloud():
return 'Hello Cloud!'
app.run(host='0.0.0.0')
Run app.py
:
python3 app.py
Flask serves traffic on localhost:5000
by default.
Open a second SSH connection:
flask-vm
and click SSH.In the second SSH connection, run curl
to confirm that the greeting that you configured in app.py
is returned.
curl http://0.0.0.0:5000
The output from this command is Hello Cloud
.
To connect to the web server from your local computer, the VM must have port 5000 open. Google Cloud lets you open ports to traffic by using firewall rules.
Add the following google_compute_firewall
Terraform resource at the end of your main.tf
file.
In Cloud Shell, run terraform apply
to create the firewall rule.
At the end of main.tf
, add a Terraform output variable to output the web server URL:
// A variable for extracting the external IP address of the VM output "Web-server-URL" { value = join("",["http://",google_compute_instance.default.network_interface.0.access_config.0.nat_ip,":5000"]) }
Run terraform apply
.
terraform apply
When prompted, enter yes
. Terraform prints the VM's external IP address and port 5000 to the screen, as follows:
Web-server-URL = "http://IP_ADDRESS:5000"
At any time, you can run terraform output
to return this output:
terraform output
Click the URL from the previous step, and see the "Hello Cloud!" message.
This means that your server is running.
If a required API isn't enabled, Terraform returns an error. The error message includes a link to enable the API. After enabling the API, you can rerun terraform apply
.
If you can't connect to your VM through SSH:
tags = ["ssh"]
argument.After completing the tutorial, you can delete everything that you created so that you don't incur any further costs.
Terraform lets you remove all the resources defined in the configuration file by running the terraform destroy
command:
terraform destroy
Enter yes
to allow Terraform to delete your resources.
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 tutorial guides you through using Terraform to create a virtual machine (VM) on Google Cloud's Compute Engine."],["You'll learn to set up a Virtual Private Cloud (VPC) network and subnet using Terraform configuration files to define the VM's network interface."],["The tutorial demonstrates how to deploy and run a basic Python Flask web server on the provisioned Compute Engine VM, with instructions on connecting via SSH."],["You will add custom firewall rules to allow SSH and web server traffic, specifically opening port 5000 to access the Flask app."],["Upon completion, you'll be able to use the `terraform destroy` command to clean up and remove all created resources to avoid continued billing."]]],[]]
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