Terraform CLI
The Terraform CLI is a command-line tool that enables you to define, manage, and interact with your infrastructure.
It works by reading all the HCL files in your project and synthesizing them to create a predicted state based on the configuration given.
When you choose to apply the changes, it logically creates a dependency.
Commands are either typed in manually or run automatically from a script and they are the same whether you are on Linux or Windows or MacOS.
Installing the Terraform CLI
Section titled “Installing the Terraform CLI”Before we can use the Terraform CLI, we need to install it. The easiest way to do this is to download the appropriate package for your operating system from the official Terraform website (https://www.terraform.io/downloads.html), and then extract it to a location in your PATH environment variable. Once installed, you can verify that the Terraform CLI is available by running the following command:
terraform version
If the command is successful, you should see the version number of the Terraform CLI printed to the console.
Initializing a project
Section titled “Initializing a project”Once you have installed the Terraform CLI, the next step is to initialize a Terraform project. This is done by running the terraform init command in the root directory of your Terraform project. This command downloads any necessary plugins and sets up the backend for storing the Terraform state.
terraform init
Creating and Managing Infrastructure Resources
Section titled “Creating and Managing Infrastructure Resources”With the Terraform CLI initialized, we can start creating and managing infrastructure resources. To create a new resource, we use a Terraform configuration file in HashiCorp Configuration Language (HCL) format. Here’s an example configuration file that creates an Amazon S3 bucket:
resource "aws_s3_bucket" "example" { bucket = "my-bucket" acl = "private"}
To create the resource, we use the terraform apply command:
terraform apply
This command shows the changes that Terraform will make to the infrastructure and prompts us to confirm the changes before they are made.
To update or delete an existing resource, we simply modify the configuration file and re-run the terraform apply command.
Common Terraform CLI Commands and Options
Section titled “Common Terraform CLI Commands and Options”Here are some of the most commonly used Terraform CLI commands and options:
terraform plan: Shows the changes that Terraform will make to the infrastructure without actually making any changes.terraform apply: Applies the changes to the infrastructure.terraform destroy: Destroys all resources managed by Terraform.terraform state: Allows you to view and manage the Terraform state.terraform workspace: Allows you to create and manage Terraform workspaces.
Some common options that can be used with these commands include -var to specify input variables, -target to apply changes to a specific resource, and -parallelism to set the number of parallel resource creations and updates.
You can see more details on the available commands here.
Conclusion
Section titled “Conclusion”In this section we have discussed the setup required to use the Terraform CLI and introduced the typical workflow used by developers with Terraform: init, plan and apply.