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.
$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
$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
$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 ]
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
Help Get-EC2Snapshot -Parameter filter
I hope this will help you as well to boost up your AWS Administration tasks via PowerShell.
The Credential-Free Watchdog: Mastering Event-Driven App Automation
We have all been there. You are an automation lover. You have…
Ditching PsExec – Running Interactive SYSTEM Shells Natively in PowerShell
If you’ve spent any time in Windows System Administration over the last…
From The Blinking Cursor to The Thinking Machine: A Memoir of Automation
There is a specific kind of silence that only exists in a…
Mange SOLIDserver EfficientIP IPAM form PowerShell
Recently I worked on an automation for cloud infra, where I had…