Figure :Architecture for vpc with Terraform

The Crispy Definition of VPC is Its a Isolated network.vpc has Several Components like NatGateway, InternetGateway, Routers, Elastic IP and Route tables ,Security Group and NACL.Lets see how to Create a vpc and configure its components.

10.0.0.0: Network address.

10.0.0.1: Reserved by AWS for the VPC router.

10.0.0.2: Reserved by AWS DNS Server

10.0.0.3: Reserved by AWS for future use.

10.0.0.255 Network broadcast address. AWS do not support broadcast in a VPC, therefore its reserve this address.

Source code for VPC


In this article we will Demonstrate How to build a CICD pipeline with Git, Jenkins and Terraform.

step1

Lets implement the below Architecture for CICD builds using jenkins.In our case we are configuring Git web hooks for implementing continuous Deployments. Whenever there is change in code to GitHub repository build will happens and a slack notification will be triggered to slack channel.

Figure 0 :CICD setup for Jenkins with Terraform

This script supports ubuntu machine .So Launch a aws EC2 instance with ubuntu machine and make sure port 8080 opened there and then Login to the server using Moboxterm or putty.

once logged in Lets Install Jenkins on Docker…


VPC:

The Crispy Definition of VPC is Its a Isolated network.vpc has Several Components like NatGateway, InternetGateway, Routers, Elastic IP and Route tables ,Security Group and NACL.Lets see how to Create a vpc and configure its components.

Private IPv4 addresses
RFC1918 name IP address range Classful description
24-bit block 10.0.0.0–10.255.255.255 single class A network
20-bit block 172.16.0.0–172.31.255.255 16 contiguous class B networks
16-bit block 192.168.0.0–192.168.255.255
256 contiguous class C networks

IP addresses reserved by vpc
there are 5 IP addresses reserved by vpc

10.0.0.0: Network address.

10.0.0.1: Reserved by AWS for the VPC router.

10.0.0.2: Reserved by AWS DNS Server

10.0.0.3: Reserved by AWS for future use.

10.0.0.255: Network broadcast address. AWS do…


Migrating Terrafrom State files from one cloud to other cloud

create the aws user and get credentials,and get create a service account in gcp and get credentails .

provider “aws” {
region = “us-east-1”
access_key = “”
secret_key = “”
}
provider “google” {
project = “kethan-3915”
region = “europe-west-2”
credentials = file(“./key.json”)
}

Create s3 and Gcs buckets

resource “aws_s3_bucket” “hello” {

bucket = “kenade77777”

acl = “private”

tags = {

Name = “My bucket”

Environment = “Dev”

}

}

resource “google_storage_bucket” “mybucket” {

name = “kenade999999”

location = “asia”

storage_class = “COLDLINE”

}

create a folder and intilize it as project folder

Do terraform init

when we do terraform init

the statefile will be present in terrform.tfstate

Terraform.tfstate on Local Machine

Lets move the state file to remote locations

we can see in the…


Local and dynamic blocks:

Local blocks:

If we need to use common values in repeated blocks then we go for local blocks.all the values can be declared in a block called locals{}.

To use these values in source code we can call them with local.tagname

In our example we declared common tags as shown below

locals {
common_tags = {
users = "devteam"
}
}

Example usage:

provider "aws" {
region = "us-east-1"
access_key = ""
}
locals {
common_tags = {
users = "devteam"
}
}
resource aws_instance "myec2" {
ami = "ami-96fda3c22c1c990a"
instance_type = "t2.small"
tags = local.common_tags
}…

This Article Demonstrates How to connect a local Machine where we Installed Terraform and we connect it to Terraform cloud where resources will be Created at Terraform Cloud , and Logs will be Streamed at Local Machine.

purpose:

logs of terraform plan and terrafrom apply can be streamed to local and output can be vieed at cloud

procedure:

Login to app.Terraform.io and Create an organization

  • create a workspace
  • configure variables

create a file myfile.tf on our local machine

terraform {  required_version = "~> 0.12"  backend "remote" {    organization = "rohit777"
workspaces { name = "myworkspace777" } }}resource aws_instance…


Terraform Provisioners

A Provisioners can be used to model specific actions on the local machine or on a remote machine in order to prepare some tasks.

For example if you are creating a server and after that some software need to installed on top it for example in our case we are installing Httpd server. Terraform wont recommend to use provisioners .

“what if the provisioners Fails in this case “If a creation-time provisioner fails, the resource is marked as tainted. A tainted resource will be planned for destruction and recreation upon the next terraform apply .


Managing Terraform States in Remote Loactions:

Terraform State:

Just like Human has Heart , Terraform has States.Terraform knows if a Resource is created or destroyed or modified from its sate file. without statefile terraform won’t work.

Sample Resource Code:

resource "aws_instance" "myec23" {
ami = "ami-08f63db601b82ff5f"
instance_type = "t2.large"
tags = {
name = "myec3"
}
}
{
"version": 4,
"terraform_version": "0.12.26",
"serial": 9,
"lineage": "a507cf62-a1a4-e2a3-71b7-a0d645df67d0",
"outputs": {},
"resources":

The above image shows the basic structure of Terraform.it has outputs and resources.
1. Outputs states the resource attributes
2. Resources States about Resource Created,Destroyed,or modified.
3. …


Alias and Data sources in Terraform

Alias:

What if I need to launch my resources in multiple regions within a single configuration file then Alias variable does this functionality.

In the below example we have declared

alias = “pillows” in one provider block.

we can call this variable to refer to a resource other than a default region with the following syntax

provider = providername. aliasvariablename

In our case alias variable is “pillows” and provider is aws so therefore we can write it as provider =pillows.aws

Provider”aws” {

region = “us-east-1”

access_key = “

}

provider “aws” {

region =…


Alias and Data sources in Terraform

Alias:

What if I need to launch my resources in multiple regions within a single configuration file then Alias variable does this functionality.

In the below example we have declared

alias = “pillows” in one provider block.

we can call this variable to refer to a resource other than a default region with the following syntax

provider = providername. aliasvariablename

In our case alias variable is “pillows” and provider is aws so therefore we can write it as provider =pillows.aws

Provider”aws” {

region = “us-east-1”

access_key = “

}

provider “aws” {

region =…

jaffar shaik

visit me at www.theterraform.in , Expertise in Aws, terraform, Docker, kubernetes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store