<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Anil Mitkari's blog]]></title><description><![CDATA[Anil Mitkari's blog]]></description><link>https://anilvishwanath3129.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 18 Jun 2026 05:07:35 GMT</lastBuildDate><atom:link href="https://anilvishwanath3129.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Configuring AWS Credentials in Jenkins for AWS Tasks]]></title><description><![CDATA[Part 1: Install Jenkins Plugins to configure AWS credentials
Access Jenkins Dashboard
--> Open your Jenkins dashboard.

Navigate to Plugin Installation
--> Click on "Manage Jenkins" in the dashboard.
Access System Configuration
--> Under "System Conf...]]></description><link>https://anilvishwanath3129.hashnode.dev/configuring-aws-credentials-in-jenkins-for-aws-tasks</link><guid isPermaLink="true">https://anilvishwanath3129.hashnode.dev/configuring-aws-credentials-in-jenkins-for-aws-tasks</guid><category><![CDATA[AWS]]></category><category><![CDATA[jenkins pipeline]]></category><category><![CDATA[aws cli]]></category><category><![CDATA[ Jenkins, DevOps]]></category><dc:creator><![CDATA[Anil Mitkari]]></dc:creator><pubDate>Wed, 06 Sep 2023 13:18:50 GMT</pubDate><content:encoded><![CDATA[<p>Part 1: Install Jenkins Plugins to configure AWS credentials</p>
<p>Access Jenkins Dashboard</p>
<p>--&gt; Open your Jenkins dashboard.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982053023/9e1602bb-19c0-4481-982f-f4041154ba6c.png" alt class="image--center mx-auto" /></p>
<p>Navigate to Plugin Installation</p>
<p>--&gt; Click on "Manage Jenkins" in the dashboard.</p>
<p>Access System Configuration</p>
<p>--&gt; Under "System Configuration," locate the "Plugins" section.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981192376/7ff230d1-af73-4ebe-889c-f2241d70b36b.png" alt class="image--center mx-auto" /></p>
<p>Search for AWS Credentials Plugin</p>
<p>--&gt;In the "Plugins" section, click on "Available Plugins." Use the search bar to find "AWS Credentials" among the available plugins.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981235993/d140cf8d-ae7a-4c70-b530-6b4c47efed47.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981321566/7763fd90-c0fa-45f3-813e-26a38a2aeb1b.png" alt class="image--center mx-auto" /></p>
<p>Install the AWS Credentials Plugin</p>
<p>--&gt; Once you locate the "AWS Credentials" plugin, click on "Install."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981361738/f10239c1-0d2e-4943-8513-204148795399.png" alt class="image--center mx-auto" /></p>
<p>Restart Jenkins</p>
<p>--&gt; After the installation is complete, restart Jenkins to apply the changes</p>
<p>Part 2: Configure AWS Access Key ID and Secret Access Key Using the Plugin</p>
<p>Access Jenkins Dashboard</p>
<p>--&gt; Open your Jenkins dashboard.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981082624/024cc4d2-f095-498c-842f-db49a8847d89.png" alt class="image--center mx-auto" /></p>
<p>Navigate to Credentials</p>
<p>--&gt; Under "Security," click on "Credentials."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981121749/29dfeeea-61d6-46c2-b587-25818dc52c8f.png" alt class="image--center mx-auto" /></p>
<p>Access Global Credentials (Unrestricted)</p>
<p>--&gt; Click on "System" and then "Global credentials (unrestricted)."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981140754/a63aa242-89d7-431c-990f-94b39742cbd8.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981542081/a26e40a9-a337-4d05-83e4-93548d273dbd.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981559231/5b8076d8-d8bc-4370-97b4-2a42c1514425.png" alt class="image--center mx-auto" /></p>
<p>Add AWS Credentials</p>
<p>--&gt; Click on "Add credentials."</p>
<p>Select AWS Credentials</p>
<p>--&gt; In the "Kind" section, select "AWS Credentials."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693981653472/d39ba34a-e086-4d91-97ea-22ef04852d74.png" alt class="image--center mx-auto" /></p>
<p>Configure AWS Credentials</p>
<p>Fill in the following details:</p>
<p>--&gt; ID: Give your credentials a unique identifier.</p>
<p>--&gt; Description: Add a description of your credentials.</p>
<p>--&gt; Access Key ID: Enter your AWS Access Key ID.</p>
<p>--&gt; Secret Access Key: Enter your AWS Secret Access Key.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982000799/dcf5648f-bfd6-4f49-8b63-e90d486b1874.png" alt class="image--center mx-auto" /></p>
<p>Save Credentials Save the credentials to store them securely.</p>
<p>Part 3: Build a Pipeline project to List Buckets Using AWS Credentials</p>
<p>Access Jenkins Dashboard</p>
<p>--&gt; Open your Jenkins dashboard.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982053023/9e1602bb-19c0-4481-982f-f4041154ba6c.png" alt class="image--center mx-auto" /></p>
<p>Create a New Pipeline</p>
<p>--&gt; Click on "New item" and select the "Pipeline" option.</p>
<p>--&gt; Name your pipeline, e.g., "configure-aws-credentials-using-pipeline."</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982093684/a81b8104-3e00-482b-8308-889ef0281d4c.png" alt class="image--center mx-auto" /></p>
<p>Go to the pipeline project</p>
<p>--&gt; Click on "Configure" and navigate to the "Pipeline" section.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982446921/b61b94ba-a96e-41e5-9687-07c87a87a16d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693982478272/0d751636-a63a-4fd5-bdcd-151a3dcd6959.png" alt class="image--center mx-auto" /></p>
<p>--&gt; Add the following script in the Pipeline section.</p>
<pre><code class="lang-plaintext">pipeline {
    agent any
    stages {
        stage('configure aws credentials') {
            steps {
                withCredentials([[
                $class: 'AmazonWebServicesCredentialsBinding',
                accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
                credentialsId: 'dev-user-aws-credentials']]) 
                {
                    script 
                    {
                        sh 'aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID'
                        sh 'aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY'
                        sh 'aws s3 ls'
                    }
                }
            }
        }
    }
}
</code></pre>
<p>Save the Pipeline</p>
<p>--&gt; Save this pipeline configuration.</p>
<p>Build the Pipeline</p>
<p>--&gt; After saving the pipeline click on "Build Now" to run the pipeline and configure AWS credentials.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693983734440/f427f26d-d8ae-40df-944e-54741d1b05b4.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693983752225/5111e2cb-d17b-4ff7-9fd8-9b08ab63a7f8.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Create AWS EC2 Windows Instance using terraform module]]></title><description><![CDATA[In this guide, we'll walk through the steps to create an AWS EC2 Instance using Terraform. This allows you to manage your AWS infrastructure as code.
Step 1: Project Setup
Create a directory named "Module_EC2_Instance_Windows_Project" for the project...]]></description><link>https://anilvishwanath3129.hashnode.dev/create-aws-ec2-windows-instance-using-terraform-module</link><guid isPermaLink="true">https://anilvishwanath3129.hashnode.dev/create-aws-ec2-windows-instance-using-terraform-module</guid><category><![CDATA[Terraform]]></category><category><![CDATA[AWS]]></category><category><![CDATA[terraform-module]]></category><category><![CDATA[EC2 instance]]></category><category><![CDATA[ec2 user data]]></category><dc:creator><![CDATA[Anil Mitkari]]></dc:creator><pubDate>Wed, 30 Aug 2023 13:00:23 GMT</pubDate><content:encoded><![CDATA[<p>In this guide, we'll walk through the steps to create an AWS EC2 Instance using Terraform. This allows you to manage your AWS infrastructure as code.</p>
<p>Step 1: Project Setup</p>
<p>Create a directory named "Module_EC2_Instance_Windows_Project" for the project.</p>
<p>Inside "Module_EC2_Instance_Windows_Project," create two more directories:</p>
<p>"aws_ec2_instance_windows"</p>
<p>"module"</p>
<p>************************************************************************</p>
<p>Step 2: Module Directory</p>
<p>Navigate to the "module" directory.</p>
<p>Create four subdirectories in the module directory.</p>
<p>2.1: Key_Pair</p>
<p>2.2: VPC</p>
<p>2.3: Security_Group</p>
<p>2.4: EC2_Instance_Windows</p>
<p>####################################################</p>
<p>Step 2.1: Key_Pair Module</p>
<p>Inside the "Key_Pair" directory, create three files:</p>
<p>2.1.1: main.tf</p>
<p>2.1.2: variables.tf</p>
<p>2.1.3: outputs.tf</p>
<p>Step 2.1.1: Configure main.tf</p>
<p>Add the following code to the main.tf file in the "Key_Pair" directory:</p>
<pre><code class="lang-plaintext"># https://registry.terraform.io/modules/terraform-aws-modules/key-pair/aws/latest
#####################
## Key Pair - Main ##
#####################

