Terraform Intro and Key Concepts

Terraform is an Infrastructure as Code (IaC) tool that allows you to define, provision, and manage infrastructure using a declarative configuration language. Terraform can help automate the setup and management of resources across various cloud providers (like AWS, Azure, and GCP), making infrastructure management repeatable and scalable.

Key concepts:

  1. Providers: Plugins that let Terraform manage different platforms (e.g., AWS, Azure, Kubernetes).
  2. Resources: The building blocks (e.g., VMs, storage buckets, load balancers) defined in the configuration files.
  3. State: Terraform keeps track of resources it creates in a state file, which helps manage infrastructure changes.
  4. Modules: Reusable pieces of infrastructure (e.g., a complete VPC setup) that can be shared and reused.
  5. Plan & Apply: The terraform plan command shows what will change, while terraform apply implements those changes.
  6. Variables: Allow for parameterizing configurations to make modules and resources more flexible.
  7. Outputs: These allow you to extract values from your Terraform configuration that can be used elsewhere in the system or passed to other Terraform configurations.
  8. Workspaces: A way to manage multiple environments (e.g., dev, staging, prod) within the same configuration. Each workspace has its own state file, allowing you to use the same code across environments with different parameters.
  9. Provisioners: These are used to execute scripts or commands on resources after they have been created. While not recommended for most use cases (since Terraform focuses on declarative state), they can be helpful for certain tasks like bootstrapping instances.
  10. Locking: Terraform uses a state locking mechanism (usually through the backend, such as S3 for AWS) to prevent simultaneous modifications to infrastructure from different users or automated systems.
  11. Backends: These define where your Terraform state is stored (e.g., locally, in an S3 bucket, or using a remote state service). Storing state remotely is key for collaboration in teams.
  12. Data Sources: These allow you to fetch or reference existing resources outside of your Terraform configuration. This is useful when you need to manage infrastructure alongside pre-existing components or share resources between configurations.
  13. Lifecycle Management: With lifecycle rules, you can control how Terraform manages resources over time. For instance, you can set dependencies between resources, manage how resources are created, updated, or destroyed, or prevent a resource from being accidentally destroyed.