Terraform Basics & Concepts
Terraform is an open-source infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. It uses configuration files to describe the infrastructure components needed to run an application.
Key Concepts
-
Infrastructure as Code (IaC):
- Terraform enables you to define infrastructure in configuration files that can be versioned, reused, and shared.
-
Providers:
- Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs.
- Common providers include AWS, Azure, Google Cloud, and more.
-
Resources:
- Resources are the most important element in Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.
-
Modules:
- Modules are containers for multiple resources that are used together. A module can include resources, input variables, output values, and even other modules.
-
State Files:
- Terraform records information about what infrastructure it created in a state file. This state is used by Terraform to map real-world resources to your configuration and keep track of metadata.
-
HCL (HashiCorp Configuration Language):
- Terraform uses HCL, which is designed to be both machine friendly and human-readable. It provides a flexible way to describe infrastructure and set variables.
Example
Here's a simple Terraform configuration example:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
This example sets up an AWS provider and defines a resource for an AWS instance.