Lab2_DevOps (1)
Lab2_DevOps (1)
Definition
● 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.
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.
Resources
● Resources are the building blocks of your infrastructure in Terraform. They represent
actual cloud services or components you manage, such as:
State
● State: Terraform maintains a state file (terraform.tfstate) that tracks the actual
infrastructure and its current configuration. This is crucial for:
● Terraform uses this state to compare the current infrastructure with the desired
configuration (the code), ensuring that the right changes are applied.
● 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.
● 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).
● "example" is the name you give this resource — you can reference this later using
aws_instance.example.
● 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 = 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
● A security group acts like a firewall — it controls which network traffic is allowed to
or from the instance.
h
CopyEdit
tags = {
Name = "TerraformExampleInstance"
}
}
● 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).
terraform init
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.).
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.
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)