Terraform

Terraform Cheat-Sheet

Terraform is a free and open-source infrastructure as code automation tool, used for provisioning, changing, and versioning resources across any environment.


πŸ” Overview

  • Product Type: Infrastructure as Code (IaC) Tool

  • Focus: Automating and managing infrastructure deployments across different cloud providers and environments.

  • Use Cases: Managing virtual machines, networks, storage, and other infrastructure resources.


πŸ”§ Format and Validate

COMMANDDESCRIPTION
terraform fmtReformat your configuration in the standard style
terraform validateCheck whether the configuration is valid

βš™οΈ Initialize Working Directory

COMMANDDESCRIPTION
terraform initPrepare your working directory for other commands

πŸš€ Plan, Deploy, and Cleanup

COMMANDDESCRIPTION
terraform apply --auto-approveCreate or update infrastructure without a confirmation prompt
terraform destroy --auto-approveDestroy previously-created infrastructure without confirmation prompt
terraform plan -out plan.outOutput the deployment plan to plan.out
terraform apply plan.outUse plan.out to deploy infrastructure
terraform plan -destroyOutputs a destroy plan
terraform apply -target=aws_instance.myinstanceOnly apply/deploy changes to targeted resource
terraform apply -var myregion=us-east-1Pass a variable via CLI while applying a configuration
terraform apply -lock=trueLock the state file so it can't be modified
terraform apply refresh=falseDo not reconcile state file with real-world resources
terraform apply --parallelism=5Number of simultaneous resource operations
terraform refreshReconcile the state in Terraform state file with real-world resources
terraform providersGet information about providers used in the current configuration

πŸ§‘β€πŸ’» Workspaces

COMMANDDESCRIPTION
terraform workspace new myworkspaceCreate a new workspace
terraform workspace select defaultChange to a workspace
terraform workspace listList all workspaces

πŸ“‚ State Manipulation

COMMANDDESCRIPTION
terraform state show aws_instance.myinstanceShow details stored in the Terraform state file
terraform state pull > terraform.tfstateOutput Terraform state to a file
terraform state mv aws_iam_role.my_ssm_role module.mymoduleMove a resource tracked via state to a different module
terraform state replace-provider hashicorp/aws registry.custom.com/awsReplace an existing provider with another
terraform state listList all resources tracked in the Terraform state file
terraform state rm aws_instance.myinstanceUnmanage a resource, delete it from the Terraform state file

πŸ“₯ Import and Outputs

COMMANDDESCRIPTION
terraform import resourcetype.myresource <id>Import a resource
terraform outputList all outputs
terraform output <output>List a specific output
terraform output -jsonList all outputs in JSON format

🌩️ Terraform Cloud

COMMANDDESCRIPTION
terraform loginLogin to Terraform Cloud with an API token
terraform logoutLogout from Terraform Cloud

πŸ› οΈ Import Existing Resources

You can use terraform import to bring existing infrastructure into Terraform management. The basic syntax for importing a resource is:

terraform import <RESOURCE_TYPE>.<NAME> <ID>

For example:

terraform import aws_instance.my_instance i-1234567890abcdef0

This command will allow Terraform to start managing the resource identified by the provided <ID>.


  • Infrastructure as Code (IaC)

  • Automation Tools

  • AWS CloudFormation: Alternative IaC tool for AWS.

  • Kubernetes: IaC for containerized workloads.


πŸ” Explore More

  • Terraform Basics: Learn the fundamentals of using Terraform.

  • Terraform Providers: Detailed documentation on using providers with Terraform.

  • Terraform Modules: A deeper dive into using modules in your Terraform configurations.

  • Terraform State Management: Learn how Terraform manages state files and how to manipulate them.

  • Terraform Cloud: Explore Terraform's cloud-based infrastructure management tools.


🏷️ Tags

#terraform
#infrastructure-as-code
#automation
#cloud-architecture
#devops
#state-management
#terraform-cloud