Title: Introduction to Terraform - Infrastructure as Code
1. What is Terraform?
- Terraform is an open-source tool by HashiCorp for building, changing, and
versioning infrastructure safely and efficiently.
- It uses a declarative configuration language (HCL - HashiCorp Configuration
Language).
2. Why Use Terraform?
- Infrastructure as Code (IaC)
- Version-controlled infrastructure definitions
- Multi-cloud compatibility (AWS, Azure, GCP)
3. Key Concepts
- Providers: Plugins for cloud platforms (e.g., AWS, Azure)
- Resources: The basic building blocks (e.g., aws_instance)
- Variables: Input parameters to customize modules
- Outputs: Return values from a Terraform module
- State: Tracks current infrastructure and changes
4. Basic Terraform Commands
- `terraform init`: Initialize the working directory
- `terraform plan`: Preview changes before applying
- `terraform apply`: Apply changes to reach desired state
- `terraform destroy`: Tear down resources
5. Sample Terraform File
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
6. Variables and Outputs
- Define variables in `variables.tf` and use them in main config
- Declare outputs to pass info between modules
7. Terraform State Management
- State file tracks deployed resources
- Use remote state (e.g., S3, Terraform Cloud) in teams
8. Modules
- Modules are reusable Terraform configurations
- Use public or private modules to simplify large projects
9. Best Practices
- Use version control for .tf files
- Protect state files and manage secrets securely
- Separate environments (dev/stage/prod)
10. Conclusion
Terraform is essential for managing infrastructure efficiently and repeatably.
Start small and build modular, scalable configurations.