# Generates a secure private key and encodes it as PEM
resource "tls_private_key" "key_pair" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

# Create the Key Pair
resource "aws_key_pair" "key_pair" {
  key_name   = "${lower(var.app_name)}-${lower(var.app_environment)}-windows-${lower(var.region)}"  
  public_key = tls_private_key.key_pair.public_key_openssh
}

# Save file
resource "local_file" "ssh_key" {
  filename = "${aws_key_pair.key_pair.key_name}.pem"
  content  = tls_private_key.key_pair.private_key_pem
}
</code></pre>
<p>Step 2.1.2: Configure variables.tf</p>
<p>Add the necessary variables to the variables.tf file.</p>
<pre><code class="lang-plaintext">#######################
## Key Pair - Variables ##
#######################


variable "app_name" {}
variable "app_environment" {}
variable "region" {}
</code></pre>
<p>Step 2.1.3: Configure outputs.tf</p>
<p>Add the necessary output and their values to the outputs.tf file.</p>
<pre><code class="lang-plaintext">output "aws_key_name" {
  value = aws_key_pair.key_pair.key_name
}
</code></pre>
<p>#######################################################</p>
<p>Step 2.2: VPC Module</p>
<p>Inside the "VPC" directory, create three files:</p>
<p>2.2.1: main.tf</p>
<p>2.2.2: variables.tf</p>
<p>2.2.3: outputs.tf</p>
<p>Step 2.2.1: Configure main.tf</p>
<p>Add the following code to the main.tf file in the "VPC" directory:</p>
<pre><code class="lang-plaintext">##########################################
## Network Single AZ Public Only - Main ##
##########################################

# Create the VPC
resource "aws_vpc" "vpc" {
  cidr_block            = var.vpc_cidr
  enable_dns_hostnames  = var.dns_hostnames
  tags = {
    Name                = "${lower(var.app_name)}-${lower(var.app_environment)}-vpc"
    Environment         = var.app_environment
  }
}

# use data source to get all avalablility zones in region
data "aws_availability_zones" "available_zones" {}

# Define the public subnet
resource "aws_subnet" "public-subnet" {
  vpc_id            = aws_vpc.vpc.id
  cidr_block        = var.public_subnet_cidr
  availability_zone = data.aws_availability_zones.available_zones.names[0]
  tags = {
    Name            = "${lower(var.app_name)}-${lower(var.app_environment)}-public-subnet"
    Environment     = var.app_environment
  }
}

# Define the internet gateway
resource "aws_internet_gateway" "ig" {
  vpc_id        = aws_vpc.vpc.id
  tags = {
    Name        = "${lower(var.app_name)}-${lower(var.app_environment)}-igw"
    Environment = var.app_environment
  }
}

