0% found this document useful (0 votes)
120 views12 pages

Terraform Ashok It

The document outlines a one-week Terraform class scheduled for January 29, 2025, covering topics such as Terraform setup, architecture, scripting with HCL, and resource management in cloud environments. It emphasizes the advantages of using Terraform for infrastructure as code, including its support for multiple cloud platforms and the use of modules for project organization. The course is free and includes soft copy notes and backup videos, with a focus on practical applications like creating EC2 instances and S3 buckets.

Uploaded by

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

Terraform Ashok It

The document outlines a one-week Terraform class scheduled for January 29, 2025, covering topics such as Terraform setup, architecture, scripting with HCL, and resource management in cloud environments. It emphasizes the advantages of using Terraform for infrastructure as code, including its support for multiple cloud platforms and the use of modules for project organization. The course is free and includes soft copy notes and backup videos, with a focus on practical applications like creating EC2 instances and S3 buckets.

Uploaded by

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

TERRAFORM CLASS 1

29-01-2025

==========
Terraform
==========

Duration : 1 week

Pre-Requisites : AWS Cloud Basics

Class Timings : 8 PM to 9:30 PM (IST)

Note: Soft copy notes + Backup videos

Course Fee : 0

===============
Course Content
===============

1) Introduction (what & why)

2) Terraform s/w setup (windows & linux)

3) Terraform Architecture

4) Terraform Scripts using HCL

5) Variables (input & output)

6) Terraform Modules

7) Project Environments

8) Terraform workspaces

9) Resource Taints & Replacement

10) Terraform Vault

=====================
IT infrastructure
====================

=> Resource required for project execution

1) Machines (Servers)
2) Databases
3) Security
4) Storage
5) Network
6) Monitoring

=> In olden companies used to maintain infrastructure on their own


(On-Prem infrastructure).
- lot of money investment
- scalability
- availability
- security
- backup

=> To overcome On-Prem infrastructure challenges companies are moving to Cloud


Infrastructure.

=> We have several cloud providers in the market

1) AWS
2) AZURE
3) GCP

==========================================
How to create infrastructure in the cloud
==========================================

=> We can create cloud infrastructure in 2 ways

1) Manually

2) Terraform

=> If we create cloud infrastructure manually then we have below challenges

1) Time taking process

2) Same work again and again

3) Mistakes

=> To overcome above problems now companies are using Terraform to software to
setup Cloud Infrastructure.

===========
Terraform
===========

=> Terraform software Developed by Hashicorp company

=> To create/provision infrastructure in cloud platform

=> IAC software (Infrastructure as code)

=> Terraform supports almost all cloud platforms available in the market..

=> Terraform using HCL language to create infrastructure in the cloud.

HCL = Hashicorp Configuration Language

=> Terraform is free software

=> We can install terraform in mulitple Operating Systems

Ex: Windows, Linux....

====================================
Terraform Installation in Windows
====================================

Step-1 : Download terraform for windows & extract zip file

Note: We can see terraform.exe file

Step-2 : Set path for terraform s/w in System environment variables

Step-3 : Verify terraform setup using cmd

$ terraform -v

Step-4 : Download and install VS CODE IDE to write terraform scripts

URL : https://code.visualstudio.com/download

=======================
Terraform Architecture
=======================

=> Terraform will use HCL script to provision infrastructure in cloud platforms.

=> We need to write HCL script and save it in .tf file

.tf => init => validate => plan => apply => state file => destroy

terraform init : Intialize script (download provider related plugins)

terraform fmt : Format scrit indentation (spaces adjustment) (optional)

terraform validate : Verify terraform script is valid or not

terraform plan : Create Execution plan for script (optional)

terraform apply : Create resources in the cloud based on script

Note: When we execute apply, it will generate terraform state file. It contains
resources track.

terraform destroy : It is used to delete the resources created with terraform


script.

### Terraform AWS Documentation :


https://registry.terraform.io/providers/hashicorp/aws/latest/docs

====================================
DAY-2 30-01-2025
=========================================

#### Git Hub Repo : https://github.com/ashokitschool/Terraform_Projects.git

===========
Terraform
===========

=> Developed by Hashicorp

=> To create/provision infrastructure in cloud platform

=> IAC software (infrastructure as code)

=> Supports all most all cloud platforms

=> Terraform will use HCL language to provision infrastructure

HCL : Hashicorp configuration language

=> We can install terraform in mulitple Operating Systems

