0% found this document useful (0 votes)
1 views

terraform_studentnotes_FinalCopy

The document, compiled by Mr. Shaan Shaik, provides an overview of AWS and DevOps, focusing on the use of Terraform as an infrastructure-as-code tool compared to others like Chef and Ansible. It discusses the differences between configuration management and orchestration, mutable vs immutable infrastructure, and procedural vs declarative approaches. Additionally, it includes installation instructions, working with variables, and code organization best practices.

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

terraform_studentnotes_FinalCopy

The document, compiled by Mr. Shaan Shaik, provides an overview of AWS and DevOps, focusing on the use of Terraform as an infrastructure-as-code tool compared to others like Chef and Ansible. It discusses the differences between configuration management and orchestration, mutable vs immutable infrastructure, and procedural vs declarative approaches. Additionally, it includes installation instructions, working with variables, and code organization best practices.

Uploaded by

sai Charan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

DVS Technologies Aws & Devops

Compiled and Scrutinized by

Mr. Shaan Shaik


(Senior DevOps Lead)

Words To The Students

Though we have taken utmost efforts to present you this book


error free, but still it may contain some errors or mistakes. Students are
encouraged to bring, if there are any mistakes or errors in this
document to our notice. So that it may be rectified in the next edition of
this document.

“Suppressing your doubts is Hindering your growth”.

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]

1. Infrastructure as a Code (IaC)

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.

But question is simple why ?

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 ??

Configuration Management vs Orchestration ::

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.

Mutable Infrastructure vs Immutable 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
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.

Immutable --> Orchestration tools

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.

using Ansible : (Procedural approach)

- 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)

resource "aws_instance" "example" {


count = 10
ami = "ami-v1"
instance_type = "t2.micro"
}

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.

Using Ansible you need to specify the code like below.

- 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.

Using Terraform you need to specify the code like below.

resource "aws_instance" "exampe" {


count = 15
ami = "ami-v1"
instance_type = "t2.micro"
}

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).

Client/Server Architecture vs Client-Only Architecture ::

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

2. Installation and Configuration

Create one 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

Configure your Aws Cli as specified below:

Let's Install the terraform:

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

3. Working with terraform

Let's Create a key pair as specified below.

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:

Terraform state file:

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

2. Dynamic data for the variables:

Simply remove the default value section 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

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:

3. Passing variables from Commandline:

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:

4. Variables via files:

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

Example file for your variables:

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

Breaking them in to individual sections:

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 post code alignment

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

6. Maps & lookups

${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

Make sure that you are creating the keypairs:

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:

Note: Only one server will be up at a time because of tf state file

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

7. Working with Workspace


Now let's do the below to overcome our issue with maps & lookups

Listing workspace :

Creating Workspace:

List & Checking the current 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

Working with Dev 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

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

Now let’s verify our prod tfstate file:

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.

Switching between workspaces:

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 the environments:

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

8. working with Modules


Let's create individual elements i.e, S3,VPC,EC2

Creation of S3:

[root@terraform ~]# mkdir moduels


[root@terraform ~]# cd moduels/
[root@terraform moduels]# mkdir s3
[root@terraform moduels]# cd s3/
[root@terraform s3]# pwd
/root/moduels/s3
[root@terraform s3]# ls -l
total 0
[root@terraform s3]# vi main.tf
[root@terraform s3]# cat main.tf
provider "aws" {
region = "us-east-1"
}

resource "random_id" "myrandid" {


byte_length = 2
}

/*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
}

resource "aws_s3_bucket" "mys3bucket" {


bucket = "${var.bucket_name}-${random_id.myrandid.dec}"
tags = {
Name = "${var.bucket_name}-${random_id.myrandid.dec}"

}
}

Before applying below:

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

After Alignment my code look 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

Total three file main.tf, outputs.tf, myvars.tfvars

Let's work with our Ec2:

[root@terraform ec2]# touch {variables.tf,main.tf,myvars.tfvars}

[root@terraform ec2]# vi myvars.tfvars

[root@terraform ec2]# cat myvars.tfvars

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}"
}

resource "aws_instance" "myec2" {


ami = "${var.ec2_ami}"
instance_type = "${var.ec2_type}"
key_name = "${var.ec2_keypair}"
tags = {
Name = "${var.ec2_name}"
}
}
[root@terraform ec2]#

Before:

Apply the changes 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

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

Finally let’s work on VPC creation:

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

Let's start working with our code

Code:

[root@terraform vpc]# cat variables.tf


/*variable*/

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" {}

[root@terraform vpc]# cat myvars.tfvars


/*myvars.tfvars*/
region="us-east-1"
vpc_name="testvpc"
vpc_cidr="10.60.0.0/16"
vpc_igw_name="testigw"
vpc_route_name="testroute"
vpc_sub_cidr="10.60.10.0/24"
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_sub_name="testsubnet"
vpc_secgrp_name="testsecgroup"
[root@terraform vpc]#

[root@terraform vpc]# cat main.tf


/*main.tf*/

provider "aws" {
region = "${var.region}"
}

resource "aws_vpc" "myvpc" {


cidr_block = "${var.vpc_cidr}"
tags = {
Name = "${var.vpc_name}"
}
}

resource "aws_internet_gateway" "myigw" {


vpc_id = "${aws_vpc.myvpc.id}"

tags = {
Name = "${var.vpc_igw_name}"
}
}

resource "aws_route_table" "myroute" {


vpc_id = "${aws_vpc.myvpc.id}"

route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.myigw.id}"
}

tags = {
Name = "${var.vpc_route_name}"
}
}

resource "aws_subnet" "mysubnet" {


vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "${var.vpc_sub_cidr}"

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}"
}
}

resource "aws_route_table_association" "myroute_association" {


subnet_id = "${aws_subnet.mysubnet.id}"
route_table_id = "${aws_route_table.myroute.id}"
}

resource "aws_security_group" "mysecgroup" {


name = "${var.vpc_secgrp_name}"
description = "Allow TLS inbound traffic"
vpc_id = "${aws_vpc.myvpc.id}"

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:

Let's create Ec2 using the module

#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

Let's append our vpc:

Variabels to pass for vpc module:

[root@terraform user1]# cat /root/moduels/vpc/myvars.tfvars


/*myvars.tfvars*/
region="us-east-1"
vpc_name="testvpc"
vpc_cidr="10.60.0.0/16"
vpc_igw_name="testigw"
vpc_route_name="testroute"
vpc_sub_cidr="10.60.10.0/24"
vpc_sub_name="testsubnet"
vpc_secgrp_name="testsecgroup"

Ec2 & VPC module as part of main.tf file:

[root@terraform user1]# cat main.tf


module "myserver" {

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:

9. Profiles - Accessing Multiple Regions


Please do the below for different profiles

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

Let's add a new profile i.e us-east-2

Let's test the profile in different regions:

Us-east-1 & us-east-2

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

Testing via CLI:

Testing via 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

You might also like