Terraform is one of famous IaaC (Infrastructure as a Code) tools. We can use it to provision, establish, re-organize, until decommission any resources in their supported platform. Of course, including GCP.


Why Terraform?

Cloud providers, for example GCP, have many convenient ways for their users to manage resources. For instance, web UI, gcloud, REST API, etc. Terraform is another of them and is very popular in circles of DevOps for do this job.

As its name, "as a Code", it allows to manage like a source code indeed. Versioning, CI/CD, including batching process are all feasible. Goodbye clicking every time and copy-paste to create a VM or else which is risky to human-errors.

Terraform is developed by HashiCorp. And it is open-source. We can visit this link as the homepage.

Terraform by HashiCorp
Terraform is an open-source infrastructure as code tool that enables you to safely and predictably provision and manage infrastructure in any cloud.

Concepts

Terraform works on the tf scripts. There are some facts we need to remember as fundamental.

  1. Usually the guide say creating main.tf, but there is no filename requirements. Only at least 1 tf file must be required.
  2. There will be a file named terraform.tfstate and terraform.tfstate.backup. They are state files. Do nothing to them or we will lose all history and tracks. In that case we need to import changes from the real resources.
  3. One folder will have only one set of state files. It means ALL tf scripts will be executed and we cannot choose which one not to be executed.

Installation

Follow this link and find the method fits you. I myself prefer brew.

Install | Terraform | HashiCorp Developer
Explore Terraform product documentation, tutorials, and examples.

Basic steps

1. init

First of all, we need a first tf script with a keyword provider like this.

We this time name it main.tf but note that we may name it others. And providers supported by Terraform are listed in this link. This time we want to deploy on GCP so we choose "google".

Make sure that we are in the folder then run.

terraform init

It will initialize the backend and provider that we selected. Output should be like this.

Once it completes, we can see a new folder .terraform and a new file .terraform.lock.hcl. They are automatically generated by the command and we have no need to make changes on them.

2. Add resources

Initialized and now we can manage our resources by editing the main.tf. The keyword is resource.

The syntax would be:

resource "<resource_type>" "<resource_name>" {
  attribute1 = value1
  attribute2 = value2 
}

Resource type must be matched with the Terraform registry. Say we are developing on GCP, we can check on this link.

Resource name is internal. We can name it whatever. However we should apply naming conventions such as having types, projects, or short purposes.

Attributes depend on resource type. Check the registry for details. Also their values.

3. validate

Best practice is to validate before proceeding to next step. Use this command.

terraform validate

It helps a lot to protect any incidents. In case of something wrong, it would notice.

Aha, I forgot location that is a required attribute. So I can fix it right now.

And validate it again.

Okay we go next.

4. Plan

Completed validation then we should plan to review changes.

terraform plan

Terraform will display all changes comparing between the current state and our tf scripts. We can double-check what would be changed here. Also we can save the plan for further use.

5. Apply

The scripts are valid and the changes are satisfied. We can firmly execute this to apply the changes.

terraform apply
terraform apply -auto-approve

With -auto-approve flag, we can force apply changes having no need to type "yes" to confirm. However we can skip the flag to review it once again.

Now the bucket is created as this figure.

6. State

All existing resources can be listed using the command.

terraform state list

There will be all resources we have applied so far.

7. Destroy

Lastly, if we want to cleanup everything we made. Yes, EVERYTHING. We should run this command.

terraform destroy
terraform destroy -auto-approve

ALL existing resources will be removed. Some resources are protected to remove and we need to check the registry in order to add some attributes for that case if needed.


Yes, this is just a basic to Terraform. I will update next articles about it.

Hope you find best way to manage your infrastructure as easy as running Terraform.

See ya.