Thursday, April 18, 2019

How to create EC2 instances using Terraform in Windows | Terraform Files for provisioning EC2 instance on AWS Cloud

Terraform can provision resources on any cloud platform. We will see how we can use Terraform to provision EC2 instance using Windows laptop. Please do the below steps for provisioning EC2 instances on AWS:

Login to AWS console, click on username and go to My security credentials.
 Continue on security credentials, click on access keys

Create a new access key if you don't have one. Make sure you download the keys in your local machine.


Perform below commands in Windows laptop where you have installed Terraform:

First setup your access keys, secret keys and region code locally.
aws configure

Open Git bash and enter below commands:

cd c:

mkdir project-terraform
cd project-terraform

Create Terraform Files
start notepad

variable "aws_region" {
  description = "The AWS region to create things in."
  default     = "us-east-2"

variable "key_name" {
  description = " SSH keys to connect to ec2 instance"
  default     =  "your_pem_keyname"

variable "instance_type" {
  description = "instance type for ec2"
  default     =  "t2.micro"

save above file as "" under c:\project-terraform folder

start notepad

provider "aws" {
  region = var.aws_region

#Create security group with firewall rules
resource "aws_security_group" "security_jenkins_port" {
  name        = "security_jenkins_port"
  description = "security group for jenkins"

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = [""]

 ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = [""]

 # outbound from jenkis server
  egress {
    from_port   = 0
    to_port     = 65535
    protocol    = "tcp"
    cidr_blocks = [""]

  tags= {
    Name = "security_jenkins_port"

resource "aws_instance" "myFirstInstance" {
  ami           = "ami-0b9064170e32bde34"
  key_name = var.key_name
  instance_type = var.instance_type
  security_groups= [ "security_jenkins_port"]
  tags= {
    Name = "jenkins_instance"

# Create Elastic IP address
resource "aws_eip" "myFirstInstance" {
  vpc      = true
  instance =
tags= {
    Name = "jenkins_elstic_ip"

save above file as "" under c:\project-terraform folder

Now execute the below command:
terraform init
* version = "~> 1.22"
Terraform has been successfully initialized!

and then execute the below command
terraform plan
the above command will show how many resources will be added.
Plan: 3 to add, 0 to change, 0 to destroy.

Execute the below command
terraform apply
Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
List Resources created by Terraform
Execute the below command to view list of the resources created by Terraform.
terraform state list
The above command will list three resources created.

Now login to AWS EC2 console, you would see the new instances up and running.

How to push Terraform files into GitHub
First create SSH keys and upload public keys into GitHub by executing below command:
This should generate both public and private keys. Now navigate to c:\users\your_username\.ssh folder and open key) file probably in notepad and copy the whole content of it and upload into GitHub. 

Create Remote repo in GitHub
Create a new repo with below name, make sure it is a private repo. Also do not click on initialize this repository with a README option.

 Note down the remote url as highlighted below:

All Terraform files should be checked into version control systems such as GitHub, BitBucket or GitLab. Let us see how to push code changes into GitHub. Make sure you are in the directory where Terraform files are created.

Initialize the directory first
Once you uploaded make sure you are in the directory where you had created tf files.
git init

The above command will create local git repository.
Now add terraform files.
git add *.tf

You need to setup your email address and user name.
git config --global ""

git config --global "your_firstname last_name"
git commit -m "added terraform files"
Copy the below green highligted url from above screenshots circled in red.
git remote add origin

Now push the code into GitHub
git push -u origin master

Now Login to GitHub to view the Terraform files

If you have any issues in uploading tf files, you may not have created ssh-keys and uploaded into GitHub. Create ssh keys using ssh-keygen command:
This should generate both public and private keys.
navigate to c:\users\your_username\.ssh folder and open key) file and copy the content of it and upload into GitHub. 


How to setup monitoring on Kubernetes Cluster using Prometheus and Grafana | Setup monitoring on EKS Cluster using Prometheus and Grafana

What is Prometheus? Prometheus is an open source monitoring tool Provides out-of-the-box monitoring capabilities for the Kubernetes containe...