Generating configuration for an EC2 instance using Amazon AWS CLI - amazon-web-services

I have an existing EC2 instance myEC2 up and running.
How do I use the CLI to generate the configuration for this so that I can run a command in my CLI window that will create a new identical EC2 with the name myEC2_B?
Amazon documentation is a disaster as far as I can see. Seems to assume a certain level of knowledge for every topic

I'm not sure about how to do this from the command line interface, but it's certainly possible (and quite easy, I might add):
Jump over to your Instances page.
Find and select the instance that you want to duplicate:
Under the actions menu, click create image
Give the image a name, etc. in the dialog. If you are in a dev environment and can shutdown the server, you should probably tick the no reboot box. Otherwise, leave it unticked. Wait while it's created.
Head over to the AMIs option in the sidebar:
Find your newly-created image and tick it:
Click the big blue launch button at the top of the screen. You'll get to choose an instance size, etc.
Enjoy your duplicated server ;)

You would need to do at least a two step process, but lets look at how we would do it all from the console.
list our instances along with their name and instance id:
aws ec2 describe-instances --filters Name=vpc-id,Values=* --query 'Reservations[].Instances[].[PrivateIpAddress,InstanceId,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/ /'
I get a nice spit out of my instances, IP and instance ID along with the name, take note of the instance we want and its instance-id
Now we are going to write that Instance to a new ami using the create-image ec2 sub command:
aws ec2 create-image --instance-id="MYINSTANCEID"
Now we have the AMI (image-id) we simply launch new ec2 instance with the ami id like so:
aws ec2 run-instances --image-id="ami-d13513b2" --key-name="jjs-skyramp" --security-group-ids="sg-daa5c5bf" --instance-type="t2.small"
Of course if you did this all too quickly you will get the following:
Which is telling us the AMI we created before is still pending, so might want to wait till thats finished, and I assure you the command will copy your Imaged machine :)
You can see the commands I used, what I used represents the bare minimum to launch an image as a new ec2 instance, you can see all the other options at: https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html
Its easy to get your id's for each appropriate paramater by simply running:
"aws ec2 describe-THING-I-WANT-TO-SEE
then play some with filters for the json data.
Hope this helps.

Related

List all running EC2 instances without any snapshots

