This folder contains a web server example of a Terraform file on Google Cloud Platform (GCP).
This Terraform file deploys a single web server on Google Cloud Platform (GCP). The web server returns "Hello, World" for the URL /
listening on port 8080.
- You must have Terraform installed on your computer.
- You must have a Google Cloud Platform (GCP) account.
- You must have downloaded a Google Cloud Platform credentials file.
- You must have enabled the Google Compute Engine API.
- It uses the Terraform Google Cloud Provider that interacts with the many resources supported by Google Cloud Platform (GCP) through its APIs.
- This code was written for Terraform 0.10.x.
-
Configure your Google Cloud access keys.
Two ways in order to configure credentials:
-
Configure
GOOGLE_APPLICATION_CREDENTIALS
environment variable. The variable must contain the path to the credentials file.To set these variable on Linux, macOS, or Unix, use
export
:export GOOGLE_APPLICATION_CREDENTIALS="~/.gcloud/terraform-examples-code.json"
To set these variable on Windows, use
set
:set GOOGLE_APPLICATION_CREDENTIALS="C:\Users\USERNAME\.gcloud\terraform-examples-code.json"
-
Configure
GOOGLE_CREDENTIALS
environment variable. The variable must contain the content of the credentials file and not the path to it.To set these variable on Linux, macOS, or Unix, use
export
:export GOOGLE_CREDENTIALS="$(cat ~/.gcloud/terraform-examples-code.json)"
-
-
Initialize working directory.
The first command that should be run after writing a new Terraform configuration is the
terraform init
command in order to initialize a working directory containing Terraform configuration files. It is safe to run this command multiple times.terraform init
-
Validate the changes.
Run command:
terraform plan
-
Deploy the changes.
Run command:
terraform apply
-
Test the web server.
When the
terraform apply
command completes, it will output the public IP address of the web server.You can test it in two ways:
-
Running this command:
curl http://<server_public_ip>:8080/
-
Writing in your browser this URL:
http://<server_public_ip>:8080/
You should get a
Hello, World
response message. -
-
Clean up the resources created.
When you have finished, run command:
terraform destroy