This repo will use Terraform to setup a pair of load-balanced VM hosts in Google Compute Engine. A script will install nginx as a post-build step, setting up the infrastruture for a basic load-balanced web hosting application with geographic zone redundancy.
This assumes you have a few things before the build can start:
- Terraform is installed on your local host
- An existing project in Google Cloud that you can work within.
- A service account that Terraform can use to build within the project.
This should have the
cloud functions service
role. - A credentials.json file on the local filesystem that you will use to run the build. This information is output when you create the service account.
- You have the Google Cloud SDK installed to run
gcloud
commands on your local shell
- Gather the custom settings information you need to define variables for your GCP project. Specific values required are:
project_id
: Your GCP project_idcredentials_file
: Path on your local filesystem to your credentials.json filedomain
: The domain name the website will be published at (Basic dns records A will be created for this zone)managed_zone_name
: Name of your DNS zone resource in GCPinstance_base_image
: Base Linux image to create the VM from (i.e. ubuntu, centos)machine_type
: GCP machine type that the VMs will useregion
: Your GCP global regionzones
: List of GCP zones to use for redundancy within the regionservice_account_id
: Name of the service account in GCP that terraform will useservice_account_email
: Email address of the service account in GCP that terraform will usetemplate_name
: A name for the VM instance template you will be creating (this can be arbitrary)
-
Create a text file named
terraform.tfvars
using theterraform.tfvars.example
file as a template, and populate it with your custom variable settings. -
Create a text file named
init.sh
also using theinit.sh.example
as a template. Edit the first three values in the file and change to your custom settings. -
Execute the
init.sh
script. This will authenticate with your service account and enable required APIs needed for the terraform build.Occasionally you may see errors from GCP at this step, and these may clear up by re-running the script. The script is idempotent so running it multiple times will not cause any issues.
-
In the
terraform-gcp-nginx_lb_par
directory, Runterraform init
to create the local terraform infrastucture files and fetch the required provider information. -
Run
terraform plan
to verify there are no potential build errors. -
Run
terraform apply
and enteryes
when prompted. -
When the build is complete, verify you can reach the nginx default page via the either the IP address URL or the domain name URL if your DNS is active.