1. Terraform workflow using terraform commands
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define, provision, and manage infrastructure resources across a variety of cloud providers (like AWS, Azure, Google Cloud), as well as other services using a high-level configuration language called HashiCorp Configuration Language (HCL).
Key Components of Terraform
Configuration Files: These are the .tf files where you define the infrastructure you want to provision. This is where you describe the desired state of your resources.
Providers: Providers are plugins that allow Terraform to interact with APIs of cloud providers, services, or platforms like AWS, Azure, Google Cloud, Kubernetes, etc.
Resources: A resource is a component in your infrastructure (e.g., EC2 instances, S3 buckets, databases). Resources are declared in the configuration files.
State: Terraform keeps track of your infrastructure in a state file (terraform.tfstate). This is how Terraform understands what’s been created and what it needs to manage.
Modules: These are reusable, self-contained packages of Terraform code that can be shared and used in multiple configurations.
Terraform Workflow
The general workflow of using Terraform consists of a few key steps: writing the code, initializing the project, planning, applying, and managing the infrastructure.
1. Write Terraform Configuration Files
Create .tf files to define the infrastructure resources you want to provision.
Example: A simple configuration to create an AWS EC2 instance might look like this:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
You can define multiple resources, outputs, and variables in these configuration files.
2. Initialize Terraform
Run the terraform init command to initialize your Terraform working directory. This command downloads the necessary provider plugins (e.g., AWS provider).
3. Plan Your Infrastructure
Run terraform plan to see what Terraform will do based on the configuration files. It compares the current state (if any) to the desired state and shows what resources will be created, modified, or destroyed.
terraform plan
The plan will not make any changes to your infrastructure. It just shows a preview of what Terraform intends to do.
4. Apply the Configuration
Once you're satisfied with the plan, run terraform apply to provision the infrastructure as defined in the configuration files.
terraform apply
Terraform will prompt for confirmation before applying the changes unless you use -auto-approve flag to skip the confirmation.
Example output:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
This will create the EC2 instance in your AWS account (or other resources depending on your configuration).
5. Manage the State
Terraform maintains a state file (terraform.tfstate) that keeps track of all resources it manages. This file should not be modified manually.
You can also store the state remotely (e.g., in an S3 bucket for AWS or in Terraform Cloud) for team collaboration.
6. Modify or Destroy Infrastructure
If you need to make changes to the infrastructure, update the .tf files and re-run terraform apply.
To remove resources, you can run terraform destroy:
terraform destroy
This command will destroy all the resources defined in your configuration.
Terraform Commands in a Typical Workflow
terraform init: Initialize the working directory containing Terraform configuration files.
terraform plan: Generate and show an execution plan (what will be created, changed, or destroyed).
terraform apply: Apply the changes defined in the Terraform configuration to your infrastructure.
terraform destroy: Tear down all the infrastructure that Terraform manages (optional).
terraform validate: Validate the syntax and configuration of your Terraform files.
terraform fmt: Format your Terraform configuration files according to standard style conventions.
Example Workflow
Here’s a step-by-step example of a typical Terraform workflow for provisioning an AWS EC2 instance:
1. Create the configuration file (main.tf):
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
2 Initialize the directory:
terraform init
3. Plan the infrastructure:
terraform plan
4. Apply the configuration:
terraform apply
Verify the infrastructure (in AWS Console):
Go to the AWS Console and verify the EC2 instance is running.
Modify the configuration (if needed):
Change the instance type or other configurations and run terraform apply again.
Destroy the infrastructure:
terraform destroy
Best Practices
Use Modules: For reusability, abstract common resources into modules.
Remote State Management: Store state files remotely (e.g., in AWS S3 with state locking using DynamoDB) to avoid issues with state file consistency.
Version Control: Always store your Terraform configuration files in a version control system (e.g., Git).
Environment Separation: Use different workspaces or separate directories for different environments (e.g., dev, staging, production).
Conclusion
Terraform provides a streamlined and repeatable approach to managing infrastructure. By defining your infrastructure in code, you can version control, share, and automate infrastructure provisioning across multiple environments. The workflow helps ensure consistency and traceability, making Terraform a powerful tool for infrastructure automation.
Terraform Workflow (Production-Ready)
Write Infrastructure as Code
Files (Typical Structure)
terraform/
├── main.tf
├── providers.tf
├── variables.tf
├── outputs.tf
├── backend.tf
├── terraform.tfvars
└── modules/
Key Rules
-
One concern per file
-
No hardcoded values
-
Use variables & modules
-
Version-pin providers
Initialize Terraform
terraform init
What Happens
-
Downloads providers
-
Configures backend (remote state)
-
Initializes modules
-
Creates
.terraform/
Re-run when:
-
Provider changes
-
Backend changes
-
Modules change
Format & Validate Code (MANDATORY)
terraform fmt
terraform validate
✔ Enforces standard formatting
✔ Catches syntax errors early
Plan Infrastructure Changes
terraform plan
Purpose
-
Shows what will be created / modified / destroyed
-
No changes applied yet
Best Practice
terraform plan -out=tfplan
Apply Changes
terraform apply tfplan
terraform apply
What Happens
-
Terraform compares desired state vs current state
-
Applies only the delta
⚠️ Never apply blindly in production
State Management (CRITICAL)
Terraform State
-
Tracks real infrastructure
-
Stored in
.tfstate
Best Practice
Use remote state:
-
AWS S3 + DynamoDB
-
Azure Storage Account
-
Terraform Cloud
-
OCI Object Storage
✔ Enables locking
✔ Prevents corruption
✔ Team collaboration
Change & Drift Management
terraform plan
Refresh State
terraform refresh
Destroy Infrastructure (Careful!)
Comments
Post a Comment