Terraform with AWS
Tags: terraform
Published: 2022-08-06 02:34:54

Terraform with AWS
. Terraform is an orchestration tool, it is used to provision AWS resources through command line
. Terraform is agnostic which means it can be used to provision AWS/Azure/GCP Cloud resources
. Terraform uses the access key and secret key in order to provision the resources through CLI
. Cloudformation can be used only with AWS, as mentioned earlier Terraform can be used with        AWS/Azure/GCP as well.
. Terraform is easy to write declarative templates in HCL ( Hashi corp configuration Language)
. Terraform includes an array of modules, built in functions which can be used in Cloud and    Onprem as well
. Before stepping into Terraform we need to be familiar with AWS or Azure or GCP and Cloud CLI
.  We need to set up Terraform Binary in our OS to proceed with Terraform installation.
. The methods to install Terraform Binary for various OS is given in this link – Downloads | Terraform by HashiCorp
. In my case it is Ubuntu Server OS so I will follow the below steps to install Terraform Binary in my OS.
**
The below commands need to be executed as an Ubuntu/Linux user in home directory**
sudo apt-get update -y
sudo apt-get install wget unzip -y
sudo wget **https://releases.hashicorp.com/terraform/1.1.7/terraform_1.1.7_linux_amd64.zip**

sudo unzip terraform_1.1.7_linux_amd64.zip
.
sudo mv terraform /usr/local/bin/

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k81j6uug9gcs6hrqfr59.png)
 
 
We have successfully installed Terraform latest version is Ubuntu Server.

Next step is to install AWS – CLI
sudo apt-get install python3-pip	 ( this command installs the pip manager) – in Linux
sudo pip3 install awscli –user ( this command installs the aws CLI in your system) – in Linux

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

The above steps to be done in Ubuntu
To check the aws version … type aws –version 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4r4ih4z7r81lgku4qr6.png)
  

**We need to configure the access key and secret key of IAM administrator user in this System**
. type aws configure
Prior to the above step… create an IAM user with administrator privileges and download the credentials… Ie Access key and Secret key
When you type aws configure … it will ask for your access key and secret key
Copy paste the access key and secret key in that field 
Now create a empty directory in your system as below

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oy832oztxjedl61e3833.png)
 
 	
Let’s create a s3 bucket using terraform 
. 	aws s3api create-bucket --bucket yourbucketnamehere --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1
Let’s see how can we create a VPC , two public subnets and an internet gateway in AWS through terraform 

Inside the directory you created … create a file with .tf extension

shiva@hypo-cloudeva:~/aravind_tfproject$ sudo nano aravindterraformnetwork.tf
 
Type the below templates according to your requirement inside the file
#to create a vpc
resource "aws_vpc" "terraformshivavpc" {
  cidr_block       = "10.0.0.0/16"
  instance_tenancy = "default"

  tags = {
    Name = "terraformshivavpc"
  }
}



#to create a public subnet1

resource "aws_subnet" "public" {
  vpc_id     = aws_vpc.terraformshivavpc.id
  cidr_block = "10.0.2.0/24"
  availability_zone = "ap-south-1a"
}

#to create a public subnet2

resource "aws_subnet" "private" {
    vpc_id = aws_vpc.terraformshivavpc.id
    cidr_block = "10.0.3.0/24"
    availability_zone = "ap-south-1b"
}


# to create a internet gateway

resource "aws_internet_gateway" "terraformawsgateway" {
  vpc_id = aws_vpc.terraformshivavpc.id
}

# to create a route table for - IGW

resource "aws_route_table" "my_table" {
  vpc_id = aws_vpc.terraformshivavpc.id


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

Save the file and exit
Type terraform plan and it throws error if any in your template else it displays the resources to be provisioned… PFB

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djlvkcfustn1oruqaiyt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3yi4z1veoqmochy3g9n.png)
  
 

 

As the terraform plan shows the resources to be provisioned in AWS its good to 
use the command
. Terraform apply
 and provision the plan in AWS console 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrinlva6u4hc5zs0byz5.png)
 

 (The word document I prepared earlier is replicated here)