There’s a project that I’m on that allows me to work in AWS. It’s pretty important stuff at work, and since there’s an AWS PowerShell module, I couldn’t be more interested. As of today, I’m going to start to include worthy AWS PowerShell work, right here. As I become more involved, so will my blog.
I’ve said it a bunch of times now, but this site isn’t just about helping others—although that’s a big part— it’s about giving myself a place to store the things, I think I’ll want one day. With over 2,000 cmdlets in version 3.3.0.0 of the AWSPowerShell module, I’m going to need a place to store this, and I’ll take any help I can get in retaining this information. Writing helps with that; you know, the whole writing-to-remember concept.
So, here’s the error I was up against today:
“Your quota allows for 0 more running instance(s). You requested at least 1 – You can request an increase of the instance limit here: http://aws.amazon.com/contact-us/ec2-request/.”
As relayed by our upcoming, but practically already there, AWS expert, “Each instance size (t2.small, c3.large, etc.) has a separate quota in each region in AWS.” The decision to do this was preventative in that we wouldn’t accidentally spin up too many instances and cost ourselves dearly. I get that.
I’m a curious type, so I couldn’t help but wonder, how many of each instance type do we have in the current AWS account? I switched over to my PowerShell console and started writing. This is straightforward and basic PowerShell, but it still seemed like something worth keeping around and sharing, as well.
$Instances = Get-EC2Instance
Foreach ($Instance in $Instances) {
[array]$Types += $Instance.Instances.InstanceType
}
$Types | Group-Object -NoElement
The example works this way: Line 1: Capture all the EC2 instances, Line 3: Begin to iterate over the returned collection. With each pass, in line 8, append the current instance’s instance type to the $Types
variable—an array. Finally, in line 7, we group the $Types
variable in that it’ll automatically provide the Count property for each Instance Type.
I’ve included a slight modification in the example below. In this instance, I didn’t cast the $Types
variable as an array and instead created the variable as an empty array prior to the Foreach
. It’s the same end goal, however, I wanted to highlight how others might be inclined to write something similar.
$Instances = Get-EC2Instance
$Types = @()
Foreach ($Instance in $Instances) {
$Types += $Instance.Instances.InstanceType
}
$Types | Group-Object -NoElement
If you run this repeatedly, you’ll quickly realize that it’ll continue to add to the $Types
variable, thus making the results incorrect as soon as the second run. You can add this as the last line: Remove-Variable -Name Type
s, or better, make it a function.
Function Get-EC2InstanceTypeCount {
<# #>
[CmdletBinding()]
Param (
)
Begin {
$Instances = Get-EC2Instance
} # End Begin.
Process {
Foreach ($Instance in $Instances) {
[array]$Types += $Instance.Instances.InstanceType
}
$Types | Group-Object -NoElement
} # End Process.
End {
} # End End.
}
Remember, you’re going to need to have already used Set-AWSCredentials
and stored a set of credentials in a persistent store. This function is dependent, just like any AWS cmdlet, on there being a set of stored credentials. I did not write the function to accept an AccessKey and SecretKey parameter, as this isn’t a recommended scenario in much of the documentation I’ve read from AWS. Here’s the function—with comment-based help—if it might be useful for your team.
Function Get-EC2InstanceTypeCount {
<# .SYNOPSIS This advanced function will return the AWS Instance Types used in an AWS account. .DESCRIPTION This advanced function will return the AWS Instance Types used in an AWS account. It will return a name property, such as t2.medium, m4.large, etc., and a Count property. The results are sorted on the count, as they are produced using the Sort-Object cmdlet. .EXAMPLE PS > Get-EC2InstanceTypeCount
This example returns the EC2 Instance Types and how many of each are being used.
Count Name
----- ----
32 t2.medium
18 t2.micro
6 c3.large
6 m4.large
7 t2.small
2 r3.large
4 r3.xlarge
5 g2.2xlarge
5 t2.large
1 t2.nano
.EXAMPLE
PS > Get-EC2InstanceTypeCount | Sort-Object -Property Name
This example returns the EC2 Instance Types and how many of each are being used, sorted by Name.
.EXAMPLE
PS > Get-EC2InstanceTypeCount | Where-Object -Property Name -like *large*
This example returns the EC2 Instance Types that include the term "large" in the Name property.
Count Name
----- ----
6 c3.large
6 m4.large
2 r3.large
4 r3.xlarge
5 g2.2xlarge
5 t2.large
.NOTES
NAME: Get-EC2InstanceTypeCount
AUTHOR: Tommy Maynard
COMMENTS: --
LASTEDIT: 09/27/2016
VERSION 1.0
#>
[CmdletBinding()]
Param (
)
Begin {
$Instances = Get-EC2Instance
} # End Begin.
Process {
Foreach ($Instance in $Instances) {
[array]$Types += $Instance.Instances.InstanceType
}
$Types | Group-Object -NoElement
} # End Process.
End {
} # End End.
}