Mastering Terraform: Essential Commands for Cloud Infrastructure Management
In the modern world of convenience, the Instant Pot has revolutionized home cooking, making it quicker, easier, and more efficient. Similarly, in the realm of cloud computing, Terraform has transformed the way we manage infrastructure. Just as the Instant Pot simplifies meal preparation by combining multiple cooking functions into one appliance, Terraform streamlines cloud infrastructure management by providing a unified tool to define, provision, and manage resources.
To use Terraform, we should know how to talk to it. Refer to my previous article for a brief introduction to Terraform here. As it is an open-source tool, we use the Terraform registry to get the provider and resources for deploying our infrastructure. After getting the required services, we should place certain commands to deploy the collected resources to get into action. That's what we say ‘Terraform Commands”
Terraform Commands are not like Linux commands or Docker Commands, these are straightforward. Just with two commands, one can deploy the infrastructure as quickly as possible.
To view a list of the commands available in your current Terraform version, run terraform
with no additional arguments:
To get specific help for any specific command, use the -help
option with the relevant subcommand. For example, to get help with the "init" subcommand you can run terraform init -help
.
The inline help built into Terraform CLI describes the most important characteristics of each command.
As the picture shows, this article will focus on the essential commands needed to run the terraform.
Main Terraform Commands:
- terraform init: Initializes a working directory containing Terraform configuration files. This command downloads the necessary provider plugins and sets up the backend for storing state files.
terraform init
2. terraform validate: Validates the Terraform configuration files for syntax and internal consistency. It does not check whether the configuration is correct for the given infrastructure.
terraform validate
3. terraform plan: Creates an execution plan, which shows what actions Terraform will take to reach the desired state of the infrastructure as described in the configuration files.
terraform plan
4. terraform apply: Applies the changes required to reach the desired state of the configuration. This command executes the actions proposed in the plan.
terraform apply
5. terraform destroy: Destroys all the infrastructure managed by the Terraform configuration. This is used to clean up and remove all resources.
terraform destroy
Essential Terraform Commands:
6. terraform fmt: Formats the Terraform configuration files to a canonical format and style, making the code easier to read and maintain.
terraform fmt
7. terraform show: Shows the current state or a saved plan. This is useful for understanding the current state of the infrastructure.
terraform show
8. terraform output: Displays the values of output variables defined in the configuration.
terraform output
9. terraform refresh: Updates the state file with the real-world resources’ current state. This is useful to reconcile the state file with any manual changes made to the infrastructure.
terraform refresh
10. terraform show: Reads and outputs a Terraform state or plan file in a human-readable form. If no path is specified, the current state will be shown.
terraform [global options] show [options] [path]
Options:
- no-color : If specified, output won’t contain any color.
-json : If specified, output the Terraform plan or state in a machine-readable form.
Example Workflow
Here is an example workflow using these commands:
First, we have to create a configuration file with an extension ‘.tf’ For example let the file be ‘main.tf’
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-xxxxxxxxxxxxxx" #Specify your AMI
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
The above code snippet is the content of the main.tf which is going to spin up an EC2 instance, named “Hello World”.
Assuming the AWS credentials are set for the environment, we are going to spin up the EC2 instance through Terraform.
We have to initialize the terraform environment. type ‘terrafrom init’ on your IDE to start. This part is the first and mandatory step, without we cannot proceed further.
terraform init
next to format the code you can use ‘terraform fmt’. This is optional but very helpful when your configuration files are very long and multiple resources are used. Since my code is in proper format it did not show any result. if it has formatted it will generate the the filename as ‘main.tf’ as output
Next validate your file. use ‘terraform validate’
This confirms the configuration file is ready for the next step which is plan.
terraform plan command gives the summary of what actions the terraform is going to do, i.e to add, to change, or to destroy resources.
Next ‘terraform apply’. This is mandatory for the plan to go into action. In other words, the output of the command is our required infrastructure.
This command will ask for your approval before going for execution. The only answer it will accept is yes other than that will stop the flow.
Once approved, it will execute its role and will show its summary. Here, one resource is added.
This can be checked on the AWS console. we should have an EC2 instance named “Hello World”
Clean Up:
The resources were up by one command. Similarly, the resources created by terraform can be shut down by a single command. ‘terraform destroy’ is the command that is going to that honor.
Again this command will ask for approval, once approved typing ‘yes’ will clean up all the resources created by it earlier.
Checking the same at the console.
Thus, this article shows the workflow of Terraform and its commands. Hope this will be helpful for anyone who wants to learn terraform. I will be adding more articles about the projects currently I am working on.