GoRapid Blog

Stories from people who build @GoRapid

Terraform tutorial

19 August: Rohit Bansal

Today, I'm going to explain how to install terraform and how can we use different workspaces for different environment.

Terraform is an open source tool that allows you to define infrastructure for a variety of cloud providers (e.g. AWS, Azure, Google Cloud, DigitalOcean, etc) using a simple, declarative programming language and to deploy and manage that infrastructure using a few CLI commands.

Why terraform?

Terraform is one of the main tools in most infrastructure-as-code tool belts. Could its popularity be due to its ability to manage a wide variety of platforms (supporting OCI as well as many other cloud and hardware manufacturers)? Or maybe it's because it can run on your local machine (MacOS, Windows or Linux) or utilize a managed Terraform service (such as OCI Resource Manager)? No matter the reason, it's become a de-facto tool in the IT industry.

Download terraform

You can download the terraform from this url. Please download the proper package for your operating system and architecture.

Terraform is distributed as a single binary. Install Terraform by unzipping it and moving it to a directory included in your system's PATH .

You can find the SHA256 checksums for Terraform 1.0.5 online and you can verify the checksums signature file which has been signed using HashiCorp's GPG key. You can also download older versions of Terraform from the releases service.

Getting started with terraform

Once you have downloaded the required version of Terraform, you need to create terraform workspace as

terraform workspace new <workspace-name> 

Now you can list out all available terraform workspaces as

terraform workspace list 

To select any available workspace, run command as

terraform workspace select <workspace-name> 

If you want to find which workspace is currently active then run command

terraform workspace show 

If you want to delete a terraform workspace then run command

terraform workspace delete <workspace-name> 

Now initialise the working directory for terraform by executing command

terraform init 

This will intialize terraform and download cloud plugins and modules for terraform and initialize workspace(s).

Note: If you are working on multiple projects using same terraform script then first you need to activate project then reconfigure terraform environment.

terraform init --reconfigure 

Once terraform has initialized, you need to select the workspace for which you need to provision the infrastructure.

To see the list of available workspaces do:

terraform workspace select <workspace-name> 

Once the workspace is selected, validate / plan what resources terraform is going to create/delete/update using:

terraform plan 

If you want to override any env based variables file (file name is dev.variables.tfvars) then run

terraform plan -var-file=dev.variables.tfvars 

Once you are satisfied with what changes terraform will do, you can apply your changes using:

terraform apply -var-file=dev.variables.tfvars 

Now if you do not want to keep the infra on cloud then delete all the resources using:

terraform destroy -var-file=dev.variables.tfvars 

Related Links:

terraform downloads

Terraform tutorial

19 August: Rohit Bansal

Today, I'm going to explain how to install terraform and how can we use different workspaces for different environment.

Terraform is an open source tool that allows you to define infrastructure for a variety of cloud providers (e.g. AWS, Azure, Google Cloud, DigitalOcean, etc) using a simple, declarative programming language and to deploy and manage that infrastructure using a few CLI commands.

Why terraform?

Terraform is one of the main tools in most infrastructure-as-code tool belts. Could its popularity be due to its ability to manage a wide variety of platforms (supporting OCI as well as many other cloud and hardware manufacturers)? Or maybe it's because it can run on your local machine (MacOS, Windows or Linux) or utilize a managed Terraform service (such as OCI Resource Manager)? No matter the reason, it's become a de-facto tool in the IT industry.

Download terraform

You can download the terraform from this url. Please download the proper package for your operating system and architecture.

Terraform is distributed as a single binary. Install Terraform by unzipping it and moving it to a directory included in your system's PATH .

You can find the SHA256 checksums for Terraform 1.0.5 online and you can verify the checksums signature file which has been signed using HashiCorp's GPG key. You can also download older versions of Terraform from the releases service.

Getting started with terraform

Once you have downloaded the required version of Terraform, you need to create terraform workspace as

terraform workspace new <workspace-name> 

Now you can list out all available terraform workspaces as

terraform workspace list 

To select any available workspace, run command as

terraform workspace select <workspace-name> 

If you want to find which workspace is currently active then run command

terraform workspace show 

If you want to delete a terraform workspace then run command

terraform workspace delete <workspace-name> 

Now initialise the working directory for terraform by executing command

terraform init 

This will intialize terraform and download cloud plugins and modules for terraform and initialize workspace(s).

Note: If you are working on multiple projects using same terraform script then first you need to activate project then reconfigure terraform environment.

terraform init --reconfigure 

Once terraform has initialized, you need to select the workspace for which you need to provision the infrastructure.

To see the list of available workspaces do:

terraform workspace select <workspace-name> 

Once the workspace is selected, validate / plan what resources terraform is going to create/delete/update using:

terraform plan 

If you want to override any env based variables file (file name is dev.variables.tfvars) then run

terraform plan -var-file=dev.variables.tfvars 

Once you are satisfied with what changes terraform will do, you can apply your changes using:

terraform apply -var-file=dev.variables.tfvars 

Now if you do not want to keep the infra on cloud then delete all the resources using:

terraform destroy -var-file=dev.variables.tfvars 

Related Links:

terraform downloads

Comments 3
amit joshi: 20 August

Useful post, Amazing write up !

Rohit Bansal: 20 August

Thanks

Divick: 20 August

Very good