5

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?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Idris Wishiwala
  • 151
  • 1
  • 7
  • nop, using the CLI, its the best method as documented by aws http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html#finding-an-ami-aws-cli - the powershell has added additional method – Frederic Henri Mar 28 '17 at 12:56

1 Answers1

6

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

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129