The "hack" you described is the correct way to retrieve these in AWS CLI. As a matter of fact, this is what the PowerShell Tools' Get-EC2ImageByName
does behind the scenes; it maps a raw AMI name (exposed by ShowFilter
parameter) to a pre-defined name pattern, exposed by AllAvailable
parameter.
You can see this by listing the ShowFilter
parameter; the first result matches the name value you've listed:
C:/ > get-ec2imagebyname -ShowFilters
Windows_Server-2012-R2_RTM-English-64Bit-Base*
Windows_Server-2012-R2_RTM-English-64Bit-SQL_2014_SP1_Express*
Windows_Server-2012-R2_RTM-English-64Bit-SQL_2014_SP1_Standard*
Windows_Server-2012-R2_RTM-English-64Bit-SQL_2014_SP1_Web*
Windows_Server-2012-RTM-English-64Bit-Base*
...
To get only the latest Windows 2012 R2 AMI ID back from AWS CLI, sort your query by CreationDate and limit to only the last result.
Example:
aws ec2 describe-images \
--owners 'amazon' \
--filters 'Name=name,Values=Windows_Server-2012-R2_RTM-English-64Bit-Base*' \
--query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
--output 'text'
Output:
ami-11e84107
Further Reading