AWS PowerShell for better Productivity

Suyash garg
9 min readOct 13, 2020

--

Introduction

In this article i am going to:-

  • How to install AWS on powershell
  • Then configuring aws profile inside powershell
  • Then setting default profile in powershell
  • Then some example
  • Thing to remember when using powershell
  • Getting help from powershell
  • Syntax of PowerShell commands
  • And finally closing note

So let get Started

AWS CLI Installation on PowerShell

First and foremost task before proceeding further we have to install aws cli in windows powershell which is pretty easy just download AWSPowerShell.zip extract it copy folder in aws ps module path which is generally in any two following directory.

C:\windows\system32\WindowsPowerShell\v1.0\Modules

C:\Program Files\WindowsPowerShell\Modules

And run Get-Module -ListAvailable

in my case is showing AWSPowerShell module in directory C:\windows\system32\WindowsPowerShell\v1.0\Modules

Configure profile in aws

Creating new profile

To configure profile in powershell perform following steps

Set-AWSCredential -AccessKey[your access key] -SecretKey[your secret key] -StoreAs MyNewProfile

StoreAS parameter gives a alias to the following credential it's always good practice name to profile which make life easy remember this profile is local to you computer and name must be unique in nature

Get-AWSCredential -ListProfileDetail

this command show all current profile in your system sometimes it's also shows profile “default” its a profile already configured by aws but it is not advised able to edit that profile so let him as it is in my case it show myec2 profile which I created early.

Setting up default profile and region

It's always good idea to setup default profile and region so whenever you start powershell its not give error and also it decreases the length of code.

Powershell did not give any command to show the currently set default profile becauses AWS powershell is made like this that it is very easy to use multiple profile by just using profile name which we created early to set default profile use

Set-AWSCredential -ProfileName [name of profile]

This set default profile to profile name you have given early

Now you are thinking that it’s the same command we use early yes this command serve two function first creating new credential object and setting up the default credential

To show default aws region use

Get-DefaultAWSRegion

If you are new this is did not give any output it means there is no default region currently set now to set default region use

Set-DefaultAWSRegion -region [region id]

If you are not sure what is your region id you search it on google or use

Get-AWSRegion

command to show all the region

if your region it not showing in the output it means command is not update don’t worry you can use that particular region powershell did not give any error

now we are done now its time to do some task using powershell to show its power

List of task

Here the list of task that we are going to do

  • Creating a key pair
  • Creating a security group
  • Launhcing an instance using the above created key pair and security group.
  • Creating an EBS volume of 1 GB.
  • The final step is to attach the above created EBS volume to the instance you created in the previous steps.

Task one creating a new key pair

To create a new pair in aws use the following command

New-EC2KeyPair -keyname thiskeyisnew

this command show the following output

KeyFingerprint:- is used to identify two uniquely key without using the actually content of the key remember KeyFingerprint is sensitive information don’t share with anyone

KeyMaterial:- 2048-bit RSA key don’t share with anyone always copy and save in a file with .pem extension just like i did i am using notepad++ as my default text editor you can use any text editor you like.

KeyPairId:- unique id given to every key pair if you wish save the store in some place.

Task two creating Security group

To create a new security group in aws use the following command

New-EC2SecurityGroup -GroupName newsecgroup -Description this-security-group-created-using-powershell

this command show the following output

remember both name and the description of the security group is required if you not give it gives you a error

copy the security group id and save it some where in case my id is sg-02a01e91c5cb5ffa4

Task three Launching an instance using the above created key pair and security group.

Two launch an instance in aws use following command

New-EC2Instance -ImageId ami-0e306788ff2473ccb -InstanceType t2.micro -SecurityGroupId sg-02a01e91c5cb5ffa4 -KeyName thiskeyisnew

ImageId:- is the unique id given to every AMI(Amazon Machine Image) you can google it to find the AMI id in my case i am using Amazon Linux 2 AMI

InstanceType:- is type of instances we are allocating to an instance in my case is t2.micro

KeyName:- is the name of the key we created early remember if you give the name of the key that is not exists it give error and if you not use any key name aws did not attach any key pair to it in that case you are not able to connect to instances

SecurityGroupId:- this is the Security Group Id which is we created early if you did not give any security group id aws use default security group

copy the reservation id for further references in my case is r-05380a3d3c96888c8

Task Four Creating an EBS volume of 1 GB.

Two create an EBS volume in aws use following command

New-EC2Volume -AvailabilityZone ap-south-1a -VolumeType gp2 -Size 1

AvailabilityZone:- is compulsory to give provide AZ in my case is it Mumbai you can google it to find your AZ

VolumeType:- is not required you can also omit it if you omit it automatically select gp2

Size:- is it required to provide volume size in GiB in my case I provided 1GB of volume remember always use integer if you give decimal value like 1.4 or 0.9 its give error

