Terraform
Terraform
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool created by
HashiCorp. It lets you define cloud and on-prem infrastructure
using declarative configuration files.
Terraform Installation:
Visit official installation documentation and follow the steps.
https://developer.hashicorp.com/terraform/install
www.linkedin.com/in/ajinkya-pame-4a752b346
2. Resources:
a. Resources are the actual infrastructure components
you want to create/manage — EC2 instances, S3
buckets, VPCs, databases, etc.
b. Resources define what infrastructure you want.
Example:
resource "aws_instance" "my_server" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
1. main.tf:
a. The primary Terraform configuration file where you
define your provider, resources, modules, and any
other configs.
b. It’s often the starting point for a Terraform project,
main entry for your infrastructure definitions.
2. variables.tf:
www.linkedin.com/in/ajinkya-pame-4a752b346
a. File where you declare variables that your Terraform
configuration will use.
b. Variables allow you to parameterize your configs for
flexibility.
Example:
variable "region" {
description = "AWS region to deploy"
type = string
default = "us-east-1"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
}
3. outputs.tf:
a. File where you define outputs — values Terraform
will print/display after apply.
b. Outputs let you easily extract useful info like
instance IPs, resource IDs, or URLs.
Example:
output "instance_id" {
value = aws_instance.my_server.id
}
output "instance_public_ip" {
value = aws_instance.my_server.public_ip
}
www.linkedin.com/in/ajinkya-pame-4a752b346
4. terraform.tfvars:
a. A file to store input variable values.
b. To keep variables (like region, instance type, AMI ID)
separate from the code, making your configs reusable
and easier to maintain.
Example:
region = "us-east-1"
instance_type = "t2.micro"
ami = "ami-12345678"
5. Modules:
a. Modules are containers for multiple resources that
are used together. Think of them like reusable
building blocks or templates.
b. They help you organize code, re-use infrastructure
patterns, and keep your configurations clean.
Example:
Instead of writing all resources inline, you can create
a vpc module and call it in your main config.
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
www.linkedin.com/in/ajinkya-pame-4a752b346
b. Terraform uses this state file to know what exists,
what changed, and what to update. Without it,
Terraform would not know what infrastructure it
controls.
c. Important:
▪ The state file contains resource IDs, metadata, and
other info.
▪ Should be protected (don’t commit to public
repos).
▪ Can be stored remotely (S3, Terraform Cloud) for
team collaboration.
Terraform commands:
Command Description
www.linkedin.com/in/ajinkya-pame-4a752b346
Command Description
www.linkedin.com/in/ajinkya-pame-4a752b346
export AWS_SESSION_TOKEN="your-session-token"
# only if using temporary credentials
D. Assume Role
If you want to assume a role (e.g., in cross-account scenarios):
provider "aws" {
region = "us-east-1"
assume_role {
www.linkedin.com/in/ajinkya-pame-4a752b346
role_arn = "arn:aws:iam::123456789012:role/myrole"
}
}
www.linkedin.com/in/ajinkya-pame-4a752b346
resource "aws_instance" "my_ec2" {
ami = var.ami
instance_type = var.instance_type
key_name = aws_key_pair.terraform_key.key_name
tags = {
Name = "TerraformEC2"
}
}
➢ Save the private key to a local file (so you can SSH into the
instance)
resource "local_file" "private_key_pem" {
content =
tls_private_key.generated_key.private_key_pem
filename = "terraform-key.pem"
file_permission = "0400"
www.linkedin.com/in/ajinkya-pame-4a752b346
}
variable "ami" {
description = "AMI ID for the EC2 instance"
type = string
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
output "instance_id" {
description = "ID of the created EC2 instance"
value = aws_instance.my_ec2.id
}
2. Variable:
➢ In variable block we mention variable block with word
“variable” and name of the variable in quotes.
➢ The details of variable are written in “{}” and the
attributes declared are type of variable such as string, int,
etc, description of the variable and if needed then default
value too.
➢ If any value is not mentioned for the variable in tfvars file
then this default value is used.
3. tfvars:
➢ As mentioned earlier the values of type mentioned in
variable are declared here. For example, we have put both
ami ID and instance type in string format.
www.linkedin.com/in/ajinkya-pame-4a752b346
4. Output:
➢ In this block we are defining the output we want with the
name we want it to be shown.
➢ Value attribute parses the values using the default
resource block names and local names of them.
➢ Using this an output appears on the console once the
infrastructure is created.
www.linkedin.com/in/ajinkya-pame-4a752b346
3. Run terraform fmt to format the documents in correct
format (not needed every time).
4. Run terraform validate to check for validation of our
configuration files if they are correctly configured then
“Success” message will appear.
www.linkedin.com/in/ajinkya-pame-4a752b346
➢ As can be seen the creation plan is shown again, it shows
“4 to add” then prompts to enter “yes” and at the end
“Apply complete” with “4 resources added”.
➢ The output is also visible as “instance id” and “Its public
IP”.
➢ The resources like key-pair and instance can be seen on
AWS console.
www.linkedin.com/in/ajinkya-pame-4a752b346