Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

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:

  1. Install Terraform from the official Terraform website.
  2. Write configuration files (usually .tf) defining the desired infrastructure resources.
  3. Run terraform init to initialize the working directory with necessary plugins.
  4. Run terraform plan to preview changes.
  5. Run terraform apply to provision the infrastructure.
  6. 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.