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

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

Related

How do I upgrade my AWS EC2 instance using the AWS CLI?

I would like to increase the number of CPU cores in my EC2 instance, using the AWS CLI.
My instance is currently a C5.4 Large.
I don't know the command for this, and I don't know if I have to know the instance type I want to switch to beforehand, or if I can browse different instance types from the AWS CLI.
aws ec2 help lists the commands describe-instances, stop-instances, modify-instance-attribute, and start-instances.
Calling aws ec2describe-instances may provide too much information. You can select the fields you would like with the --query option. --query takes a "JMESPath", which stands for "JSON Matching Expression paths" - a set of special syntaxes for getting values out of complicated JSON.1
You need enough information to identify the instance, for example, its name and type. Start the JMESPath with Reservations[*].Instances[*], followed by [InstanceId, InstanceType, KeyName].2
Example:
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId, InstanceType, KeyName]' \
--region us-east-1
Copy the ID.
Stop the instance before upgrading it:
aws ec2 stop-instances \
--instance-ids "$INSTANCE_ID"
It can take 5-10 minutes for it to stop.
Change the instance type with modify-instance-attribute:
aws ec2 modify-instance-attribute \
--instance-id "$INSTANCE_ID" \
--instance-type "{\"Value\":\"$REQUESTED_TYPE\"}
Replace $REQUESTED_TYPE with the name of a type, for example, t2.small.3
There may be limitations on what kinds of instances are allowed for your account, so make sure it's an instance type you have access to.
Now, restart the instance and you're done:
aws ec2 start-instances \
--instance-ids "$INSTANCE_ID"
You'll have to wait a few minutes before the instance has finished booting up.
1 Here is a tutorial on using JMESPath.
2 Every time you call an ec2 command, it is important to specify the region with the option --region. If you don't query a specific region, you won't see your instances.
3 (The quotes are escaped with a backslash so your shell doesn't misinterpret them.)

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.

Do AWS charge licence fee for an image based on RHEL AMI

The problem is I bake an AMI with Packer and I use RHEL as a source.
When the image is being baked AWS charge for the Packer VM on pay-as-you-go basis, but when that image is ready and I start using it, do AWS know that it’s based on RHEL? Do they still charge licence fee for it?
do AWS know that it’s based on RHEL
Yes exactly, no matter if created it using a packer, as it still bases on some AMI same like Docker base image.
You can check the platform info using aws cli
aws ec2 describe-images --region us-west-2 --image-ids ami-03c752ed9 --query 'Images[].PlatformDetails'
or for running instance you can check with
You can check the AMI using AWS CLI bypassing the newly created instance
EC2_ID=i-042dbcf65c7bdc3d4 aws ec2 describe-images --image-ids $(aws ec2 describe-instances --instance-ids $EC2_ID --query 'Reservations[0].Instances[0].ImageId' --output text) --query 'Images[0].Name'
Do they still charge a licence fee for it?
This is something that depends on the source AMI
aws ec2 describe-images --region us-west-2 --image-ids ami-03c752ed931sdf5 --query 'Images[].ProductCodes'
You can find price details here for AMI if it from the market place if the source AMI is community AMI then it will be free.
You can also check further details about RedHat AMI in AWS redhat FAQ

Get the latest Windows 2012R2 base AMI id using aws cli?

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

Get information about new instances spawned by auto scaling using CLI

I am working on creating a monitor dashboard for monitoring status of ec2 instances.
I am searching for a method to get information (especially instances ID) of newly spawned instances using auto scaling.
Can anyone point me in the right direction. Thanks
If you know your instance type then you can use describe-instances command to get details about instances and use query command to get the details what you need (in your case Instance-id)
aws ec2 describe-instances --filters "Name=instance-type,Values=t1.micro" --query 'Reservations[*].{InstanceId:Instances[0].InstanceId}'
## Enter your instance type in the 'Values' field of '--filters' command
I was able to get instance id by using combination of following commands
aws elb describe-load-balancers --load-balancer-name "LoadBalanceID" --region "region" --output text | grep INSTANCES
Using the AWS CLI you can get a list of scaling activities for an auto scaling group.
aws autoscaling describe-scaling-activities --auto-scaling-group-name my-group-name
See AWS CLI
This is the newer Python CLI, so you would need to install that if you have not already done so. It will return a JSON block with all of the scale up and down activities in the group, including the reason an the date and time.