IaC Terraform-1
IaC Terraform-1
Terraform
Terraform is an open-source infrastructure-as-code (IaC) tool developed by HashiCorp.
It allows you to define, provision, and manage infrastructure resources in a
consistent and version-controlled way across multiple cloud platforms, on-premises
environments, and even custom providers. It uses a declarative configuration
language, HashiCorp Configuration Language (HCL), to define resources and manage
their lifecycle.
Page 1|9
Created By: Dhruv Singhal | LinkedIn
2. Declarative Language (HCL): With HCL, you describe the desired state of your
infrastructure, and Terraform calculates the necessary steps to achieve it.
This is different from imperative programming, where you would define each step
manually.
3. Providers: Providers are plugins that allow Terraform to interact with various
cloud platforms (like AWS, Azure, and Google Cloud) or on-premises
environments. Providers define the specific resources that can be managed (like
aws_instance for EC2 instances in AWS or azurerm_storage_account for storage
accounts in Azure).
Terraform Workflow
Page 2|9
Created By: Dhruv Singhal | LinkedIn
3. Plan: Use terraform plan to generate and review an execution plan. The plan
shows a preview of what actions Terraform will take to make the infrastructure
match the configuration.
4. Apply: Apply the changes with terraform apply, which executes the plan and
makes the necessary changes to the infrastructure. After applying, Terraform
updates the state file to reflect the new state.
• terraform state: Manages the state file, allowing you to inspect and modify
state as needed.
provider "aws" {
region = "us-west-2"
}
Page 3|9
Created By: Dhruv Singhal | LinkedIn
variable "instance_type" {
type = string
default = "t2.micro"}
4. Output Block: Defines output values, useful for referencing information from
the configuration.
output "instance_ip" {
value = aws_instance.example.public_ip
}
• Remote Backends: Examples include AWS S3, Azure Blob, GCP Storage, and
HashiCorp's Terraform Cloud. Remote backends are essential for collaborative
work and team projects.
Terraform Modules
Modules in Terraform allow you to organize and reuse code effectively. You can break
down complex configurations into smaller modules, which can be maintained and
versioned separately.
module/
Using a module:
module "my_vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
Page 4|9
Created By: Dhruv Singhal | LinkedIn
• Remote State Management: Secure storage for state files, accessible to multiple
users.
• VCS Integration: Integrates with version control systems like GitHub, GitLab,
and Bitbucket for automated plan and apply steps.
Advantages of Terraform
Best Practices
Modules in Terraform:
Terraform modules are a way to organize and reuse code in Terraform configurations. A
module is essentially a collection of .tf files in a directory, which allows you to
group related resources together and manage them as a single entity. Modules are
powerful because they enable you to write once and reuse in multiple configurations,
making your infrastructure code modular, consistent, and easier to maintain.
• Reusability: Modules let you define infrastructure once and use it in multiple
places.
• Consistency: Modules enforce best practices and ensure consistency across
environments.
• Abstraction: Modules abstract away details, exposing only the variables and
outputs needed by other parts of your infrastructure.
Page 5|9
Created By: Dhruv Singhal | LinkedIn
Structure of a Module
mkdir ec2-instance-module
cd ec2-instance-module
variable "instance_type" {
type = string
default = "t2.micro"
variable "ami_id" {
type = string
variable "key_pair_name" {
type = string
Page 6|9
Created By: Dhruv Singhal | LinkedIn
ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_pair_name
tags = {
output "instance_id" {
value = aws_instance.example.id
output "public_ip" {
value = aws_instance.example.public_ip
This completes the module structure. The module ec2-instance-module is now ready to
be used in other Terraform configurations.
Now, create a new directory (e.g., project-root) to serve as the root module, which
will call the ec2-instance-module:
mkdir project-root
cd project-root
Page 7|9
Created By: Dhruv Singhal | LinkedIn
Inside this root module, create a main.tf file to call the EC2 instance module:
provider "aws" {
region = "us-west-2"
}
module "ec2_instance" {
source = "../ec2-instance-module" # Path to the module
instance_type = "t2.micro"
ami_id = "ami-0abcdef1234567890" # Replace with a valid AMI ID in your
region
key_pair_name = "my-key-pair"
}
output "ec2_instance_id" {
value = module.ec2_instance.instance_id
}
output "ec2_public_ip" {
value = module.ec2_instance.public_ip
}
In this example:
terraform init
terraform apply
Terraform also supports modules from the Terraform Registry, allowing you to use
community-maintained modules. For example, to use a public VPC module from the
registry:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.14.2"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b"]
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
Page 8|9
Created By: Dhruv Singhal | LinkedIn
enable_dns_support = true
enable_dns_hostnames = true
}
Note:
This VPC module provisions a fully configured VPC in AWS, including subnets, internet
gateway, NAT gateway, and route tables. Just customize the variables as needed!
Summary
Terraform modules are essential for organizing and reusing infrastructure code. They
can be local, shared across projects, or downloaded from the Terraform Registry. By
encapsulating reusable code, modules make infrastructure more modular, maintainable,
and scalable.
https://developer.hashicorp.com/terraform/language/modules
Page 9|9