What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to provision, manage, and version infrastructure using a declarative configuration language.
Key Concepts:
1. Providers
-
Plugins that let Terraform interact with APIs of cloud platforms (e.g., AWS, Azure, GCP) or services (e.g., GitHub, Kubernetes).
-
Example:
provider "aws" { region = "us-west-2" }
2. Resources
-
The building blocks of infrastructure (e.g., EC2 instances, S3 buckets).
-
Declared using
resource
blocks. -
Example:
3. Variables
-
Allow configuration flexibility.
-
Declared using
variable
blocks and passed via CLI,.tfvars
, or environment variables.
4. Outputs
-
Provide information after a Terraform apply.
-
Example:
5. State
-
Terraform keeps track of infrastructure in a state file (
terraform.tfstate
). -
This file is critical for tracking changes between your configuration and real-world infrastructure.
6. Modules
-
Reusable groups of resources.
-
Promote code reuse and organization.
Basic Commands:
Command | Purpose |
---|---|
terraform init | Initializes Terraform and downloads providers |
terraform plan | Shows what Terraform will do before applying |
terraform apply | Applies the configuration to build the infrastructure |
terraform destroy | Destroys the infrastructure defined in the code |
terraform validate | Validates the configuration syntax |
Typical Workflow:
-
Write
.tf
files (Terraform configuration) -
Run
terraform init
-
Run
terraform plan
-
Review changes
-
Run
terraform apply
Let me know if you’d like a simple project example or guidance on setting up Terraform with a specific cloud provider.
Comments
Post a Comment