Infrastructure as Code FAQ: Top Questions
2. What is Terraform and how does it work?
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp that enables you to define and provision infrastructure across a wide variety of cloud providers and services using a declarative configuration language called HashiCorp Configuration Language (HCL).
πΊοΈ Step-by-Step Instructions:
- Install Terraform from the official Terraform website.
- Write configuration files (usually
.tf
) defining the desired infrastructure resources. - Run
terraform init
to initialize the working directory with necessary plugins. - Run
terraform plan
to preview changes. - Run
terraform apply
to provision the infrastructure. - Use
terraform destroy
to tear down the infrastructure when it's no longer needed.
π₯ Example Input:
# main.tf
provider "aws" {
region = "us-west-1"
}
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name-123"
acl = "private"
}
π Expected Output:
A new S3 bucket named "my-unique-bucket-name-123" is created in AWS region us-west-1.
β Terraform CLI Commands:
terraform init
terraform plan
terraform apply
terraform destroy
π Detailed Explanation:
- Declarative Language: You describe what the infrastructure should look like, and Terraform figures out the how.
- Execution Plan: Terraform generates a plan before applying changes to give you insight into what will happen.
- State Management: Maintains a state file to keep track of resources, enabling incremental updates and comparisons.
- Provider Plugins: Extensible through providers for AWS, Azure, GCP, Kubernetes, and many more.
π οΈ Use Cases:
- Automating cloud infrastructure deployment and scaling.
- Environment replication across dev, test, and prod stages.
- Tracking infrastructure as code in source control (Git).
- Creating reusable and shareable modules for standard architectures.