Creating an EC2 Instance with Powershell

**PLEASE READ THE COMMENT SECTION BEFORE USING THE SCRIPT

This example will launch an AWS EC2 instance in us-east-1 using Amazon AMI Linux Image.

All syntaxes will take its values from variables defined.

 

$creds = Set-AWSCredential -accesskey "AKIAIUEGD6LCXW7ALpha" -secretkey "gqfE+Q0FqNfTzVovGHt+/PudymXEWiiqHelloWORLD"
$region = "us-east-1"

 

The keypair with the name “WebServer” is already created and *.pem file is on my file server

$Keypair = "WebServer"
$imageID = "ami-a4c7edb2"
$instancetype = "t2.micro"
$VPCID = "vpc-70039e09"
$Securitygroup = "sg-1b94d26a"

Now that basic variables are declared, let’s start building the actual command to create EC2 Instance.

New-EC2Instance -ImageId $imageID -AssociatePublicIp $true -KeyName $Keypair -SecurityGroupID $Securitygroup -AvailabilityZone us-east-1a -InstanceType $instancetype -SubnetId "subnet-58d45b02"

This creates a new instance in AWS US-EAST-1 ( North Virginia ) region with the parameters as defined in the script.

New-EC2Instance -ImageId $imageID -AssociatePublicIp $true -KeyName $Keypair -SecurityGroupID $Securitygroup -AvailabilityZone us-east-1a -InstanceType $instancetype -SubnetId "subnet-58d45b02"

GroupNames    : {}
Groups        : {}
Instances     : {WebServer}
OwnerID       : 513142545209
RequesterID   :
ReservationID : r-00cab22cbf0d8846e

Caution: Running the same command several times will create as many machines. For testing, run this script just once unless you intend to create multiple VM’s.

Get the EC2 Inventory

Alright, Now that we have deployed several VM’s with our favorite scripting platform, let’s get a basic inventory of the all VM’s with certain information like their Instance ID’s and their private/public IP address.  We need to start using loops now as we need to look into each of the instances in AWS and get their attributes from the metadata.

Eventually this Is how its gonna look like.

$AllVMs = (Get-EC2Instance -Region us-east-1).Instances
foreach ( $instance in $AllVMs)

{
$instance.instanceid + "," +  $instance.publicIPaddress + "," + $instance.privateIPaddress

} 

The output as below:

i-07cc395d6e38c32aa,54.82.215.221,192.168.30.213
i-094263c3de8a635f5,54.92.194.158,192.168.30.100
i-0db046644d228fbdb,52.87.210.223,192.168.30.43

You can of course export it to CSV or HTML and get the output in much presentable fashion.

 

Terminating EC2 Instances

Now that we have learnt to create instances and pull up their metadata reports as well, let’s terminate them all in one go.

Get-EC2Instance | Stop-EC2Instance -Terminate 

 

Notice the “|” symbol in the one line script.  It’s a simple way of telling the script to pass the output of command in front of it (which is get-ec2instance) to the command right after it (which is stop-EC2instance in this case).

Get-EC2Instance is retrieving the list of instances in the region and stop-instances will stop them. The Terminate switch will terminate the instance once the instances are stopped. Here is how the EC2 looks like before executing the script.

Once the instances are terminated, you will see the status turn ember. You are good to delete the instances.

Anand Rao