Is there a way to get the latest Windows 2012R2 base AMI id using aws cli?
Something like Get-EC2ImageByName -Names WINDOWS_2012R2_BASE
in Powershell. I want to use it in Linux.
I have tried fetching AMI id with
aws ec2 describe-images --owners amazon --filters "Name=name,Values=Windows_Server-2012-R2_RTM-English-64Bit-Base-*"
but it feels like a hack. Is there a better way to do this like in Powershell?
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