Skip to content

Code Blocks

Terraform is an Infrastructure as Code (IaC) tool that allows you to define, provision, and manage cloud infrastructure using declarative configuration files. It enables you to create, modify, and version your infrastructure efficiently across various cloud providers, with AWS being one of the most popular.

1. Providers

Providers are plugins that Terraform uses to interact with various cloud platforms and services. They act as a bridge between Terraform and the APIs of different infrastructure providers. For example:

provider "aws" {
  region = "us-east-2"
}

2. Resources

Resources represent the individual components of your infrastructure, such as virtual machines, databases, or network interfaces. You define these in your Terraform configuration files:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"  
  instance_type = "t2.micro"
}

4. Modules

Modules are reusable components that encapsulate a set of resources

module "vpc" {
  source = "./modules/vpc"
  cidr_block = "10.0.0.0/16" 
}

5. Variables

Variables allow you to parameterize your Terraform configurations, making them more flexible and reusable

variable "instance_type" {
  default = "t2.micro" 
} 

resource "aws_instance" "example" {
  instance_type = var.instance_type 
}

6. Outputs

Outputs allow you to extract and display specific values from your Terraform-managed resources

output "instance_ip" {
  value = aws_instance.example.public_ip
}

7. Data Sources

Data sources allow Terraform to use information defined outside of Terraform, such as existing resources or other Terraform states

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"] # Canonical
}