Terraform is a robust Infrastructure as Code (IaC) tool that enables organizations to automate cloud infrastructure. Knowing how to use Terraform’s Command Line Interface (CLI) effectively is critical to managing resources, planning infrastructure changes, and automating deployments. This guide will dive deep into essential Terraform CLI commands, covering everything from initialization and configuration to state management, modules, and workspaces.

Initialization and Configuration

1. Preparing Your Terraform Environment

The first step in any Terraform project is initializing the environment. This is done using the following command:

terraform init

This command initializes your working directory, downloads necessary provider plugins, and prepares the backend for state storage.

2. Validating Configuration Files

Before applying infrastructure changes, it’s crucial to validate the syntax and structure of your Terraform configuration files:

terraform validate

This command checks for potential syntax issues, ensuring your configuration is valid.

3. Planning and Applying Infrastructure Changes

Once your environment is initialized and validated, you can preview the changes Terraform will make to your infrastructure with the following:

terraform plan

The plan command outputs a summary of the actions Terraform will take. To apply the changes, use:

terraform apply

4. Removing Managed Infrastructure

When you want to delete the infrastructure managed by Terraform, use the destroy command:

terraform destroy

This removes all the resources in the current configuration but ensures you have complete visibility of what will be deleted before proceeding.

State Management

Terraform uses a state file to keep track of your infrastructure. Managing this state is crucial for accurate deployment.

1. Listing and Manipulating Resources

You can view the resources currently managed by Terraform with the following:

terraform state list

This command lists all resources in the state file. You can also manipulate specific resources using:

terraform state mv

or

terraform state rm

to move or remove resources in the state file.

2. Pulling and Pushing State Information

When working with remote backends, you can manually pull or push state information using:

terraform state pull

or

terraform state push

This ensures that your local environment and the remote backend are in sync.

3. Importing and Refreshing Infrastructure State

If you need to bring existing infrastructure under Terraform management, use the import command:

terraform import [resource] [id]

To ensure that the state file is up-to-date with the real-world infrastructure, you can refresh the state with the following:

terraform refresh

Modules and Providers

Modules and providers help Terraform manage and scale infrastructure efficiently.

1. Managing Module Updates

To pull the latest changes for a module, use:

terraform get -update

This command ensures that your project uses the latest version of your modules.

2. Viewing Required Providers

To check which providers your configuration requires, use:

terraform providers

This outputs a list of all the providers in use and their version constraints.

3. Marking Resources for Recreation

If you want to force Terraform to recreate specific resources during the next application, use the following:

terraform taint [resource]

This marks the resource for recreation, ensuring a fresh deployment of that component.

Terraform Workspace Commands

Workspaces in Terraform help manage multiple environments (e.g., development, production) within a single configuration.

1. Creating and Managing Workspaces

To create a new workspace, use:

terraform workspace new [workspace-name]

You can list all available workspaces with:

terraform workspace list

2. Switching Between Workspaces

To switch between different workspaces, use:

terraform workspace select [workspace-name]

3. Deleting Workspaces

To delete a workspace, first switch to a different workspace and then use:

terraform workspace delete [workspace-name]

This removes the workspace and its associated state.

Additional Commands

Here are some other essential commands that can help streamline Terraform workflows:

1. Releasing Stuck Locks

If a Terraform operation is interrupted, it might leave the state file locked. To manually release the lock, use:

terraform force-unlock [lock-id]

2. Checking Terraform Version

To verify the version of Terraform you are running, use:

terraform version

3. Accessing Command Help

For a detailed explanation of any Terraform command, use the help flag:

terraform [command] -help

This provides specific guidance on each command’s available options.

4. Managing Remote Credentials

When using a remote backend, Terraform requires credentials. You can configure these using environment variables or directly in your terraform.tfvars file. To avoid security risks, never hardcode credentials in your configuration files.

Conclusion

Mastering the Terraform CLI commands is essential for managing cloud infrastructure efficiently. These commands form the backbone of Terraform operations, from initialization and configuration to handling state and managing workspaces. Understanding these commands will ensure smooth and effective infrastructure management, whether you’re working on large-scale deployments or small test environments.

References

Getting started with Terraform support for AWS SAM CLI

Better together: AWS SAM CLI and HashiCorp Terraform