Terraform enables us to safely and predictably create, change, and improve infrastructure. It is an open-source tool that codifies APIs into declarative configuration files that can be shared among team members, treated as code, edited, reviewed, and versioned.
In this lab, we will create a Terraform configuration with a module to automate the deployment of Google Cloud infrastructure. Specifically, we will deploy one auto mode network with a firewall rule and two VM instances, as shown in this diagram:
Set up Terraform and Cloud Shell. Create mynetwork and its resources
Configure your Cloud Shell environment to use Terraform. Install Terraform.
Terraform is now integrated into Cloud Shell. Verify which version is installed.
1. In the Cloud Console, click Activate Cloud Shell.
2. If prompted, click Continue.
3. To confirm that Terraform is installed, run the following command:
terraform — version
Don’t worry if you get a warning that the version of Terraform is out of date.
Terraform is distributed as a binary package for all supported platforms and architectures, and Cloud Shell uses Linux 64-bit.
4. To create a directory for your Terraform configuration, run the following command:
5. Now change to this directory called “tfinfra” and create 2 files named “provider.tf” and “mynetwork.tf” like below:
6. Now create a new directory inside tfinfra called “instance” and change to this directory. create a file named “main.tf” as below:
7. You should have the following folder structure in Cloud Shell:
8. Now Go back to the “tfinfra” directory and Initialize the terraform with below command:
9. To create an execution plan, run the following command:
10. To apply the desired changes, run the following command. To confirm the planned actions when prompted, type “yes”
Congratulations !! Your deployment is completed.
Verify your deployment
Verify your network in the Cloud Console
- In the Cloud Console, on the Navigation menu , click VPC network > VPC networks.
2. View the mynetwork VPC network with a subnetwork in every region.
3. On the Navigation menu, click VPC network > Firewall.
4. View the mynetwork-allow-http-ssh-rdp-icmp firewall rule for mynetwork.
Verify your VM instances in the Cloud Console
- On the Navigation menu, click Compute Engine > VM instances.
2. View the mynet-us-vm and mynet-eu-vm instances.
3. Click SSH to launch a terminal and connect, you should be good
That Concludes our tutorial for Automating the Deployment of Infrastructure Using Terraform.