The purpose of this series of posts on Terraform with GCP is to accomplish more with less. Here we try to optimize our templates for bringing up multiple environments across multiple projects in GCP. Below approach will help spin multiple instances with minimal efforts by introducing .tfvars files into our templates.
Use case: I have 2 projects gcp-homecompany-qa and gcp-homecompany-dev for this purpose and we will have to create compute instances with terraform on GCP. Lets get on with it.
The values to these variables are assigned in the respective .tfvars files, so here we create 2 .tfvars files to lets say spin up 2 environments Dev and QA environments. And the two files are defined as below:
In the above .tfvars files we tried to populate the list test_servers with 3 google compute instances. In order to iterate through this list with key-value pairs we try to implement for loop with a for_each meta-arguments in the below template. Hence following changes are to be done to our main.tf file:
The for_each meta argument will assign the values to the arguments from the list with key-value pair. While we can now test the above template. For generalizing the network, subnet and load balancer related stuffs, I will post in the future articles.
terraform apply -var-file=app-<env>.tfvars
And the above command create compute instances depending on the .tfvars files passed while applying .
Continuing from the previous post we will try to introduce interpolation, flow control and looping. We will split the main.tf to different chunks of files that hold specific definitions to create the resources in GCP. We will create the provider.tf file which holds the provider configurations.
resource "google_compute_firewall" "allow-http-port" { name = "allow-http-port" network = "default"
allow { protocol = "tcp" ports = ["80"] }
target_tags = ["allow-http"] }
resource "google_compute_firewall" "allow-https-port" { name = "allow-https-port" network = "default"
allow { protocol = "tcp" ports = ["443"] }
target_tags = ["allow-https"] }
Interpolation in Terraform helps to assign values to variables, this way we can dynamically manage the provisioning of resources in the cloud environments. Here we create variables.tf file with defines the variables that can be used in the script.
Further the above lines also shows the looping and flow control. Here we are looping to create 3 compute instances of type production grade. Below we see clear interpolation the terraform which refers the image and machine_size defined in the variables.tf
This and the next series of posts will demonstrate the simplification of introducing complexity in IaC best practices. But first a simple Terraform script to provision resources on a GCP cloud. We dive into getting a VM instance with Apache web server with in Google Cloud Platform public in public cloud. We start with one main.tf which has all the configurations and the resources to provision and orchestrate in GCP.
Lets first define the provider configurations:
1 2 3 4 5 6 7 8 9
provider "google" {
project = "triple-virtue-271517" version = "~> 3.38.0" region = "us-central1" zone = "us-central1-a" credentials = "${file("${var.path}/cloud-access.json")}"
}
The path variable refers to the access tokens to GCP cloud project as below:
The metadata_startup_script also tries to install webserver while provisioning the vm instance. The network_interface section assigns a public ip to the same instance.