0% found this document useful (0 votes)
2 views7 pages

Lab2_DevOps (1)

Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that automates the provisioning and management of cloud infrastructure using configuration files. It allows for automated setup, repeatability, and scalability of resources across various cloud platforms through providers. Key commands include initializing the project, planning changes, applying configurations, and destroying resources, all of which are essential for managing infrastructure efficiently.

Uploaded by

Sarosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Lab2_DevOps (1)

Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that automates the provisioning and management of cloud infrastructure using configuration files. It allows for automated setup, repeatability, and scalability of resources across various cloud platforms through providers. Key commands include initializing the project, planning changes, applying configurations, and destroying resources, all of which are essential for managing infrastructure efficiently.

Uploaded by

Sarosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

What is Terraform and Why It’s Important

Definition

●​ Terraform is an open-source tool created by HashiCorp. It's used for Infrastructure as


Code (IaC), which means you can define your infrastructure (like virtual machines,
storage, networks) using configuration files written in code (HCL - HashiCorp
Configuration Language).​

●​ It's different from traditional manual setup (e.g., using a console or GUI) because with
Terraform, your infrastructure is automatically created, managed, and versioned based
on the code.

Purpose

●​ Provisioning: You can use Terraform to automatically set up and configure your cloud
infrastructure.

Benefits

●​ Automated Setup: No manual configuration. You describe the desired state of the
infrastructure, and Terraform handles creating and configuring the resources.​

●​ Repeatability: You can easily recreate infrastructure using the same configuration file.
This is useful for environments like development, testing, or production.​

●​ Scalability: It's easy to scale resources up or down. For example, you can change the
number of virtual machines with minimal effort.

2. Key Concepts in Terraform

Providers

●​ A provider is a plugin that allows Terraform to interact with different cloud platforms or
services. For example:​

○​ AWS Provider: Used for managing AWS resources like EC2 instances, S3
buckets, etc.​

○​ Azure Provider: Used for managing Microsoft Azure resources.​

○​ GCP Provider: Used for managing Google Cloud resources.​


●​ Terraform uses these providers to know how to create, manage, and destroy cloud
resources.​

Resources

●​ Resources are the building blocks of your infrastructure in Terraform. They represent
actual cloud services or components you manage, such as:​

○​ aws_instance: Represents an EC2 instance.​

○​ aws_s3_bucket: Represents an S3 bucket.​

○​ aws_vpc: Represents a Virtual Private Cloud.

State

●​ State: Terraform maintains a state file (terraform.tfstate) that tracks the actual
infrastructure and its current configuration. This is crucial for:​

○​ Determining what changes need to be made during the next terraform


apply.​

○​ Keeping track of what resources have been created, modified, or destroyed.​

●​ Terraform uses this state to compare the current infrastructure with the desired
configuration (the code), ensuring that the right changes are applied.

3. Basic Terraform Commands

Here are the core commands you'll use with Terraform:

●​ terraform init: Initializes your working directory. This command installs the provider
plugins and prepares your directory to work with Terraform.​

○​ You run it once when you start working on a new project or when you add or
change providers.​

●​ terraform plan: Shows you the changes Terraform will make to your infrastructure
based on your configuration. It’s a preview and doesn't make any actual changes yet.​

○​ This allows you to review what Terraform intends to do before you apply it (e.g.,
create new resources, modify existing ones).​
●​ terraform apply: This command applies the changes you reviewed in the
terraform plan step. It provisions the resources as described in your configuration.​

○​ After running terraform apply, Terraform will ask you to confirm (usually by
typing "yes") before proceeding.​

●​ terraform destroy: This command destroys (removes) all the resources created by
Terraform.​

○​ It’s useful for cleaning up and ensuring you’re not charged for unused resources.​

In order to do this lab, we first create an IAM user and make our access keys and download
aws CLI. then we configure our aws cli by just entering our keys that we got while making IAM
user.

2.1 Create a Project Directory

1.​ Open your terminal or command prompt.​

Create a new directory for your Terraform project:​



mkdir terraform-ec2-demo
cd terraform-ec2-demo

2.​

✅ 2.2 Create a File Named main.tf


This file will contain your Terraform code
📄 main.tf - Explained Step by Step
# Specify the AWS provider and region
provider "aws" {
region = "us-east-1"
}

🔹 What this does:


●​ provider "aws" tells Terraform that you want to use Amazon Web Services (AWS)
to create resources.​

●​ region = "us-east-1" sets the AWS region where resources like EC2 instances
will be created.​
For example: us-east-1 refers to Northern Virginia (USA).​

# Create an EC2 instance in the specified VPC, subnet, and security


group
resource "aws_instance" "example" {
🔹 What this does:
●​ This line defines a new EC2 instance (a virtual machine on AWS).​

●​ "aws_instance" is the resource type (i.e., EC2 instance).​

●​ "example" is the name you give this resource — you can reference this later using
aws_instance.example.​

ami = "ami-07d9b9ddc6cd8dd30" # Ubuntu 20.04 AMI for us-east-1

🔹 What this does:


●​ ami stands for Amazon Machine Image — a template for the OS and software installed
on your instance.​

●​ This AMI ID is for Ubuntu 20.04 LTS in the us-east-1 region.​

●​ It’s like saying “create an EC2 machine with Ubuntu 20.04”.​

instance_type = "t2.micro" # Free-tier eligible instance type

🔹 What this does:


●​ Specifies the size and power of the EC2 instance.​

●​ t2.micro is a small, lightweight instance, and it’s free-tier eligible, meaning it’s free
if your account is within AWS free usage limits.​

subnet_id = "subnet-06e33107f26f2fd57" # Your provided subnet ID

🔹 What this does:


●​ Specifies which subnet in your VPC (Virtual Private Cloud) the instance will be launched
into.​

●​ Subnet = a small network within your VPC, and this ID is the one you copied from your
AWS console.​

hcl
CopyEdit
vpc_security_group_ids = ["sg-0ce3d42592fea8dce"] # Your provided SG
ID

🔹 What this does:


●​ Attaches security groups to your EC2 instance.​

●​ A security group acts like a firewall — it controls which network traffic is allowed to
or from the instance.​

●​ You provide the ID of an existing security group.​

h
CopyEdit
tags = {
Name = "TerraformExampleInstance"
}
}

🔹 What this does:


●​ Tags help identify and organize AWS resources.​

●​ This tag will label the EC2 instance in the AWS console with the name:
"TerraformExampleInstance".

5. Initialize Terraform
Once your main.tf configuration file is written, you need to initialize the Terraform project. This
downloads the necessary plugins (like the AWS provider).

Run this command:

terraform init

It sets up everything Terraform needs to run your configuration.

6. Preview the Changes (terraform plan)

Before applying the configuration, you should always preview the changes Terraform will make:

bash
CopyEdit
terraform plan

This command will show what Terraform will do (create new resources, modify existing ones,
etc.).

7. Apply the Configuration (terraform apply)

After reviewing what Terraform will do, you can apply the configuration to create the resources:

terraform apply

●​ Terraform will ask for confirmation (yes) before proceeding. After confirmation, it will
create the EC2 instance.

8. Verify the Instance

Once the terraform apply command is complete, go to your AWS Management Console
and verify if the EC2 instance is running:

●​ Check EC2 > Instances in the console. ( waha par ec2 bana hua hoga)​

You might also like