# Define the public route table
resource "aws_route_table" "public-rt" {
  vpc_id        = aws_vpc.vpc.id
  route {
    cidr_block  = var.public_rt_cidr_block
    gateway_id  = aws_internet_gateway.ig.id
  }
  tags = {
    Name        = "${lower(var.app_name)}-${lower(var.app_environment)}-public-subnet-rt"
    Environment = var.app_environment
  }
}

# Assign the public route table to the public subnet
resource "aws_route_table_association" "public-rt-association" {
  subnet_id      = aws_subnet.public-subnet.id
  route_table_id = aws_route_table.public-rt.id
}
</code></pre>
<p>Step 2.2.2: Configure variables.tf</p>
<p>Add the necessary variables to the variables.tf file.</p>
<pre><code class="lang-plaintext">##########################################
## Network Single AZ Public Only - Main ##
##########################################

# vpc variables

variable "vpc_cidr" {}
variable "dns_hostnames" {}
variable "app_name" {}
variable "app_environment" {}

#public subnet
variable "public_subnet_cidr" {}

#public route table
variable "public_rt_cidr_block" {}

#region
variable "region" {}
</code></pre>
<p>Step 2.2.3: Configure outputs.tf</p>
<p>Add the necessary output and their values to the outputs.tf file.</p>
<pre><code class="lang-plaintext">output "region" {
  value = var.region
}

output "app_name" {
  value = var.app_name
}

output "app_environment" {
  value = var.app_environment
}

output "vpc_id" {
  value = aws_vpc.vpc.id
}


output "publiic_subnet_id" {
  value = aws_subnet.public-subnet.id
}

output "internet_gateway" {
  value = aws_internet_gateway.ig
}
</code></pre>
<p>#######################################################</p>
<p>Step 2.3: Security_Group Module</p>
<p>Inside the "Security_Group" directory, create three files:</p>
<p>2.3.1: main.tf</p>
<p>2.3.2: variables.tf</p>
<p>2.3.3: outputs.tf</p>
<p>Step 2.3.1: Configure main.tf</p>
<p>Add the following code to the main.tf file in the "Security_Group" directory:</p>
<pre><code class="lang-plaintext"># Define the security group for the Windows server
resource "aws_security_group" "aws-windows-sg" {
  name        = "${lower(var.app_name)}-${var.app_environment}-windows-sg"
  description = "Allow incoming connections"
  vpc_id      = var.vpc_id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow incoming HTTP connections"
  }

  ingress {
    from_port   = 3389
    to_port     = 3389
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow incoming RDP connections"
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name        = "${lower(var.app_name)}-${var.app_environment}-windows-sg"
    Environment = var.app_environment
  }
}
</code></pre>
<p>Step 2.3.2: Configure variables.tf</p>
<p>Add the necessary variables to the variables.tf file.</p>
<pre><code class="lang-plaintext">##########################################
## Security Group ##
##########################################

variable "vpc_id" {}
variable "app_name" {}
variable "app_environment" {}
</code></pre>
<p>Step 2.3.3: Configure outputs.tf</p>
<p>Add the necessary output and their values to the outputs.tf file.</p>
<pre><code class="lang-plaintext">output "aws_security_group_id" {
  value = aws_security_group.aws-windows-sg.id
}
</code></pre>
<p>######################################################</p>
<p>Step 2.4: EC2_Instance_Windows Module</p>
<p>Inside the "EC2_Instance_Windows" directory, create three files:</p>
<p>2.4.1: main.tf</p>
<p>2.4.2: variables.tf</p>
<p>2.4.3: outputs.tf</p>
<p>2.4.4: windows-versions.tf</p>
<p>Step 2.4.1: Configure main.tf</p>
<p>Add the following code to the main.tf file in the "EC2_Instance_Windows" directory:</p>
<pre><code class="lang-plaintext">###################################
## Virtual Machine Module - Main ##
###################################

# Bootstrapping PowerShell Script
data "template_file" "windows-userdata" {
  template = &lt;&lt;EOF
&lt;powershell&gt;
# Rename Machine
Rename-Computer -NewName "${var.windows_instance_name}" -Force;

# Install IIS
Install-WindowsFeature -name Web-Server -IncludeManagementTools;

# Restart machine
shutdown -r -t 10;
&lt;/powershell&gt;
EOF
}

# Create EC2 Instance
resource "aws_instance" "windows-server" {
  ami                         = data.aws_ami.windows-2019.id
  instance_type               = var.windows_instance_type
  subnet_id                   = var.publiic_subnet_id
  vpc_security_group_ids      = [var.aws_security_group_id]
  associate_public_ip_address = var.windows_associate_public_ip_address
  source_dest_check           = var.source_dest_check_tf
  key_name                    = var.aws_key_name
  user_data                   = data.template_file.windows-userdata.rendered

  # root disk
  root_block_device {
    volume_size           = var.windows_root_volume_size
    volume_type           = var.windows_root_volume_type
    encrypted             = var.root_encrypted_tf
    delete_on_termination = var.root_delete_on_termination
  }

  # extra disk
  ebs_block_device {
    device_name           = "/dev/xvda"
    volume_size           = var.windows_data_volume_size
    volume_type           = var.windows_data_volume_type
    encrypted             = var.ebs_data_encrypted
    delete_on_termination = var.ebs_delete_on_termination
  }

  tags = {
    Name        = "${lower(var.app_name)}-${var.app_environment}-windows-server"
    Environment = var.app_environment
  }
}

# Create Elastic IP for the EC2 instance
resource "aws_eip" "windows-eip" {
  domain = "vpc"  
  tags = {
    Name        = "${lower(var.app_name)}-${var.app_environment}-windows-eip"
    Environment = var.app_environment
  }
}


