Get the latest Windows 2012R2 database AMI using aws cli?

Is there a way to get the latest Windows 2012R2 AMI using the aws cli?

Something like Get-EC2ImageByName -Names WINDOWS_2012R2_BASE

Powershell. I want to use it on Linux.

I tried to get the AMI from aws ec2 describe-images --owners amazon --filters "Name=name,Values=Windows_Server-2012-R2_RTM-English-64Bit-Base-*"

, but it seems like a hack. Is there a better way to do it like in Powershell?

+3


source to share


1 answer


The "hack" you described is the correct way to retrieve them in the AWS CLI. In fact, this is what the PowerShell Tools do Get-EC2ImageByName

behind the scenes; it maps the original AMI name (as exposed by the parameter ShowFilter

) to a predefined name template displayed by the parameter AllAvailable

.

You can see this by specifying a parameter ShowFilter

; the first result matches the given name:

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 from the AWS CLI, sort your request with CreationDate and limit yourself 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

      

additional literature

+4


source







All Articles