Get the latest Windows 2012R2 base AMI id using aws cli? - amazon-web-services

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
Further Reading
AWS CLI Documentation - describe-images
AWS PowerShell Documentation - Get-EC2ImageByName
AWS Documentation - Controlling Command Output from the AWS Command Line Interface

Related

AWS CLI Command to list latest 7 ami' s and there snapshot

I need a cli command to list 7 latest ami's and there snapshot.
Based on Fetch last modified object from AWS S3 CLI, you should be able to limit the result set with:
aws ec2 describe-images --owners self --query 'sort_by(Images, &CreationDate)[-7:].ImageId' --output text
You should be able to list the associated snapshots with something like this (but I didn't test it):
aws ec2 describe-images --owners self --query 'sort_by(Images, &CreationDate)[-7:].[ImageId, BlockDeviceMappings.Ebs.SnapshotId]' --output text
That should give you an idea of how to access the fields in the result set. Use the documentation to identify the desired fields.

Finding the full name of windows server

This command lists hundreds of windows servers. How do I select the most popular ones those are displayed on web console while I create a new instance?
# aws ec2 describe-images --owners amazon --filters "Name=name,Values=Windows_Server*" --query 'sort_by(Images, &CreationDate)[].Name'
[
"Windows_Server-2016-English-Full-ECS_Optimized-2017.11.24",
"Windows_Server-2016-English-Full-ECS_Optimized-2018.01.10",
"Windows_Server-2016-English-Full-ECS_Optimized-2018.02.21",
"Windows_Server-2016-English-Full-ECS_Optimized-2018.03.26",
"Windows_Server-2016-English-Nano-Base-2018.04.11",
...
...
]
I am looking for the full name and not just the ami-id.
For e.g. which one of the above is "ami-04ca2d0801450d495"?
The DescribeImages API call returns the name of the AMI along with the rest of the info. To extract just the name of the AMI, you can run the following command:
aws ec2 describe-images --image-ids $IMAGE_ID \
--output text --query 'Images[*].Name'
Details about the describe-images command can be found here.
This command will return the full name of the given ami ID
aws ssm get-parameters-by-path --path "/aws/service/ami-windows-latest" --region us-east-1 | grep -C3 '04ca2d0801450d495'

How to get latest EBS volume Id with specific tag using aws cli?

I have to query my AWS account to find latest created volume with specific tags and should have it attached to running EC2 instance. How do I achieve this using aws cli and powershell?
I was using below powershell script and aws cli to achieve this, But I was not able to find out exact query to get latest volume id using aws cli command to replace variable $volumeid.
Any help would be appreciated.
$volumeid = "aws ec2 describe-volumes --region us-east-1 --filters Name=tag:Application_Name,Values=APPone Name=tag:Name,Values=APP_test --query "Volumes[*].{ID:VolumeId}"
$instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
Add-EC2Volume -VolumeId $volumeid -InstanceId $instanceId -Device xvdf -Region us-east-1
You can use the following query to fetch the latest volume id using the specific tag :
aws ec2 describe-volumes --query Volumes[*].[VolumeId] --filters Name=tag-value,Values=testvolume --output text
The above query first describes the volume descriptions of all the volumes.
Then the "--query" returns only the volume ids of all the volumes present.
Later it filters out the volume id based on the Tag you specify.
And the "--output text" converts the output in text format which you can store in the variable.
For more details you can refer describe-volumes
This should help.

How to create EC2 instance without given Image-Id in AWS template?

I want to create a RHEL OS EC2 Instance using AWS template. I don't have any RHEL instance currently. So don't have any Image-ID.
Red Hat maintains RHEL AMIs. We can use the CLI describe-images to query their public AMIs based on:
Account ID: 309956199498
A known string pattern matching their AMI names: RHEL-*_HVM_GA-*-Hourly2-GP
For the sake of this example, we will sort the images by CreationDate, request only the last element in the collection (via -1) and filter the results down to Name, ImageId, and CreationDate.
Example:
aws ec2 describe-images \
--owners 309956199498 \
--filters "Name=name,Values=RHEL-*_HVM_GA-*-Hourly2-GP2" \
--query 'sort_by(Images, &CreationDate)[-1].[Name, ImageId, CreationDate]' \
--output text
Output:
RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2 ami-b63769a1 2016-10-26T22:32:29.000Z
To verify this is correct, you can double-check by visiting the 'Quick Start' section of the AWS Console's EC2 'Launch Instances' Wizard and checking the most recent RHEL AMI that is sorted near the top of this quick start listing. At the time of this posting, the most recent RHEL AMI was ami-b63769a1.
After this, you would take that resulting ImageId and use it as part of your request to launch a new instance.
Further Reading:
AWS Documentation - Finding a Linux AMI

EC2 instance from AWS command line

I am very new to AWS. I've got a Windows instance running and have my aws command line configured. I've read through the AWS docs but can't seem to find exactly what I'm looking for.
How do I view my current instances from the command line?
If by view your current instances, you mean list all running instances from the command line, you can call the describe-instances command:
aws ec2 describe-instances
This will list all of your current instances.
See describe-instances
As noted in the answer by Rodrigo M, you should use describe-instances to view your EC2 instances. In general, the help command is the best way to explore the CLI. Start with aws ec2 help and try the various options. You can get more details on subcommands with aws ec2 describe-instances help as well.
The output is a bit verbose and by default in JSON. This can be a bit overwhelming and hard to read without additional processing. I'd recommend getting familiar with the --query aws CLI parameter if you intend to use the CLI interactively.
In particular, I use this for a quick overview of my EC2 instances:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`] | [0].Value, State.Name, PublicDnsName]' --output table
To check one particular attribute on an instance:
aws ec2 describe-instances --query Reservations[0].Instances[0].InstanceType --output text --instance-ids <my-instance-id>
The CLI is very powerful once you get comfortable with learning the commands and managing the output. It's also helpful for learning the porgramming APIs as well, since aws CLI commands generally map one-to-one with an API call.