Terraform Basics – AWS / GCP / Aliyun

What is Terraform?

It’s a tool to create, manage infrastructure as a code. Infrastructure includes not only servers but also network resources –e.g. DNS, loadbalancer. The benefit you can get is as follows:

  • Versioning of your changes
  • Management of all services as a whole (orchestration)
  • Single management of multi-cloud platform
  • and so on …

Let’s Try

I make two compute instances and make modifications, and finally delete all resources to demonstrate how to use Terraform.

  • on AWS (amazon web service), GCP (Google cloud platform) and Aliyun (Alibaba cloud)
  1. Install Terraform
  2. Get credentials
  3. Create servers
  4. Modify servers
  5. Delete all procured resources

Install Terraform


Terraform is distributed as a binary. Just download it and move it to wherever you like.

Download Terraform

Get credentials


To use cloud provider, you need to create a user credential in each cloud provider. You need to grant each users a sufficient privilege to carry out your tasks –e.g. create a compute instance.

Create servers


To demonstrate the procurement process, I create following resources in each cloud provider.

  • VPC … whole network range for this project
  • Subnets … sub network in VPC where services are installed
  • Compute instances … servers

In Terraform, configurations are defined in the text file with extension “.tf“.

First I create <cloud-provider>_sandbox.tf file for each provider.

AWS

aws_sandbox.tf

GCP

gcp_sandbox.tf

Aliyun

aliyun_sandbox.tf

You can check if the syntax are correct.

AWS

GCP

Aliyun

Let’s apply and create servers.

AWS

GCP

Aliyun

To confirm these servers are correctly procured, log in to the servers using local private key.

AWS

GCP

Aliyun

Modify servers


Procured resources can be modified by modifying the tf file, and apply it again.

Add tags on the procured servers.

AWS

aws_sandbox.tf

GCP

gcp_sandbox.tf

Aliyun

aliyun_sandbox.tf

Delete one server.

AWS

aws_sandbox.tf

GCP

gcp_sandbox.tf

Aliyun

aliyun_sandbox.tf

Delete all resources


Once you played with cloud resources, it is important to remove all resources you have procured. Otherwise it keeps charging you bills.

AWS

GCP

Aliyun


Terraform is especially useful for me whenever I need to create a temporary lab in the cloud. Previously I did all the provisioning and house keeping manually(and some scripting), and it is not only a couple of times that I got unexpected charge because I forgot to delete resources.

Since Terraform is specialized in infrastructure orchestration, the other tools are also required to have a life cycle management of services –e.g. CI/CD, which I will introduce them in separate post.