Ex: Windows, Linux....

==============================
Terraform Vs Cloud Formation
==============================

=> Cloud Formation is used to create infrastructure only in aws cloud

=> Terraform supports all cloud platforms available in the market.

====================================
Terraform Installation in Windows
====================================

Step-1 : Download terraform for windows & extract zip file

Note: We can see terraform.exe file

Step-2 : Set path for terraform s/w in System environment variables

Step-3 : Verify terraform setup using cmd

$ terraform -v

Step-4 : Download and install VS CODE IDE to write terraform scripts

URL : https://code.visualstudio.com/download

=========================
Terraform Architecture
=========================

=> Terraform will use HCL script to provision infrastructure in cloud platforms.

=> We need to write HCL script and save it in .tf file

.tf => init => fmt => validate => plan => apply => destroy

=> Below are the terraform commands


terraform init : Initialize terraform script (.tf file)

terraform fmt : Format terraform script indent spacing (optinal)

terraform validate : Verify terraform script syntax is valid or not (optional)

terraform plan : Create execution plan for terraform script

terraform apply : Create actual resources in cloud based on plan

Note: tfstate file will be created to track the resources created with our script.

terraform destroy : It is used to delete the resources created with our script.

### Terraform AWS Documentation :


https://registry.terraform.io/providers/hashicorp/aws/latest/docs

==========================================
Terraform Script To create EC2 Instance
==========================================

provider "aws" {

region = "ap-south-1"
access_key = "AKIATCKAMNKD6R2YIMPM"
secret_key = "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"
}

resource "aws_instance" "ashokit_linux_vm" {

ami = "ami-0e53db6fd757e38c7"
instance_type = "t2.micro"
key_name = "awslab"
security_groups = ["default"]
tags = {
Name = "LinuxVM"
}
}
---------------------------------------

$ terraform init

$ terraform validate

$ terraform fmt

$ terraform plan

$ terraform apply --auto-approve

$ terraform destory --auto-approve

=========================
Variables in Terraform
=========================
=> Variables are used to store data in key-value format

id = 101
name = ashok

=> We can remove hard coded values from resources script using variables

=> Variables we can maintain in seperate .tf file

Ex : input-vars.tf

variable "ami" {
description = "Amazon machine image id"
default = "ami-0e53db6fd757e38c7"
}

variable "instance_type" {
description = "Represens EC2 instance type"
default = "t2.micro"
}

variable "key_name" {
description = ""
default = "awslab"
}

=> We can access variables in our resources script like below

resource "aws_instance" "ashokit_ec2_vm" {

ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
security_groups = ["default"]
tags = {
Name = "AIT-Linux-VM
}
}

=================================
Types of variables in terraform
=================================

1) Input Variables

2) Output Variables

=> Input variables are used to supply values to the terraform script.

Ex : ami, instance_type, keyname, securitygrp

=> Output variables are used to get the values from terraform script after
execution.

Ex-1 : After EC2 VM created, print ec2-vm public ip

Ex-2 : After S3 bucket got created, print bucket info


Ex-3 : After RDS instance got created, print DB endpoint

Ex-4 : After IAM user got created print IAM user info

-------------------input-vars.tf------------------

variable "ami" {
description = "Amazon machine image id"
default = "ami-0e53db6fd757e38c7"
}

variable "instance_type" {
description = "Represens EC2 instance type"
default = "t2.micro"
}

variable "key_name" {
description = ""
default = "awslab"
}

--------------------main.tf-------------------

resource "aws_instance" "ashokit_ec2_vm" {


ami = var.ami
instance_type = var.instance_type
key_name = var.key_name
security_groups = ["default"]
tags = {
Name = "AIT-Linux-VM"
}
}

---------------output-vars.tf--------------------

output "ec2_vm_public_ip" {
value = aws_instance.ashokit_ec2_vm.public_ip
}

output "ec2_private_ip" {
value = aws_instance.ashokit_ec2_vm.private_ip
}

output "ec2_subnet_id"{
value = aws_instance.ashokit_ec2_vm.subnet_id
}

output "ec2_complete_info"{
value = aws_instance.ashokit_ec2_vm
}

========================================
31-01-2025
=============================================
====================
Creating S3 Bucket
===================
=> S3 is storage service in AWS cloud

=> S3 provides unlimited storage

resource "aws_s3_bucket" "mys3b" {


bucket = var.bucket_name
acl = "private"

versioning {
enabled = true
}
}