copy volume id for further references in my case is vol-0e75a1c770a162f4c

Task Five attach EBS to an instances.

For no good reason powershell never provided instances id directly with any command but we can use reservation id print more detail about instances to that use following command:-

(Get-EC2Instance -Filter @{name=”reservation-id”;value=”r-05380a3d3c96888c8"}).Instances | Format-List

note:- the above image not shown full output because its to big fit in screen

Get-EC2Instance:- its used to print information but that command alone did not print instance id

Filter:- its use to filter instances in based on some parameter it very useful when you have 100s of instance and find only instances that uses particular AMI. .Instances in the tell to fetch the information we are asking is belong to an instance

| :- this symbol is know as pipe(|) is used to give output of one command to another command in my our case we give output to Format-List

Format-List:- to print data in list if you don’t use list format it use table by default table in powershell omit extra column which did not fit in the screen in my case is show instances id but this is not always and sometimes is omit required information so is always recommended to use Format-List.

copy instances id for future use in my case is i-07850653c13b782c9

Finally to attach volume to EC2 instances use the following command

Add-EC2Volume -VolumeId vol-0e75a1c770a162f4c -InstanceId i-07850653c13b782c9 -Device /dev/sdh

VolumeId:- its the id of volume we want to attach

InstanceId:- its the id of instance we want to attach remember both instance and volume is must in same AZ or not it gives error if

Device:- is name the name of the device we want to give according amazon you can give name from sdf to sdp but don’t worry you can give any name from sde and saz also it only give error when you have attach volume same name.

note:- who guys don’t know what is meaning of /dev/sdh from previous example is a quick summary for this

  • /dev is the folder in which all information about device is store
  • /sd is name of hard disk
  • h is the number of hard disk h means 8 and i means 9 it's not important to have a-g before creating h you can directly create say z or if you want to goes beyond sdz you can use sdaa, sdab up to sdzzz(which is equivalent 18278 according linux official documents)

so in summary /dev/sdh means hard disk number 8(which is equivalent to h)

note:- amazon instances did not use /dev/sdh internally it’s uses /dev/xdvh (xdv stands for xen virtual disk number 8(which is equivalent to h))

Things to remember when using PowerShell

In the end here are some thing which you have to remember when using powershell

  • powershell command are not case sensitive it means New-EC2Volume can be written as new-ec2volume or like this nEw-eC2voLume as long as the spelling is write powershell take care of it
  • just like command parameter is also not case sensitive means -Device can be written as -device or like this -deViCe as long as the spelling is write powershell take care of it
  • value of parameter case sensitive means /dev/sdh is not equivalent to /dev/Sdh and it may throw error in some case
  • tab completion:- powershell support tab completion for every command for Get-EC2I[then press tab key] its automatically show every command the start with Get-EC2I. its also correct the case of the command
  • like command parameters also support tab completion

most of the AWS powershell command support some common parameters

  • (-AccessKey, -SecretKey):- when you want to use another account
  • -Region:- when you want do some work other than default region
  • -ProfileName:- when you want to use profile other than default profile you can create any number of profile using Set-AWSCredential

Getting help

Find help of any command is not much difficult in powershell you can use

help [command-name] for eg

help New-EC2Volume

give the help about New-EC2Volume command you can also use some parameter:-

  • -Full:- this parameter show the full help of that command along all the parameter its support it’s uses and whether is required or not
  • -Example:- this parameter show the example of that command
  • -Online:- this parameter show open help file in default browser you must have internet connectivity for this parameter

Syntax of command

Every AWS powershell follow a syntak which is defined below:-

  • Action:- every command start with an action for eg [get, new, remove, edit]
  • Separator:- every action is separated by dash(-)
  • Service-Name:- then come service name for eg(ec2, s3, lm(for lamda))
  • Action-Inside-service:- then come Action Inside service what thing we have to do inside that particular service for eg (want to launch new instance, delete, edit, list all the instance)

This syntax become very easy when you start working with powershell for some week in different services and able to see pattern more easily

Closing Note

Thank for everyone to read my article till end if you have any doubt please comment if you have any suggestion please mail all comment both positive and negative is more than welcomed

I am consistency using word command but fact they are not command(cmd) they are command let(cmdlet) here are some different between cmd and cmdlet:-

  • Cmdlets are instances of .NET Framework classes they are not stand-alone executables.
  • Cmdlets do not generally do their own parsing, error presentation, or output formatting. Parsing, error presentation, and output formatting are handled by the Windows PowerShell runtime (i.e the reason why they are not case sensitive).

Contact Detail

LinkeDin [https://www.linkedin.com/in/suyash-garg-50245b1b7]

Additional Tags

#awscloud #awscli #aws #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #arthbylw #awsbylw

--

--

Suyash garg
Suyash garg

No responses yet