terraform practise updates doc
terraform practise updates doc
Installation Terraform
DAY1
Install Terraform on your local system
C:\Users\Asus\Downloads\terraform_1.7.3_windows_386
Terraform Codes
DAY 2
Aws & devops by veera nareshit
Aws & devops by veera nareshit
cidr_block = "10.0.0.0/16"
tags = {
Name = "ankit_vpc"
vpc_id = aws_vpc.custnw.id
tags = {
vpc_id = aws_vpc.custnw.id
cidr_block = "10.0.0.0/24"
tags = {
vpc_id = aws_vpc.custnw.id
tags = {
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.custnw.id
route_table_id = aws_route_table.custnw.id
subnet_id = aws_subnet.custnw.id
name = "custnw_sg"
vpc_id = aws_vpc.custnw.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
egress {
from_port =0
to_port =0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ami = var.ami
instance_type = var.instance_type
key_name = var.key_name
subnet_id = aws_subnet.custnw.id
associate_public_ip_address = true
tags = {
Name = "CustANKITec2"
DAY 3
S3 BUCKET CREATION WITH VERSIONING
bucket = "terrabucketcreate"
bucket = aws_s3_bucket.devankit.id
versioning_configuration {
status = "Enabled"
#Create the fresh EC2 instance and print the output of public ip,
public dns and private ip dns
++Don't print output of privateip_by using sensative.
resource "aws_instance" "MrSingh" {
ami = var.ami
instance_type = var.instance_type
key_name = var.key_name
tags = {
Name = "MrSinghec2"
DAY 4
Backend.tf script
#we are creating one S3 Bucket and try to see the whole creation process inside
terraform.tfstate.
#terraform.tfstate can be vanished or it will not get seen into the local as above by using
configuring backend.tf block. Means after we do terraform apply terraform.tfstate will get
created and it will capture also the running process whatever any creation deletion any
ongoing process it will able to capture but it will not located into local as above it will get
located into backend.tf
DAY 5
IMPORT : import resource into terraform
To do any further changes in created ec2 instance we import or clone to our local
system and control the main.tf for further changes for ec2 instance.
First we create a resource block before that we will create a ec2 instance
Now we will map the ec instance id with our local ec2 resource block
terraform import aws_instance.importec2 i-0e5ffb92c68b388e7
Now we can give all ami instance_type key_name by the refrence of statefile
because state file recorded capture all details of that ec2 while importing to our local
We can refer the details from statefile and code on our main resource block
Now suppose I want to make further changes on it I will give another key pair
previous at first before import it was redhat in statefile means first ec2 before import
have redhat key_name
DAY 6
DATA SOURCE
Here we can use custom network where we already have vpc created and inside vpc
my subnet , my internet g/w RT everything configured and that all vpc configuration
attached to our placed ec2 public instance inside public subnet.
But here we can create any instance at any time and we can call same cust netwoek
configuration where we already have our vpc details and all.
So we already have the custom network configured we can copy the whole
configuration and paste to our new folder.
#create vpc
resource "aws_vpc" "custnw" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "ankit_vpc"
}
}
#create Internet Gateway and attach to VPC
resource "aws_internet_gateway" "custnw" {
vpc_id = aws_vpc.custnw.id
tags = {
Name = "Ankit Internet Gateway"
}
}
#create subnet attach to vpc
ingress {
description = "TLS from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TLS from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Now we can create fresh ec2 instance and inside it only we call vpc which we
already taken from old and pasted to our new dir.
Go to vpc
Subnet ID (from here copying subnet id and pasting inside data source block)
Like above we passed the value of subnet into fresh ec2 via creating data source.
Same we can pass Security Group as well
Go to vpc
Security Group ID (from here copying SG id and pasting inside data source block)