=======================================
What is taint and untaint in terraform
=======================================

=> Terraform "taint" is used to replace the resource when we apply the script next
time.

=> For example we have created two resources like below

resource "aws_instance" "vm1"{


// configuration
}

resource "aws_s3_bucket" "abt1"{


// configuration
}

=> After sometime we realized that ec2 vm got damaged...

Note : we can taint that ec2 vm using below command to replace when we apply the
script next time

$ terraform taint aws_instance.vm1

$ terraform apply --auto-approve

Note: The alternate for "taint" is "replace"

$ terraform apply -replace=aws_instance.vm1 --auto-approve

============
Assignment
===========

1) Create Custom VPC

2) Create Ec2 Instance using Custom VPC


FEBRUARY 3RD HELD IN STATE
=========================================
FEBRUARY 4-02-2025
=========================================

===================
Terraform Modules
===================

=> A Terraform module is a set of terraform configuration files available in a


single directory.

=> One module can contain one or more .tf files

01-Project
- provider.tf
- main.tf
- input-vars.tf
- output-vars.tf

=> One module can have any no.of child modules in terraform

irctc-app
- provider.tf
- main.tf
- outputs.tf
- ec2
- main.tf
- inputs.tf
- outputs.tf
- s3
- main.tf
- inputs.tf
- outputs.tf
- rds
- main.tf
- inputs.tf
- outputs.tf

Note : Using terraform modules we can achieve re-usability

Note: We will run terraform commands from root module and root module will invoke
child modules for execution.

======================================
Terraform project setup with Modules
======================================

### Step-1 : Create Project directory

Ex: 05-TF-Modules

### Step-2 : Create "modules" directory inside project directory

Ex: 05-TF-Modules
- modules

### Step-3 : Create "ec2" & "s3" directories inside "modules" directory
Ex: 05-TF-Modules
- modules
- ec2
- s3

### Step-4 : Create terraform scripts inside "ec2" directory

inputs.tf
main.tf
outputs.tf

### Step-4 : Create terraform scripts inside "S3" directory

inputs.tf
main.tf
outputs.tf

### Step-6 : create "provider.tf" file in root module

### Step-7 : create "main.tf" file in root module and invoke child modules from
root module.

module "my_ec2"{
source = "./modules/ec2"
}

module "my_s3" {
source = "./modules/s3"
}

### Step-8: Create "ouputs.tf" in project root module and access child modules
related outputs.

output "ec2_public_ip" {
value = module.my_ec2.a_public_ip
}

output "ec2_private_ip" {
value = module.my_ec2.b_private_ip
}

output "s3_bucket" {
value = module.my_s3.c_s3_info
}

======================================
FEBRURARY 5TH HELD STATE / FEBRURARY 6TH
=======================================
================================
Environments of the project
===============================

=> Env means the platform that is required to run our application

Ex: Servers, Database, Storage, Network....

=> One project contains multiple envs


Ex: DEV, SIT or QA, UAT, PILOT, PROD or LIVE

Dev Env : Developers will use it for code integration testing

SIT / QA Env : Testers will use it for System Integration Testing

UAT Env: Client will use it for Acceptance testing.

Pilot Env : Pre-Prod testing and Performance testing.

Prod Env : Live Environment.

Note: In real-time from environment to environment infrastructure resources


configuration might be different

DEV Env : t2.medium

PROD Env : t2.xlarge

=> In order to achieve this requirement we will maintain environment specific input
variable file like below

inputs-dev.tf => Input variables file for DEV env

inputs-sit.tf => input variables file for SIT env

inputs-uat.tf => input variables file for UAT env

inputs-pilot.tf => input variables file for PILOT env

inputs-prod.tf => input variables file for PROD env

=> When we are executing terraform apply command we can pass inputs variable file
like below.

# create infrastructure for DEV Env


$ terraform apply --var-file=inputs-dev.tf

# create infrastructure for PROD Env


$ terraform apply --var-file=inputs-prod.tf

Note: With this approach we can achieve loosely coupling and we can achieve script
re-usability.

=========================
Workspace in terraform
=========================

=> To manage infrastructure for multiple environments we will use Terraform


workspace concept.

=> When we use workspace, it will maintain seperate state file for every
environment/workspace.
Note: We can execute same script for multiple environments.

$ terraform workspace show

$ terraform workspace new <workspace-name>

$ terraform workspace list

$ terraform workspace select <workspace-name>

You might also like