Terraform is an open-source Infrastructure as Code (IaC) tool that allows to define, provision, and manage infrastructure using a high-level configuration language (HashiCorp Configuration Language or HCL). Terraform supports a variety of cloud providers (e.g., AWS, Azure, GCP) and enables you to manage both cloud and on-premises infrastructure resources consistently.
- Providers: Plugins that enable Terraform to interact with APIs of cloud platforms and other services. Examples include aws, azure, google, and others. Providers define the types of resources that Terraform can manage on each platform.
- Resources: The fundamental components Terraform manages. Resources represent services like virtual machines, databases, networks, etc. Each provider has its own set of resources with specific attributes and configuration options.
- State: Terraform maintains a state file (terraform.tfstate) that tracks the current state of your infrastructure. The state allows Terraform to compare the actual infrastructure with the desired configuration and determine what changes need to be made. Storing this state in a shared location (e.g., an S3 bucket for AWS) is essential for team collaboration.
- Execution Plans: Terraform generates an execution plan to show which actions it will take to achieve the desired infrastructure state. This plan is generated by running terraform plan and helps you review changes before applying them.
- Modules: A way to organize and reuse configurations. A module is a container for multiple resources and can be called with different configurations, similar to functions in programming.
- Input and Output Variables: Variables allow you to parameterize configurations. Input variables make Terraform configurations more flexible, while output variables provide values from Terraform resources for use outside the configuration.
- Write Configuration Files: Define resources in
.tf
files using HCL syntax. This configuration declares the desired state of your infrastructure.
- Initialize: Run
terraform init
to initialize the working directory. Terraform will download the provider plugins specified in the configuration.
- Plan: Run
terraform plan
to create an execution plan. This step shows the changes Terraform will make to reach the desired state, including additions, modifications, or deletions of resources.
- Apply: Run
terraform apply
to apply the changes and create or modify infrastructure according to the execution plan.
- Destroy: Run
terraform destroy
to delete all infrastructure managed by Terraform in that configuration.
Example
- Module Structure: Organize modules in folders. Each module typically has its own
main.tf
, variables.tf
, and outputs.tf
files.
modules/
└── s3_bucket/
├── main.tf
├── variables.tf
└── outputs.tf
- Define the Module (
main.tf
):
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
acl = "private"
}
- Define Variables for the Module (
variables.tf
):
variable "bucket_name" {
description = "Name of the S3 bucket"
type = string
}
- Define Outputs for the Module (
outputs.tf
):
output "bucket_arn" {
description = "The ARN of the S3 bucket"
value = aws_s3_bucket.bucket.arn
}
- Call the Module in the Root Configuration:
provider "aws" {
region = "us-west-2"
}
module "example_bucket" {
source = "./modules/s3_bucket"
bucket_name = "example-bucket-name"
}