Does anyone know of an AWS CLI command that will list any running instance (run against a particular region) that doesn't have a snapshot available.
The closest command Ive found to try would be something like:
aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[]' --region=us-east-1
I didn't actually get any return on it - just:
-------------------
|DescribeSnapshots|
+-----------------+
This is supposed to name every EC2 snapshot for each instance -- so I would have to subtract these ones from the entire EC2 inventory to reveal EC2 instances without.
Hence - I would like a command that would show running EC2 instances without any snapshots available -- so I can put something in place going forward.
Amazon EBS Snapshots are associated with Amazon EBS Volumes, which are associated with Amazon EC2 instances.
Therefore, you would need to write a program using an AWS SDK (I'd use Python, but there are many available) that would:
Obtain a list of all EBS Snapshots (make sure you use the equivalent to --owner-ids self), in which the return data will include the associated EBS VolumeId
Obtain a list of all EBS Volumes, in which the return data will include Attachments.InstanceId
Obtain a list of all running EC2 instances
Do a bit of looping logic to find Volumes without Snapshots, and then determine which instances are associated to those Volumes.
Note that rather than finding "instances without snapshots" it has to find "instances that have volumes without snapshots".
I don't think there is by default a CLI command that will allow you to do this. You can tag your snapshots with your instance ids for example then can query snapshots by filtering on the tags. Or you will have to use AWS SDK and create a custom script to allow you the get all instances and then check their volume ids if they have snapshots created or not.

Difference between InstanceID and ImageID

I am currently working on some IAM policies on AWS and being asked to specify Instance and Image ID. I am not sure what is the difference between the two. From what I understand and instanceID is the one that starts with ami-XXXXXX. Correct me if I am wrong, if that is the case where can I find imageID.
You can achieve this in following ways :-
First make sure you are using the new EC2 Experience ( these are applicable on updated UI)
Now log in to your console.
Go to EC2 instances.
You will see resources, under resources click on instances (all states).
Note :- You have chosen the right region.
Click onleft check box for the instance you want to find details ; in the bottom of the screen under Details you can find instance id starting with i-*** and further scroll it you will get ami id starting with ami-*** also.
The ID of an Amazon EC2 instance looks like: i-xxxx
The ID of an Amazon Machine Image (AMI) looks like: ami-xxxx
When an Amazon EC2 instance is launched, the contents of the AMI is copied to the disks of the new instance.
imageId often starts with "ami-" and you can find it in EC2/Images/AMIs
instanceId is the id of instance, you can find it in list of your instances EC2/Instances/Instances->Instance ID field

need to copy the Userdata script from one ec2 instance to another ec2 instance

I am working on a script through which I can create an instance and has the same setting (like vpc, subnet ID, Security group and user data) from another existing instance.
I am able to get all the setting except user data as user data needs to be saved then can be passed to another instance. But when I saved the user data, it is saved in Jibrish language. I used this command : (Get-EC2InstanceAttribute -InstanceId i-sda23dsf -Attribute userData).UserData > userdata.txt
so how can I pass it to another instance so that both can have same user data script.
Userdata is available at
http://169.254.169.254/latest/user-data
if you absolutely want to continue down the route you're on.
It would make far more sense to create both instances using an orchestration language like ansible or puppet or terraform, which would give you reproducibility and allow you to make changes to various parts of your infrastructure on the fly.
If you still have access to the original instance, even if that instance is stopped, try to select the instance, then in "Actions", do "Launch more like this" and continue until you reach "3. Configure Instance" screen. Under "Advanced Details" you'll find the original UserData.
No need to actually launch "new" instance.
You don't do it that way, the user data script is run once the first time an EC2 is created.
What you're talking about sounds like "Baking an AMI" or "making a Golden Image".
This is handy for user data scripts that take more than a minute in that you can spin up the AMI with everything pre-installed. This is also ideal for multiple EC2s that have the same userdata:
The professional way is to automate the AMI creation using a tool called Packer.
Another common way people share userdata scripts between EC2s is have the userdata download a script from an S3 bucket and then execute it. This is perfect for userdata scripts larger than the 16500 byte limit.

Register an instance to an AWS ECS cluster

I created an EC2 instance on AWS to use as an ECS instance. I followed these steps here to do that.
I also created a new Cluster under ECS but for some reasons, I cannot see the instance under the cluster:
Any ideas on what might be the issue here?
I found the missing piece. It was stated here as part of 10th item on the list that:
By default, your container instance launches into your default cluster. If you want to launch into your own cluster instead of the default, choose the Advanced Details list and paste the following script into the User data field, replacing your_cluster_name with the name of your cluster.
I would strongly suggest letting ECS Cluster create its own EC2 Instance(s), especially if you are new to this. You can define the type of instance that you want when you create the cluster and everything will magically work.
Doing it the other way around (EC2 Instance first, then feeding/attaching it into the Cluster) might sound quite natural to you, but it means that you have to handle a lot of things by yourself. You have to manually spend time on
Making sure you pick the right AMI (ecs-optimized),
Making sure the VPC-Subnets are right,
Making sure the architecture is right (e.g. t4g medium instances don't take x86 containers),
Making sure the Cluster Name is in /etc/ecs/ecs.config and then after that restart docker & ecs-agent.
Making sure ecs-agent is properly set up and connecting,
... and blah blah blah.
Maybe, and hopefully, in future they will add an "attaching this EC2 Instance to that Cluster" button that does all these chores. But until then, save yourself this list of headaches and try to get the instance created by the cluster.
I already had a cluster configured, so the easiest way to do this for me was to go to EC2 Console > Auto scaling groups and select the autoscaling group for the cluster. Make sure to increase the desired capacity and maximum capacity. This should start a new EC2 instance.

How to find how Amazon AMI created date

Is there a way to find out Amazon EC2 AMI creation date/time from AWS Console or command line?
Note: I am not looking for instance creation, rather when was a particular AMI created?
Querying the creation date of an AMI has been a requested feature since 2006.
Back when I was creating community AMIs in 2007-2009 I started a trend of including the creation date in the AMI name/description (e.g., 20121210). This practice has been picked up by a number of popular AMI series, so you may want to check out the name and description of the AMI for a date looking string.
From AWS's 'What's New' :
AMI Creation Date is now available on the AWS Management Console. You can find this attribute by going to the EC2 console and choosing AMIs on the left navigation menu. You should see a new column on the AMIs page called "Creation Date"
You can also see this 'CreationDate' attribute with AWS EC2 CLI using:
ec2 describe-images --image-ids ami-a3e0thy0
Two alternatives:
1) You can view info about the associated snapshot from the EC2 snapshot panel, specifically the "Started" field which will tell you the time the snapshot started. I think this is very close, and worked well for AMIs my coworkers and I created - not so well for private AMIs. Simply find the snapshot ID (snapshot-1234abcd) in the AMI view in the "Block devices" field, then look up that snapshot in the Snapshots view.
2) If you are in a region which offers AWS CloudTrail, you can probably find that info in the CloudTrail logs.
This is not currently possible. The ability to better track versions of AMIs is a often requested feature though. Expect AWS to be releasing some enhancements in this area relatively soon.
If you want to use the CLI, but only want the image ID and the creation date, you can run this:
aws ec2 describe-images --image-ids ami-XXXXXXXX --query 'Images[*].[ImageId,CreationDate]'