0% found this document useful (0 votes)
3 views

Terraform Cheatsheet

This document is a comprehensive Terraform cheatsheet covering installation, key commands, configuration file structure, state management, best practices, common providers, error troubleshooting, and helpful links. It provides essential commands for initializing, validating, planning, applying, and destroying infrastructure, along with tips for managing configurations and state files. The document also emphasizes the importance of using version control and modularizing configurations.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Terraform Cheatsheet

This document is a comprehensive Terraform cheatsheet covering installation, key commands, configuration file structure, state management, best practices, common providers, error troubleshooting, and helpful links. It provides essential commands for initializing, validating, planning, applying, and destroying infrastructure, along with tips for managing configurations and state files. The document also emphasizes the importance of using version control and modularizing configurations.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Terraform Cheatsheet

1. Terraform Basics

- Install Terraform: [Terraform Installation Guide](https://www.terraform.io/downloads.html)

- Initialize Terraform:

terraform init

- Validate Configuration:

terraform validate

- Plan Changes:

terraform plan

- Apply Configuration:

terraform apply

- Destroy Infrastructure:

terraform destroy

2. Key Commands

- Format Code:

terraform fmt

- Show State:

terraform show

- List Resources:

terraform state list

- Remove a Resource from State:

terraform state rm <resource_name>


3. Terraform Configuration File Structure

- Providers:

provider "aws" {

region = "us-east-1"

- Resources:

resource "aws_instance" "example" {

ami = "ami-123456"

instance_type = "t2.micro"

- Variables:

variable "instance_type" {

default = "t2.micro"

- Outputs:

output "instance_id" {

value = aws_instance.example.id

4. Terraform State

- Check State:
terraform state show <resource_name>

- Move Resources Between States:

terraform state mv <source> <destination>

5. Best Practices

1. Use Remote State: Store your state files securely using AWS S3, Azure Blob Storage, etc.

2. Modularize Configurations: Organize your Terraform files into reusable modules.

3. Use .tfvars: Manage environment-specific configurations.

4. Version Control: Always use version control for your Terraform code.

5. Backup State Files: Ensure state files are backed up in case of corruption.

6. Common Terraform Providers

- AWS Provider:

provider "aws" {

region = "us-east-1"

- Azure Provider:

provider "azurerm" {

features = {}

- Google Cloud Provider:

provider "google" {

project = "my-project-id"
region = "us-central1"

7. Error Troubleshooting

- Error: No Provider Found:

- Run:

terraform init

- Error: State Lock:

- Unlock State:

terraform force-unlock <LOCK_ID>

8. Helpful Links

- Official Documentation: [Terraform by HashiCorp](https://www.terraform.io/docs/index.html)

- Terraform Registry: [Terraform Registry](https://registry.terraform.io/)

- Community Support: [Terraform Discuss Forum](https://discuss.hashicorp.com/c/terraform/)

9. Quick Tips

- Use terraform refresh to sync the state file with real-world resources.

- Always run terraform plan before applying changes.

- Leverage Workspaces for managing multiple environments.

terraform workspace new <workspace_name>

You might also like