I personally can’t imagine supporting a big infrastructure without introducing automation and to automate something you have to use command line. If you are more into Windows and started working on AWS for your clients, then I assume that you would love to start managing AWS using AWS PowerShell Module like I do. AWS Console is really good and informative but when it comes to bulk activity I am always depending on PowerShell.
There is a very good documentation created by Amazon for AWS PowerShell, you will get the basic help for all the available PowerShell cmdlet from here. It will be very much helpful for you to start working on AWS PowerShell, but unfortunately when it comes to using filter available for those cmdlet, you won’t be able to find any good example. Using filter on a AWS cmdlet will be a smart move and it also reduce the total execution time. Initially I also struggled to get this working due to lack of examples, but later I figured it out, and now I always try to utilize filter and I am writing this to you help you understand how easily we could use this.
Some of the sample, hope this will help you.
Extract the list of the snapshots under your ownership.
1 2 3 | $Filter.Name = "owner-id" $Filter.Value = "XXXXXXXXXXXX" Get-EC2Snapshot -Filter $Filter -Region AWS-REGION |
This script, I am using filter to identify the snapshots mapping with instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $GetAllRegions = (Get-AWSRegion).Region #or Specific Region #$GetAllRegions = "us-east-1","us-west-2","ap-southeast-2" $GetAllSnapshots=@() $GetAllRegions | % { $getSnaps = $null $RG = $_ $SFilter = new-object Amazon.EC2.Model.Filter $SFilter.Name = "owner-id" $SFilter.Value = "XXXXXXXXXXXX " $getSnaps = Get-EC2Snapshot -Filter $SFilter -Region $RG foreach ($getSnap in $getSnaps){ $IFilter = new-object Amazon.EC2.Model.Filter $IFilter.Name = "block-device-mapping.volume-id" $IFilter.Value = "$($getSnap.VolumeId)" $getInstanceID = (Get-EC2Instance -Filter $IFilter -Region $RG).Instances.InstanceID if ($getInstanceID){ $getSnap | Add-Member @{LinkedInstanceID=$getInstanceID} }else{ $getSnap | Add-Member @{LinkedInstanceID="Missing"} } $getSnap | select Description, SnapshotID, VolumeID, LinkedInstanceID $GetAllSnapshots += $getSnap } } $GetAllSnapshots | select Description, SnapshotID, VolumeID, VolumeSize, LinkedInstanceID | Export-Csv $env:USERPROFILE\Desktop\SnapR.csv -NoTypeInformation |
Use multiple filter to extract attached root volumes
1 2 3 4 5 6 7 | $Filter0 = New-Object Amazon.EC2.Model.Filter $Filter0.Name = 'attachment.status' $Filter0.Value = 'attached' $Filter1 = New-Object Amazon.EC2.Model.Filter $Filter1.Name = 'attachment.device' $Filter1.Value = '/dev/sda1' Get-EC2Volume -Filter $Filter0, $Filter1 -Region AWS-REGION |
Extracting instances details from name. [ Use: GetInstanceFromName NAME-OF-THE-INSTANCE ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function GetInstanceFromName([string] $fInstanceName,[string] $fRegion) { $fFilter = new-object Amazon.EC2.Model.Filter $fFilter.Name = "tag:Name" $fFilter.Value = "$fInstanceName" if ($fRegion){ $fCmdOutput = Get-EC2Instance -Filter $fFilter -Region $fRegion } if (!$fRegion){ $fCmdOutput = Get-EC2Instance -Filter $fFilter } if($fCmdOutput){ return $fCmdOutput }else{ return Write-Host "Please make sure you are using correct name in and correct case" -ForegroundColor Yellow } } |
You can use filter in almost every cmdlet, and to get all the available options for filter, use this.
Help cmdlet -Parameter filter
1 | Help Get-EC2Snapshot -Parameter filter |
I hope this will help you as well to boost up your AWS Administration tasks via PowerShell.
Disclaimer: All posts and opinions on this site are provided AS IS with no warranties. These are our own personal opinions and do not represent our employer’s view in any way.
This article currently have 8,115 views
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.