Terraform Cheatsheet
Terraform Cheatsheet
1. Terraform Basics
- 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:
- Providers:
provider "aws" {
region = "us-east-1"
- Resources:
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>
5. Best Practices
1. Use Remote State: Store your state files securely using AWS S3, Azure Blob Storage, etc.
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.
- AWS Provider:
provider "aws" {
region = "us-east-1"
- Azure Provider:
provider "azurerm" {
features = {}
provider "google" {
project = "my-project-id"
region = "us-central1"
7. Error Troubleshooting
- Run:
terraform init
- Unlock State:
8. Helpful Links
9. Quick Tips
- Use terraform refresh to sync the state file with real-world resources.