terraform_studentnotes_FinalCopy
terraform_studentnotes_FinalCopy
We urge you to work hard and make use of the facilities we are
providing to you, because there is no substitute for hard work. We wish
you all the best for your future.
“The grass isn’t greener on the other side; the grass is greener
where you water it.”
You and your suggestions are valuable to us; Help us to serve you
better. In case of any suggestions, grievance, or complaints, please feel
free to write us your suggestions, grievance and feedback on the
following
[email protected]
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Terraform Introduction ::
Why we use Terraform and not Chef, Puppet, Ansible, SaltStack, or CloudFormation
If you search the Internet for “infrastructure-as-code”, it’s pretty easy to come up with a
list of the most popular tools:
Chef
Puppet
Ansible
SaltStack
CloudFormation
Terraform
All the above tools helps us to manage our infrastructure in the form of code.
should we choose "terraform" than all these. If you observe all the above tools are open
source and they
have their own communities and the contribution & one more thing is they all are
enterprise tools.
Even we have "cloud formation" for automating the things with AWS than terraform. but
question remains same why terraform ??
The above mentioned tools except "CloudFormation & Terraform" all other tools are
basically configuration
management tools.
Which means that they are used to manage and install the s/w or helps to maintain a state
of the particular
machine.
But "Terraform" & "CloudFormation" are the Orchestration tools which means that they
are designed to provison the machines & their infrastructure. Once the machine is builded
you can use the configuration management tools for performing your task.
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Mutable --> Configuration management tools
using configuration management tools, we can deploy the new software versions based up
on the environment we are choosing. But if you observe each server will be having a
separate version based up on the environment.
But if you are choosing the Orchestration tools you can simply maintain all the servers with
a single version
of OS. It's simple create a simple OS image and start deploy the servers as per the
requirement and all your
old machines will be replaced with the newly builded machines and all the machines will
have same version of
the package installed !!
Procedural vs Declarative ::
Procedural approach means if you want to achieve something you need to mention the
things in an programmatic approach. "Chef & Ansible" works on the same.
But in Declarative approach you no need to worry about flow it will automatically gets the
respective information based up the resource what we are choosing.
For example if you want to create 10 servers with app version v1 then the code for different
tools will be like below.
- ec2:
count: 10
ami: app-v1
instance_type: t2.micro
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Using terraform : (Declarative)
Till now its fine no much changes in both the configuration. But question is what will
happen if the load is high and if you want to add 5 more servers.
- ec2:
count: 15
ami: app-v1
instance_type: t2.micro
Soon after executing this code, you will get a 15 more servers along with 10 machines so
total will be 25 servers. But your desire state is to have only 5 machines without changing
the code. Which means that you need to again re-write the entire code and find the
previous machines and has to do all the other stuff.
Now what Terraform will do it, it won't create 15 more servers it will simply create 5
servers because it is well aware of the current state whatever it is having. Hence you no
need to break you head to write new code.
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Disadvantages:
Of course, there are downsides to declarative languages too. Without access to a full
programming language, your expressive power is limited. For example, some types of
infrastructure changes, such as a rolling, zero-downtime deployment, are hard to express
in purely declarative terms. Similarly, without the ability to do “logic”
(e.g. if-statements, loops), creating generic, reusable code can be tricky (especially in
CloudFormation).
Chef,puppet & salt stack are purely based on "Client/Server". Which indeed there are
many
hiccups when you are dealing with these tools, like
1. you need to install the client in all the machines in order to get the desired state as
per the requirement
2. you should require a manageable server which should give the instructions to the client
machines.
3. you will get all the issues with the network, client,management and etc.
Ansible, CloudFormation & Terraform are purely client-only Architecture, which in deed
you no need to install any agents as part of your machines in order to do the management.
CloudFormation is also client/server, but AWS handles all the server details so
transparently, that as an end user, you only have to think about the client code. The
Ansible client works by connecting directly to your servers over SSH
Terraform uses cloud provider APIs to provision infrastructure, so there are no new
authentication mechanisms beyond what you’re using with the cloud provider already, and
there is no need for direct access to your servers
Final Conclusion:
Of course, Terraform isn’t perfect. It’s younger and less mature than all the other tools on
the list: whereas Puppet came out in 2005, Chef in 2009, SaltStack and CloudFormation in
2011, and Ansible in 2012,
Terraform came out just 4 years ago, in 2014.
Bugs are relatively common (e.g. there are over 800 open issues with the label “bug”),
although the vast majority are harmless eventual consistency issues that go away when you
Re-run Terraform
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
sudo curl -O
https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
sudo yum install -y unzip
sudo unzip terraform_0.11.13_linux_amd64.zip -d /usr/local/bin/
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Init:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Plan:
Apply:
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
After:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Destroy:
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
After:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
4. Working with variables
Let's create a server with static variables & later convert them to variables
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
1. Default variables:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
After:
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
After:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Output Section:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Other example:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
5. Segregating my code
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
${lookup(var.my_server_name,var.env)}"
${lookup(var.my_key_name,var.env)}"
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Dev-key
Prod-key
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Final testing:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prod Testing:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Listing workspace :
Creating Workspace:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
But if you check in your dev workspace you will find the new tf state file like below.
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Let's work on prod workspace:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Now if you observe we can have both the environments from the same code only change we
applied is just adding new workspace for the same directory.
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
null_resource:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Creation of S3:
/*variable*/
variable "bucket_name" {
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
}
}
}
terraform init .
terraform apply -auto-approve -var bucket_name="mytestbkt"
Post changes:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
ec2_ami="ami-0c94855ba95c71c99"
ec2_keypair="shan_harish_nvirginia"
ec2_type="t2.micro"
region="us-east-1"
ec2_name="mytestserver"
[root@terraform ec2]# vi variables.tf
[root@terraform ec2]# cat variables.tf
variable "ec2_name" {}
variable "ec2_keypair" {}
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
variable "ec2_ami" {}
variable "ec2_type" {}
variable "region" {}
[root@terraform ec2]# vi main.tf
[root@terraform ec2]# cat main.tf
provider "aws" {
region = "${var.region}"
}
Before:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
After Changes:
Destroy infrastructure:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Before code execution I can see that only one vpc is existing
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Code:
variable "region" {}
variable "vpc_cidr" {}
variable "vpc_name" {}
variable "vpc_igw_name" {}
variable "vpc_route_name" {}
variable "vpc_sub_name" {}
variable "vpc_sub_cidr" {}
variable "vpc_secgrp_name" {}
provider "aws" {
region = "${var.region}"
}
tags = {
Name = "${var.vpc_igw_name}"
}
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.myigw.id}"
}
tags = {
Name = "${var.vpc_route_name}"
}
}
tags = {
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Name = "${var.vpc_sub_name}"
}
}
ingress {
description = "mysecgroup"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.vpc_secgrp_name}"
}
}
Execution:
terraform init .
terraform apply -auto-approve -var-file=myvars.tfvars
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Code reusability:
#mkdir user1
#cd user1
[root@terraform user1]# pwd
/root/user1
[root@terraform user1]# cat main.tf
module "myserver" {
source = "/root/moduels/ec2/"
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
ec2_ami = "ami-0c94855ba95c71c99"
ec2_keypair = "dev-key"
ec2_type = "t2.micro"
region = "us-east-1"
ec2_name = "dvsserver1"
terraform init .
terraform apply -auto-approve
source = "/root/moduels/ec2/"
ec2_ami = "ami-0c94855ba95c71c99"
ec2_keypair = "dev-key"
ec2_type = "t2.micro"
region = "us-east-1"
ec2_name = "dvsserver1"
module "network" {
source = "/root/moduels/vpc/"
region="us-east-1"
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
vpc_name="dvsvpc"
vpc_cidr="10.20.0.0/16"
vpc_igw_name="dvsigw"
vpc_route_name="dvsroute"
vpc_sub_cidr="10.20.10.0/24"
vpc_sub_name="dvssubnet"
vpc_secgrp_name="dvssecgroup"
Execution:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Final Verification:
Default Profile:
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in
DVS Technologies Aws & Devops
Prepared by Shaan, DVS Technologies, Opp Home Town, Beside Biryani Zone, Marathahalli, Bangalore
Phone: 9632558585 Mobile: 8892499499 Mail : [email protected] Web: www.dvstechnologies.in