In this blog, we will learn about how to install terraform on ubuntu 18.04. Along with some useful commands of terraform. As well as to learn more about terraform click.
Prerequisites
- Ubuntu 18.04
- Root privileges
What we will do?
- Download Terraform
- Setup Terraform
- Run Basic commands of Terraform
- Conclusion
Download Terraform
Terraform uses HashiCorp Configuration Language (HCL) to manage environments of Operators and Infrastructure teams. To download open ternimal with root privileges and enter commands.
1 2 3 |
sudo apt update sudo apt install -y zip sudo wget https://releases.hashicorp.com/terraform/0.12.29/terraform_0.12.29_linux_amd64.zip |
Now unzip the downloaded terraform file in folder. Use any name for folder, in my case it is terraform.
1 2 3 |
mkdir ~/terraform cd ~/terraform unzip ~/terraform_0.12.29_linux_amd64.zip |
By this commands the terraform file in unzip under terraform folder.
Setup Terraform
Now to setup terraform so that we can use terraform from any path in terminal.
Check for the hidden file in home directory “~” of linux by command and open profile by any editor.
1 2 |
ls -al sudo nano ~/.profile |
And now export the path of terraform folder in .profile file. At last of .profile file enter the line. In my case path is “~/terraform”.
1 |
export PATH="$PATH:~/terraform" |
Now save the file with “ctrl+x” to exit, “y” to save and press “enter”.
Now this will not automatically update the path for current session to do this run command.
1 |
source ~/.profile |
By this command it will update path in environment variable and we can use terraform from any location of terminal.
Run basic Commands of Terraform
There are so many commands in terraform we can discuss about some.
- Initialization
- Plan and Apply
- Formatting and Validation
- State and Graph
- Destroy
Initialization
It is the first command to run any new configuration, and also use to check an existing configuration from version control is “terraform init”.
Terraform use to support hundreds of infrastructure and service provider, it uses a plugin-based architecture. The command “terraform init” is use to downloads and installs providers used in the configuration.
As we see when we build the infrastructure.
In our example “terraform init” is use to downloads the AWS provider and also installs some hidden sub directories of the current working directory.
By this command the version of plugin was installed.
Plan and Apply
Use “terraform apply” within the same directory in which we save .tf file.
From terraform 0.11 and above versions we don’t required “terraform plan” for showcase of execution plan before applying it.
With the help of “terraform apply” it will first show execution plan and then ask to build the infrastructure or not.
In our example we are building EC2 instance. Along with this it wrote some data in “terraform.tsftate” file. This file is very important because it keeps track of the Ids of created resources so that Terraform knows what it is managing.
Formatting and Validation
We use “terraform fmt” when different teams are use to write the files and modules. With the use of “terraform fmt” it enables standardization which is use to updates configurations automatically in the current directory.
With the help of “terraform validate” we will use to check and report errors within modules, attribute names, and value types.
State and Graph
With the help of “terraform state” we manage advance state terraform has build in command called “terraform state”. To modify the state file by finding resources in the terraform.tfstate file with “terraform state list”.
To see visual representation of either a configuration or execution plan we use command “terraform graph”. The output of this command “terraform graph” is in the DOT format. Which can be used by GraphViz to generate charts.
Destroy
Now to destroy all the resources of terraform directory we use “terraform destroy”.
With the help of “-” in the execution plan it will show the resources which gone to be destroyed after completion of process.
To complete write “yes” for confirmation.
Conclusion
With the help of installation steps you can easily install terraform in your ubuntu 18.04 machine. And with the help of this commands you can easily start with terraform. This all commands are important as well as very useful commands of terraform.
In case of any help or query, please contact us or raise a ticket.