# Associate Elastic IP to Windows Server
resource "aws_eip_association" "windows-eip-association" {
  instance_id   = aws_instance.windows-server.id
  allocation_id = aws_eip.windows-eip.id
}
</code></pre>
<p>Step 2.4.2: Configure variables.tf</p>
<p>Add the necessary variables to the variables.tf file.</p>
<pre><code class="lang-plaintext">########################################
## Virtual Machine Module - Variables ##
########################################

variable "app_name" {}
variable "app_environment" {}

variable "windows_instance_name" {}

variable "windows_instance_type" {}
variable "publiic_subnet_id" {}
variable "aws_security_group_id" {}
variable "windows_associate_public_ip_address" {}
variable "source_dest_check_tf" {}
variable "aws_key_name" {}

variable "windows_root_volume_size" {}
variable "windows_root_volume_type" {}
variable "root_encrypted_tf" {}
variable "root_delete_on_termination" {}

variable "windows_data_volume_size" {}
variable "windows_data_volume_type" {}
variable "ebs_data_encrypted" {}
variable "ebs_delete_on_termination" {}
</code></pre>
<p>Step 2.4.3: Configure outputs.tf</p>
<p>Add the necessary output and their values to the outputs.tf file.</p>
<pre><code class="lang-plaintext">#####################################
## Virtual Machine Module - Output ##
#####################################

output "vm_windows_server_instance_name" {
  value = var.windows_instance_name
}

output "vm_windows_server_instance_id" {
  value = aws_instance.windows-server.id
}

output "vm_windows_server_instance_public_dns" {
  value = aws_instance.windows-server.public_dns
}

output "vm_windows_server_instance_public_ip" {
  value = aws_eip.windows-eip.public_ip
}

output "vm_windows_server_instance_private_ip" {
  value = aws_instance.windows-server.private_ip
}
</code></pre>
<p>2.4.4: Configure windows-versions.tf</p>
<p>Add resources to get the latest ami of windows versions.</p>
<pre><code class="lang-plaintext"># https://github.com/guillermo-musumeci/terraform-aws-latest-ami/blob/master/Get-Latest-Windows-AMI.tf
################################################
# Get latest Windows Server AMI with Terraform #
################################################

# Get latest Windows Server 2012R2 AMI
data "aws_ami" "windows-2012-r2" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["Windows_Server-2012-R2_RTM-English-64Bit-Base-*"]
  }
}

# Get latest Windows Server 2016 AMI
data "aws_ami" "windows-2016" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["Windows_Server-2016-English-Full-Base*"]
  }
}

# Get latest Windows Server 2019 AMI
data "aws_ami" "windows-2019" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["Windows_Server-2019-English-Full-Base*"]
  }
}

# Get latest Windows Server 2022 AMI
data "aws_ami" "windows-2022" {
  most_recent = true
  owners      = ["amazon"]
  filter {
    name   = "name"
    values = ["Windows_Server-2022-English-Full-Base*"]
  }
}
</code></pre>
<p>Step 3: AWS_EC2_Instance_Windows Directory</p>
<p>Navigate to the "aws_ec2_instance_windows" directory.</p>
<p>Create four files:</p>
<p>3.1: providers.tf</p>
<p>3.2: main.tf</p>
<p>3.3: variables.tf</p>
<p>3.4: terraform.tfvars</p>
<p>Step 3.1: Configure providers.tf for EC2 instance</p>
<p>Add the following code to the providers.tf file in the "aws_ec2_instance_windows" directory:</p>
<pre><code class="lang-plaintext"># https://registry.terraform.io/providers/hashicorp/aws/latest/docs
################################
## AWS Provider Module - Main ##
################################

# AWS Provider

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~&gt; 5.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = var.region
}
</code></pre>
<p>Step 3.2: Configure main.tf for the EC2 instance</p>
<p>Add the following code to the main.tf file in the "aws_ec2_instance_windows" directory:</p>
<pre><code class="lang-plaintext"># vpc module

module "aws_vpc" {
  source               = "../module/VPC"
  region               = var.region
  vpc_cidr             = var.vpc_cidr
  dns_hostnames        = var.dns_hostnames 
  app_name             = var.app_name
  app_environment      = var.app_environment
  public_subnet_cidr   = var.public_subnet_cidr
  public_rt_cidr_block = var.public_rt_cidr_block

}

# key_pair module

module "aws_key_pair" {
  source          = "../module/Key_Pair"
  app_name        = module.aws_vpc.app_name
  app_environment = module.aws_vpc.app_environment
  region          = module.aws_vpc.region
}

#Security group module

module "security_group" {
  source          = "../module/Security_Group"
  vpc_id          = module.aws_vpc.vpc_id
  app_name        = module.aws_vpc.app_name
  app_environment = module.aws_vpc.app_environment
}

#instance module

module "aws_instance" {
  source                              = "../module/ec2-instance-windows"
  app_name                            = module.aws_vpc.app_name
  app_environment                     = module.aws_vpc.app_environment
  windows_instance_name               = var.windows_instance_name
  windows_instance_type               = var.windows_instance_type
  publiic_subnet_id                   = module.aws_vpc.publiic_subnet_id
  aws_security_group_id               = module.security_group.aws_security_group_id
  windows_associate_public_ip_address = var.windows_associate_public_ip_address
  source_dest_check_tf                = var.source_dest_check_tf
  aws_key_name                        = module.aws_key_pair.aws_key_name
  windows_root_volume_size            = var.windows_root_volume_size
  windows_root_volume_type            = var.windows_root_volume_type
  root_encrypted_tf                   = var.root_encrypted_tf
  root_delete_on_termination          = var.root_delete_on_termination
  windows_data_volume_size            = var.windows_data_volume_size
  windows_data_volume_type            = var.windows_data_volume_type
  ebs_data_encrypted                  = var.ebs_data_encrypted
  ebs_delete_on_termination           = var.ebs_delete_on_termination
}
</code></pre>
<p>Step 3.3 Configure variables.tf for EC2 instance</p>
<p>Add the necessary variables to the variables.tf file in the "aws_ec2_instance_windows" directory.</p>
<pre><code class="lang-plaintext">##############################################
## Network Single AZ Public Only - Variables #
##############################################

variable "vpc_cidr" {}
variable "dns_hostnames" {}
variable "app_name" {}
variable "app_environment" {}

#public subnet
variable "public_subnet_cidr" {}

#public route table
variable "public_rt_cidr_block" {}

#region
variable "region" {}



########################################
## Virtual Machine Module - Variables ##
########################################

variable "windows_instance_name" {}

variable "windows_instance_type" {}
variable "windows_associate_public_ip_address" {}
variable "source_dest_check_tf" {}

# root device 
variable "windows_root_volume_size" {}
variable "windows_root_volume_type" {}
variable "root_encrypted_tf" {}
variable "root_delete_on_termination" {}

# ebs device
variable "windows_data_volume_size" {}
variable "windows_data_volume_type" {}
variable "ebs_data_encrypted" {}
variable "ebs_delete_on_termination" {}
</code></pre>
<p>Step 4.2: Configure terraform.tfvars for EC2 instance</p>
<p>Add your specific variable and values to the terraform.tfvars file in the "aws_ec2_instance_windows" directory.</p>
<pre><code class="lang-plaintext"># Application Definition 
app_name        = "netflix" # Do NOT enter any spaces
app_environment = "dev"       # Dev, Test, Staging, Prod, etc

# Network
vpc_cidr           = "172.31.0.0/16"
dns_hostnames      = "true"
public_subnet_cidr = "172.31.0.0/20"
public_rt_cidr_block = "0.0.0.0/0"
region     = "ap-south-1"

# Windows Virtual Machine
windows_instance_name               = "test-windows-ec2"
windows_instance_type               = "t2.micro"
windows_associate_public_ip_address = true
source_dest_check_tf                = false
windows_root_volume_size            = 30
windows_root_volume_type            = "gp3"
root_encrypted_tf                   = true    
root_delete_on_termination          = true
windows_data_volume_size            = 10
windows_data_volume_type            = "gp3"
ebs_data_encrypted                  = true
ebs_delete_on_termination           = true
</code></pre>
<p>Step 5: Terraform Commands</p>
<p>Open a command prompt or terminal and navigate to the "aws_ec2_instance_windows" directory.</p>
<p>Run the following commands:</p>
<p>5.1: terraform init: Initialize Terraform.</p>
<p>5.2: terraform validate: Validate your Terraform configuration.</p>
<p>5.3: terraform plan: Preview the changes Terraform will make.</p>
<p>5.4: terraform apply --auto-approve: Apply the Terraform configuration, creating the security group.</p>
]]></content:encoded></item><item><title><![CDATA[Create AWS Security-Group using terraform security-group module]]></title><description><![CDATA[In this guide, we'll walk through the steps to create an AWS security group using Terraform. This allows you to manage your AWS infrastructure as code.
Step 1: Project Setup

Create a directory named "module-security-group-project" for the project.

...]]></description><link>https://anilvishwanath3129.hashnode.dev/create-aws-security-group-using-terraform-security-group-module</link><guid isPermaLink="true">https://anilvishwanath3129.hashnode.dev/create-aws-security-group-using-terraform-security-group-module</guid><category><![CDATA[Terraform]]></category><category><![CDATA[AWS]]></category><category><![CDATA[AWS Security Group]]></category><category><![CDATA[terraform-module]]></category><dc:creator><![CDATA[Anil Mitkari]]></dc:creator><pubDate>Fri, 25 Aug 2023 09:51:35 GMT</pubDate><content:encoded><![CDATA[<p>In this guide, we'll walk through the steps to create an AWS security group using Terraform. This allows you to manage your AWS infrastructure as code.</p>
<p><strong>Step 1: Project Setup</strong></p>
<ol>
<li><p>Create a directory named "module-security-group-project" for the project.</p>
</li>
<li><p>Inside "module-security-group-project," create two more directories:</p>
<ul>
<li><p>"aws-security-group"</p>
</li>
<li><p>"module"</p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 2: Module Directory</strong></p>
<ol>
<li><p>Navigate to the "module" directory.</p>
</li>
<li><p>Create a subdirectory named "security-group" within the "module" directory.</p>
</li>
</ol>
<p><strong>Step 3: Security Group Module</strong></p>
<ol>
<li><p>Inside the "security-group" directory, create three files:</p>
<ul>
<li><p><code>main.tf</code></p>
</li>
<li><p><code>variables.tf</code></p>
</li>
<li><p><code>outputs.tf</code></p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 4: Configure</strong> <a target="_blank" href="http://main.tf"><strong>main.tf</strong></a></p>
<ol>
<li>Add the following code to the <a target="_blank" href="http://main.tf"><code>main.tf</code></a> file in the "security-group" directory:</li>
</ol>
<pre><code class="lang-plaintext">resource "aws_security_group" "demo_sg01_sg" {
  name        = var.demo_sg01_sg_name
  description = var.demo_sg01_sg_description
  vpc_id      = var.demo_sg01_sg_vpc
  ingress {
    description      = var.demo_sg01_insgress_description
    from_port        = var.demo_sg01_ingress_from_port
    to_port          = var.demo_sg01_ingress_to_port
    protocol         = var.demo_sg01_ingress_protocol
    cidr_blocks      = [var.my_ip_address]
    }

  egress {
    from_port        = var.demo_sg01_egress_from_port
    to_port          = var.demo_sg01_egress_to_port
    protocol         = var.demo_sg01_egress_protocol
    cidr_blocks      = [var.demo_sg01_egress_cidr_block]
    }

  tags = {
    Name = var.demo_sg01_tag_name
  }
}


resource "aws_security_group" "demo_sg02_sg" {
  name        = var.demo_sg02_sg_name
  description = var.demo_sg02_sg_description
  vpc_id      = var.demo_sg02_sg_vpc

  ingress {
    description      = var.demo_sg02_insgress_description
    from_port        = var.demo_sg02_ingress_from_port
    to_port          = var.demo_sg02_ingress_to_port
    protocol         = var.demo_sg02_ingress_protocol
    security_groups = [aws_security_group.demo_sg01_sg.id]
    }

  egress {
    from_port        = var.demo_sg02_egress_from_port
    to_port          = var.demo_sg02_egress_to_port
    protocol         = var.demo_sg02_egress_protocol
    cidr_blocks      = [var.demo_sg02_egress_cidr_block]
    }

  tags = {
    Name = var.demo_sg02_tag_name
  }
}
</code></pre>
<p><strong>Step 5: Configure</strong> <strong>variables.tf</strong></p>
<ol>
<li>Add the necessary variables and their descriptions to the <code>variables.tf</code> file.</li>
</ol>
<pre><code class="lang-plaintext"># Define variables

variable "my_ip_address" {}
variable "demo_sg01_sg_name" {}
variable "demo_sg01_sg_description" {}
variable "demo_sg01_sg_vpc" {}

variable "demo_sg01_insgress_description" {}
variable "demo_sg01_ingress_from_port" {}
variable "demo_sg01_ingress_to_port" {}
variable "demo_sg01_ingress_protocol" {}


variable "demo_sg01_egress_from_port" {}
variable "demo_sg01_egress_to_port" {}
variable "demo_sg01_egress_protocol" {}
variable "demo_sg01_egress_cidr_block" {}

variable "demo_sg01_tag_name" {}


#########################################

variable "demo_sg02_sg_name" {}
variable "demo_sg02_sg_description" {}
variable "demo_sg02_sg_vpc" {}

variable "demo_sg02_insgress_description" {}
variable "demo_sg02_ingress_from_port" {}
variable "demo_sg02_ingress_to_port" {}
variable "demo_sg02_ingress_protocol" {}


variable "demo_sg02_egress_from_port" {}
variable "demo_sg02_egress_to_port" {}
variable "demo_sg02_egress_protocol" {}
variable "demo_sg02_egress_cidr_block" {}

variable "demo_sg02_tag_name" {}
</code></pre>
<p><strong>Step 6: Configure</strong> <strong>outputs.tf</strong></p>
<ol>
<li>Add the necessary variables and their descriptions to the <code>outputs.tf</code> file.</li>
</ol>
<pre><code class="lang-plaintext">output "demo_sg01_sg_id" {
  value = aws_security_group.demo_sg01_sg.id
}

output "demo_sg02_sg_id" {
  value = aws_security_group.demo_sg02_sg.id
}
</code></pre>
<p><strong>Step 7: AWS Security Group Directory</strong></p>
<ol>
<li><p>Navigate to the "aws-security-group" directory.</p>
</li>
<li><p>Create three files:</p>
<ul>
<li><p><code>main.tf</code></p>
</li>
<li><p><code>variables.tf</code></p>
</li>
<li><p><code>terraform.tfvars</code></p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 8: Configure</strong> <strong>main.tf for Security Group</strong></p>
<ol>
<li>Add the following code to the <code>main.tf</code> file in the "security-group" directory:</li>
</ol>
<pre><code class="lang-plaintext"># configure aws provider

provider "aws" {
  region = var.region
  }


#create security group

module "security_groups" {
  source                          = "../modules/security-group"
  my_ip_address                   = var.my_ip_address
  demo_sg01_sg_name               =  var.demo_sg01_sg_name    
  demo_sg01_sg_description        = var.demo_sg01_sg_description
  demo_sg01_sg_vpc                = var.demo_sg01_sg_vpc      

  demo_sg01_insgress_description  = var.demo_sg01_insgress_description     
  demo_sg01_ingress_from_port     = var.demo_sg01_ingress_from_port   
  demo_sg01_ingress_to_port       =  var.demo_sg01_ingress_to_port  
  demo_sg01_ingress_protocol      =  var.demo_sg01_ingress_protocol  

  demo_sg01_egress_from_port      =  var.demo_sg01_egress_from_port   
  demo_sg01_egress_to_port        =  var.demo_sg01_egress_to_port   
  demo_sg01_egress_protocol       = var.demo_sg01_egress_protocol   
  demo_sg01_egress_cidr_block     = var.demo_sg01_egress_cidr_block   

  demo_sg01_tag_name              =  var.demo_sg01_tag_name  

  demo_sg02_sg_name                = var.demo_sg02_sg_name      
  demo_sg02_sg_description         = var.demo_sg02_sg_description
  demo_sg02_sg_vpc                 = var.demo_sg02_sg_vpc

  demo_sg02_insgress_description   = var.demo_sg02_insgress_description    
  demo_sg02_ingress_from_port      = var.demo_sg02_ingress_from_port
  demo_sg02_ingress_to_port        = var.demo_sg02_ingress_to_port  
  demo_sg02_ingress_protocol       = var.demo_sg02_ingress_protocol    

  demo_sg02_egress_from_port       = var.demo_sg02_egress_from_port     
  demo_sg02_egress_to_port         = var.demo_sg02_egress_to_port  
  demo_sg02_egress_protocol        = var.demo_sg02_egress_protocol     
  demo_sg02_egress_cidr_block      = var.demo_sg02_egress_cidr_block

  demo_sg02_tag_name               = var.demo_sg02_tag_name 

}
</code></pre>
<p><strong>Step 9: Configure</strong> <strong>variables.tf for Security Group</strong></p>
<ol>
<li>Add the necessary variables and their descriptions to the <code>variables.tf</code> file in the "security-group" directory.</li>
</ol>
<pre><code class="lang-plaintext">variable "region" {}

##################################
# Define variables

variable "my_ip_address" {}
variable "demo_sg01_sg_name" {}
variable "demo_sg01_sg_description" {}
variable "demo_sg01_sg_vpc" {}

variable "demo_sg01_insgress_description" {}
variable "demo_sg01_ingress_from_port" {}
variable "demo_sg01_ingress_to_port" {}
variable "demo_sg01_ingress_protocol" {}


variable "demo_sg01_egress_from_port" {}
variable "demo_sg01_egress_to_port" {}
variable "demo_sg01_egress_protocol" {}
variable "demo_sg01_egress_cidr_block" {}

variable "demo_sg01_tag_name" {}


#########################################

variable "demo_sg02_sg_name" {}
variable "demo_sg02_sg_description" {}
variable "demo_sg02_sg_vpc" {}

variable "demo_sg02_insgress_description" {}
variable "demo_sg02_ingress_from_port" {}
variable "demo_sg02_ingress_to_port" {}
variable "demo_sg02_ingress_protocol" {}


variable "demo_sg02_egress_from_port" {}
variable "demo_sg02_egress_to_port" {}
variable "demo_sg02_egress_protocol" {}
variable "demo_sg02_egress_cidr_block" {}

variable "demo_sg02_tag_name" {}
</code></pre>
<p><strong>Step 10: Configure terraform.tfvars for Security Group</strong></p>
<ol>
<li>Add your specific variable values to the <code>terraform.tfvars</code> file in the "security-group" directory.</li>
</ol>
<pre><code class="lang-plaintext">region="ap-south-1"

#########################################
#secutiry group

my_ip_address="198.187.653.159/32"
demo_sg01_sg_name="demo-ec2-windows-sg-sg"
demo_sg01_sg_description="this is demo-ec2-windows-sg sg"     
demo_sg01_sg_vpc="vpc-0965d56f"     

demo_sg01_insgress_description="allow all ports in demo-ec2-windows-sg sg"     
demo_sg01_ingress_from_port=0     
demo_sg01_ingress_to_port=65535     
demo_sg01_ingress_protocol="tcp"     

demo_sg01_egress_from_port=0     
demo_sg01_egress_to_port=0     
demo_sg01_egress_protocol="-1"     
demo_sg01_egress_cidr_block="0.0.0.0/0"     

demo_sg01_tag_name="Demo-Ec2-Windows-Security-Group"   


demo_sg02_sg_name="demo-ec2-linux-sg sg"     
demo_sg02_sg_description="this is demo-ec2-linux-sg sg"     
demo_sg02_sg_vpc="vpc-0965d56f"

demo_sg02_insgress_description="allow all tcp in demo-ec2-linux-sg sg"    
demo_sg02_ingress_from_port=0  
demo_sg02_ingress_to_port=65535   
demo_sg02_ingress_protocol="tcp"     

demo_sg02_egress_from_port=0     
demo_sg02_egress_to_port=0   
demo_sg02_egress_protocol="-1"     
demo_sg02_egress_cidr_block="0.0.0.0/0"     

demo_sg02_tag_name="Demo-Ec2-Linux-Security-Group"
</code></pre>
<p><strong>Step 11: Terraform Commands</strong></p>
<ol>
<li><p>Open a command prompt or terminal and navigate to the "security-group" directory.</p>
</li>
<li><p>Run the following commands:</p>
</li>
</ol>
<ul>
<li><p><code>terraform init</code>: Initialize Terraform.</p>
</li>
<li><p><code>terraform validate</code>: Validate your Terraform configuration.</p>
</li>
<li><p><code>terraform plan</code>: Preview the changes Terraform will make.</p>
</li>
<li><p><code>terraform apply --auto-approve</code>: Apply the Terraform configuration, creating the security group.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Create AWS key-pair using terraform key-pair module]]></title><description><![CDATA[In this guide, we'll walk through the steps to create an AWS key pair using Terraform. This allows you to manage your AWS infrastructure as code.
Step 1: Project Setup

Create a directory named "module-key-pair-project" for the project.

Inside "modu...]]></description><link>https://anilvishwanath3129.hashnode.dev/create-aws-key-pair-using-terraform-key-pair-module</link><guid isPermaLink="true">https://anilvishwanath3129.hashnode.dev/create-aws-key-pair-using-terraform-key-pair-module</guid><category><![CDATA[Terraform]]></category><category><![CDATA[terraform-module]]></category><category><![CDATA[keypair]]></category><category><![CDATA[awscli]]></category><category><![CDATA[AWS]]></category><dc:creator><![CDATA[Anil Mitkari]]></dc:creator><pubDate>Wed, 23 Aug 2023 08:29:03 GMT</pubDate><content:encoded><![CDATA[<p>In this guide, we'll walk through the steps to create an AWS key pair using Terraform. This allows you to manage your AWS infrastructure as code.</p>
<p><strong>Step 1: Project Setup</strong></p>
<ol>
<li><p>Create a directory named "module-key-pair-project" for the project.</p>
</li>
<li><p>Inside "module-key-pair-project," create two more directories:</p>
<ul>
<li><p>"aws-key-pair"</p>
</li>
<li><p>"module"</p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 2: Module Directory</strong></p>
<ol>
<li><p>Navigate to the "module" directory.</p>
</li>
<li><p>Create a subdirectory named "key-pair" within the "module" directory.</p>
</li>
</ol>
<p><strong>Step 3: Key Pair Module</strong></p>
<ol>
<li><p>Inside the "key-pair" directory, create two files:</p>
<ul>
<li><p><a target="_blank" href="http://main.tf"><code>main.tf</code></a></p>
</li>
<li><p><a target="_blank" href="http://variables.tf"><code>variables.tf</code></a></p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 4: Configure</strong> <a target="_blank" href="http://main.tf"><strong>main.tf</strong></a></p>
<ol>
<li>Add the following code to the <a target="_blank" href="http://main.tf"><code>main.tf</code></a> file in the "key-pair" directory:</li>
</ol>
<pre><code class="lang-plaintext">

resource "aws_key_pair" "key_pairs" {
  count = length(var.key_pairs)
  key_name   = var.key_pairs[count.index].key_name
  public_key = tls_private_key.key_pairs[count.index].public_key_openssh
}

resource "tls_private_key" "key_pairs" {
  count      = length(var.key_pairs)
  algorithm  = "RSA"
  rsa_bits   = var.key_pairs[count.index].rsa_bits
}

resource "local_file" "private_key" {
  count       = length(var.key_pairs)
  content     = tls_private_key.key_pairs[count.index].private_key_pem
  filename    = "${var.key_pairs[count.index].key_name}.pem"
}

resource "local_file" "putty_key" {
  count       = length(var.key_pairs)
  content     = tls_private_key.key_pairs[count.index].private_key_pem
  filename    = "${var.key_pairs[count.index].key_name}.ppk"
  provisioner "local-exec" {
   command = "puttygen ${var.key_pairs[count.index].key_name}.pem -o ${var.key_pairs[count.index].key_name}.ppk"
  }
  provisioner "local-exec" {
   command = "cp -r ${var.key_pairs[count.index].key_name}.pem /home/anil/aws/" 
  }
}
</code></pre>
<p><strong>Step 5: Configure</strong> <a target="_blank" href="http://variables.tf"><strong>variables.tf</strong></a></p>
<ol>
<li>Add the necessary variables and their descriptions to the <a target="_blank" href="http://variables.tf"><code>variables.tf</code></a> file.</li>
</ol>
<pre><code class="lang-plaintext">variable "key_pairs" {
  type = list(object({
    key_name  = string
    rsa_bits  = number
  }))
}
</code></pre>
<p><strong>Step 6: AWS Key Pair Directory</strong></p>
<ol>
<li><p>Navigate to the "aws-key-pair" directory.</p>
</li>
<li><p>Create three files:</p>
<ul>
<li><p><a target="_blank" href="http://main.tf"><code>main.tf</code></a></p>
</li>
<li><p><a target="_blank" href="http://variables.tf"><code>variables.tf</code></a></p>
</li>
<li><p><code>terraform.tfvars</code></p>
</li>
</ul>
</li>
</ol>
<p><strong>Step 7: Configure</strong> <a target="_blank" href="http://main.tf"><strong>main.tf</strong></a> <strong>for AWS Key Pair</strong></p>
<ol>
<li>Add the following code to the <a target="_blank" href="http://main.tf"><code>main.tf</code></a> file in the "aws-key-pair" directory:</li>
</ol>
<pre><code class="lang-plaintext"># configure aws provider

provider "aws" {
  region = var.region
  }

module "aws_key_pair" {
  source     = "../modules/key-pair"
  key_pairs  = var.key_pairs
}
</code></pre>
<p><strong>Step 8: Configure</strong> <a target="_blank" href="http://variables.tf"><strong>variables.tf</strong></a> <strong>for AWS Key Pair</strong></p>
<ol>
<li>Add the necessary variables and their descriptions to the <a target="_blank" href="http://variables.tf"><code>variables.tf</code></a> file in the "aws-key-pair" directory.</li>
</ol>
<pre><code class="lang-plaintext">variable "region" {}


######################################################3
variable "key_pairs" {
  type = list(object({
    key_name  = string
    rsa_bits  = number
  }))
}
</code></pre>
<p><strong>Step 9: Configure terraform.tfvars for AWS Key Pair</strong></p>
<ol>
<li>Add your specific variable values to the <code>terraform.tfvars</code> file in the "aws-key-pair" directory.</li>
</ol>
<pre><code class="lang-plaintext">region="ap-south-1"

key_pairs = [
  {
    key_name = "aws_key_pair01"
    rsa_bits = 4096
  },
  {
    key_name = "aws_key_pair02"
    rsa_bits = 4096
  },
  {
    key_name = "aws_key_pair03"
    rsa_bits = 4096
  }
]
</code></pre>
<p><strong>Step 10: Terraform Commands</strong></p>
<ol>
<li><p>Open a command prompt or terminal and navigate to the "aws-key-pair" directory.</p>
</li>
<li><p>Run the following commands:</p>
</li>
</ol>
<ul>
<li><p><code>terraform init</code>: Initialize Terraform.</p>
</li>
<li><p><code>terraform validate</code>: Validate your Terraform configuration.</p>
</li>
<li><p><code>terraform plan</code>: Preview the changes Terraform will make.</p>
</li>
<li><p><code>terraform apply --auto-approve</code>: Apply the Terraform configuration, creating the AWS key pair.</p>
</li>
</ul>
<p><strong>Step 11: Conclusion</strong></p>
<ol>
<li>After executing these commands, you will receive output confirming the successful creation of the AWS key pair using Terraform.</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692778768866/6bfcfd5d-9593-4538-b658-f624372b2e0e.png" alt class="image--center mx-auto" /></p>
<p>Congratulations! You've successfully created an AWS key pair using Terraform's modular approach.</p>
<hr />
]]></content:encoded></item></channel></rss>