Using Google Cloud can be very effortless. You can easily create, deploy, or delete any resource(s). These resources can include a bucket, virtual machine, amongst others.
In recent times, it has become insanely difficult to create multiple resources simultaneously. For example if you were creating 50 Google Cloud storage buckets and 50 virtual machines on Google Cloud at the same time. Aha! you can imagine how difficult and exhausted you’d be.
In this article, I’ll introduce you to the basics of Terraform and how you can safely and predictably create, change, and improve your infrastructure.
Terraform is a cloud-agnostic Infrastructure as code tool (IAC) for building, managing and destroying an infrastructure. This tool can deploy 50 Google cloud Storage buckets and 50 Virtual machines simultaneously.
One of the many things I love about terraform is that you can deploy it to Google Cloud, AWS, Microsoft Azure, and many more cloud providers. It also enhances collaboration among engineers using State Management.
There are three most important commands I use when working with terraform daily. I will explain each of them:
Terraform init
This is the first command that initializes a working directory. It runs when a new configuration is detected. You can run this command as many times as you want.
Terraform plan
This is the second command that creates an overview of what’s going to be executed. It also gives a human-readable output from a state.
Terraform apply
This is the third and last command that executes the plan. For example, this command can provision, modify or/and delete a particular infrastructure.
A basic terraform folder structure looks like the output below:
├── main.tf
├── outputs.tf
├── providers.tf
└── variables.tf
Let’s take a deep dive into each of the following files in the tree structure above.
This contains some set of configurations and/or instructions used to execute the resources on the script.
For example, The code snippet below will deploy a virtual machine on AWS.
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
This contains another set of configurations that will output properties of the resource created from the state.
For example, the code snippet below prints out the public IP address
when the virtual machine is deployed.
output "public_ip" {
value = aws_instance.example.public_ip
description = "The public IP of the webserver"
}
This tells terraform the Cloud provider you wish to use. The provider can be Google Cloud, AWS, Digital Ocean, or others.
For example, the code snippets below tell terraform that you want to use AWS as your cloud provider and select the region you want to deploy to.
provider "aws" {
region = "eu-west-1"
}
It will help if you think of this file as one used to set environmental variables. This allows your code to be more configurable and DRY.
For example, the code snippet below allows you to set env variables for the AWS region you want to deploy to.
variable "aws-region" {
default = "eu-west-1"
type = string
description = "The AWS Region to deploy"
}
For more understanding, let’s break down the attributes of the variables. tf
file.
This is where you provide the value of the variable.
This allows you to set type constraints on it like string, bool, list, maps, list(maps(strings)), and many more.
This is more like sentences used to document the use of variables.
Now that we’ve covered the basics let’s explore more technical features you can achieve with terraform.
It’s like a container or multiple collections, and resources off terraform files. They are small reusable configurations for grouping resources.
Terraform has a ton of functions that can be used for transforming data. A few popular functions are:
This is used to return the highest number in a set.
max([1, 5, 2]...)
Output:
5
This is used to remove any character in a string.
trim("?!obinna?!", "!?")
Output:
obinna
This takes two or more different list and joins them as one.
concat(["james", ""], ["hey", "obinna"])
Output:
[
"james",
"",
"hey",
"obinna",
]
This adds to or more number together
> sum([2, 2, 2, 2])
Output:
8
This article only covers the tip of the iceberg in terraforming. There are several other resources out there that will guide you on deploying your first resource via terraform.
You should check out these:
Thanks for reading!
— Feb 25, 2021
Made with ❤ and on Netlify.