DEV Community

Cover image for Infrastructure as Code (IaC) and Terraform
Mohammed Nasser
Mohammed Nasser

Posted on

11

Infrastructure as Code (IaC) and Terraform

Infrastructure as Code (IaC) revolutionizes how IT infrastructure is managed by using code-based configurations. This approach ensures:

Consistency: Eliminate manual errors by defining infrastructure in code.

Repeatability: Quickly replicate environments with standardized configurations.

Version Control: Track infrastructure changes just like application code.

Among IaC tools, Terraform, developed by HashiCorp, stands out for its simplicity, flexibility, and provider-agnostic approach. It facilitates creating, modifying, and versioning infrastructure seamlessly, making it a favorite among developers and DevOps engineers.


Key Terraform Commands: A Comprehensive Guide

1.Initialization and Setup

terraform init: Initializes a working directory containing Terraform configuration files.

terraform login: Saves credentials for Terraform Cloud.

terraform logout: Removes credentials for Terraform Cloud.


2.Planning and Execution

terraform plan: Generates an execution plan, outlining actions Terraform will take.

terraform apply: Applies the changes described in the Terraform configuration.

terraform apply --auto-approve: Automatically applies changes without requiring approval.

terraform apply -target=resource: Applies changes only to a specific resource.

terraform apply -var='key=value': Sets a variable's value directly in the command line.

terraform apply -var-file=filename.tfvars: Specifies a file containing variable definitions.

terraform plan -out=filename: Saves the generated plan to a file.


3.Resource Management

terraform destroy: Destroys all resources described in the configuration.

terraform destroy -target=resource: Destroys a specific resource.

terraform taint: Manually marks a resource for recreation.

terraform untaint: Removes the 'tainted' state from a resource.

terraform import: Imports existing infrastructure into Terraform state.


4.Validation and Output

terraform validate: Checks the syntax and validity of Terraform configuration files.

terraform output: Displays or retrieves output values from the state.

terraform show: Displays a human-readable output of the current state or a specific resource.


5.State Management

terraform state list: Lists resources within the Terraform state.

terraform state mv: Moves an item in the state.

terraform state rm: Removes items from the state.

terraform state pull: Pulls the state from a remote backend.

terraform state push: Pushes the state to a remote backend.

terraform refresh: Updates the state file against real resources in the provider.


6.Formatting and Visualization

terraform fmt: Rewrites configuration files to a canonical format.

terraform graph: Generates a visual representation of the Terraform dependency graph.


7.Workspace Management

terraform workspace list: Lists available workspaces.

terraform workspace select: Switches to another existing workspace.

terraform workspace new: Creates a new workspace.

terraform workspace delete: Deletes an existing workspace.


8.Troubleshooting and Lock Management

terraform force-unlock: Releases a locked state.


Terraform empowers you to take control of your cloud infrastructure with precision and efficiency. With this cheat sheet of essential commands, you're equipped to tackle real-world infrastructure challenges confidently.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay