AWS VM creation through Powershell

Topic General

Amazon Web Services (AWS) is now widely used in several companies around the world. AWS is a secure cloud services platform, offering compute power, database storage, content delivery and other functionalities to help businesses scale and grow. It offers a broad set of global compute, storage, database, analytics, application, and deployment services that help organizations move faster, lower IT costs, and scale applications.

AWS Cloud spans 57 Availability Zones within 19 geographic regions around the world, with announced plans for 15 more Availability Zones and five more Regions.

It provides end-to-end solutions for IT Solutions for both small and big scale organisations.

Today I am going to demonstrate one of the very basic but yet necessary process : Create an AWS VM through Powershell which can be plugged into any CI tool (I have used Jenkins for the same).

You may ask the question, when there is a wonderful GUI available within AWS EC2 Dashboard to provision a VM, what is the need to create it through Powershell ?

Well, again it totally depends on the requirements – I had one requirement, where I needed to provision VMs through CI tool so that my user had to use only one Interface which is “Jenkins” for whole CI-CD stuff which starts from creating VM.

Hence I have used Powershell and used that script in my Jenkins job to provision the VM within minutes.

Get the AMI name which will be used to create VMs.

Let me explain what actually this Image (AMI) means and how it is beneficially used :

AWS Machine Images, aka AMIs are used to provide a new virtual machine with an operating system. It would be replica of existing VM with exactly the same OS, Memory, Data, Applications, Tools already loaded. An image might also have one or more data disks.

It is useful when you want to create same machine to avoid the saying “It just work on my machine” which is really useful in today’s leading world. You can create multiple VMs sequentially or parallel based on your requirements with just one image, such as Dev Environment, Test Environment, Stage Environment, QA Environment and so on….

So the first step is “Adding your AWS account in the Powershell

To do that – You need to configure AWS on your powershell.

You need to provide AWS Access Key ID and Secret Access Key which you can fetch by following the below process

Log in to your AWS Management Console.

Click on your user name at the top right of the page.

Click on the ‘My Security Credentials’ link from the drop-down menu.

Find the ‘Access Keys’ section, and create the Access Key Or Copy the latest Access Key ID.

Click on the ‘Show Access Key’ in the same row, and copy the Secret Access Key.

Now, Type “aws configure” in the Powershell console

PS C:\Users\Administrator> aws configure
AWS Access Key ID [None]: AKIAJP*********CQ
AWS Secret Access Key [None]: AFqTi9O**************WMnPY
Default region name [None]: ap-southeast-2
Default output format [None]: text

 

Find out that Machine “image-id” and respective “subnet-id”

Copy and paste below script on your local and change the variables value accordingly.

 

# Creation of AWS windows VMs - Ravi

 [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$instname
    )

echo -e “Launching the Windows 2016 Server ....\n”

$instance_id=$(aws ec2 run-instances --image-id ami-f15e4992  --subnet-id subnet-b0d742c6 --count 1 --instance-type m1.large --key-name MicroFocusDemo --associate-public-ip-address --query Instances[*].InstanceId)

echo -e “AWS instance_id is $instance_id\n”

echo -e “Server is still provisioning . . . .  Please Wait....\n”

sleep 30

do { sleep 30; echo 'Still Initializing....'; $state=$(((aws ec2 describe-instance-status --instance-id $instance_id | select-string -pattern INSTANCESTATUS | select-object -Last 1 | out-string).split("`n") -match '\S').Substring(15)) } until ($state -match 'ok')

echo -e “Server is ready for your use\n”

aws ec2 create-tags --resources $instance_id --tag Key=Name,Value=$instname

echo -n “”

$ip_address=$(aws ec2 describe-instances --instance-ids $instance_id --output text --query 'Reservations[*].Instances[*].PublicIpAddress')

echo -e “================================================\n”
echo -e "AWS VM Instance id is $instance_id\n"
echo -e "VM Public IP Address is $ip_address\n"
echo -e "AWS VM name is $instname\n"
echo -e “================================================\n”


Clear-Variable -Name instance_id
Clear-Variable -Name instname
Clear-Variable -Name state
Clear-Variable -Name ip_address

echo -e “AWS VM provisioned successfully\n”

 

Run the powershell script and you can see a New Windows VM gets created for you.

0 comments… add one

Leave a Comment