How to use infrastructure as code with Terraform
· Category: Cloud Computing
Short answer
Terraform uses declarative configuration files to create, update, and version infrastructure across cloud providers.
Steps
- Install Terraform and configure provider credentials.
- Write a
.tffile:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t3.micro"
}
- Initialize:
terraform init
- Plan and apply:
terraform plan
terraform apply
Tips
- Use remote state backends (S3, Azure Blob) for team collaboration.
- Modules promote reuse and standardization.
- Run
terraform fmtandvalidatein CI pipelines.
Common issues
- State conflicts: use state locking with DynamoDB or Terraform Cloud.
- Drift: resources changed manually cause unexpected plans.