I have a lab environment in Amazon Web Services (AWS) that consists of four small EC2 instances. When I’m not using the lab, I shut the instances down and then start them up when I need them. When I do this, they get new public IP addresses, and I need to know those addresses in order to connect to them. I wanted to simplify this whole process by using the AWS command line utility (CLI).
There were five different commands that I wanted to run:
- Get the instance IDs of my four servers
- Check to see if the instances were running
- Start the instances
- Stop the instances
- Get the public IP address of the running instances
This seems like a common scenario and should be straightforward, but it took a little more time than I thought to get the commands right. So I thought it might be helpful if I put these together in a short post to help others who want to do the same thing.
This isn’t going to be a deep dive into the AWS CLI, and I’m assuming that you already have the AWS CLI installed where you need it and have the access you need to run these commands. This post is going to focus on getting the data and managing the instance states. Let me caveat this by saying there are probably other ways to do this, and other options for formatting or filtering the output, but these work, so they’ll get you started.
In my scenario, I tagged my four instances with the clever names servera, serverb, serverc, and serverd. I’ll use these names to filter down my list, rather than seeing every instance in my environment. Also, all of the five things I want to do use the “$ aws ec2” command. You might want to start by just running
$ aws ec2 describe-instances
It will give you a lot of data back, and you’ll see that you need to filter through that, and that the data is formatted. Let’s begin.
First, we need the instance IDs. Here’s the command I ran, and the output:
$ aws ec2 describe-instances –filters “Name=tag:Name,Values=server*” –query “Reservations[].Instances[].InstanceId”
– i-01dz16q4y49x813r5
– i-072296h3w126fgh17
– i-0v8765aba1po6e93n
– i-03456abcdzx213nm7
(Note: I’ve garbled up my instance IDs and used some invalid entries so that I’m not accidentally using someone else’s instance IDs.)
In the command above, I’m filtering on the “Name” tag, and looking for any instances that start with “server”. That pulls my four instance IDs. While it’s great that I can get that, let’s add a little more so that I know which instance ID goes with which server, and whether that instance is running or not. I used this command to do that:
$ aws ec2 describe-instances –filters “Name=tag:Name,Values=server*” –query “Reservations[].Instances[].[Tags[?Key==’Name’],InstanceId,State.Name]” –output text
i-01dz16q4y49x813r5 running
Name servera
i-072296h3w126fgh17 running
Name serverb
i-0v8765aba1po6e93n stopped
Name serverc
i-03456abcdzx213nm7 stopped
Name serverd
Now I’ve got something. My instance ID is associated with the name that I’ve tagged my instances with, and I can see that two of them are running and two of them are stopped. Next, let’s start up another instance. It’s pretty easy to do if we have the instance ID, so let’s start serverc with this command:
$ aws ec2 start-instances –instance-ids i-03456abcdzx213nm7
StartingInstances:
– CurrentState:
Code: 0
Name: pending
InstanceId: i-03456abcdzx213nm7
PreviousState:
Code: 80
Name: stopped
The output shows that the state is changing from stopped and has moved to pending. Conversely, the command to stop the instance is very similar, as is the output.
$ aws ec2 stop-instances –instance-ids i-03456abcdzx213nm7
StoppingInstances:
– CurrentState:
Code: 64
Name: stopping
InstanceId: i-03456abcdzx213nm7
PreviousState:
Code: 16
Name: running
Now that I’ve got my EC2 instance started, I’ll need the public IP address to connect to it. Here’s the command I used for that:
$ aws ec2 describe-instances –instance-ids i-03456abcdzx213nm7 –query “Reservations[].Instances[].PublicIpAddress”
– 3.45.123.366
(Note: Again, the IP address is made up and actually invalid.)
In this post, I’ve described five different commands to get useful information about your EC2 instances and to start and stop those instances. While there are probably other viable ways to do this, these work well, and won’t overwhelm you with output when you’re just trying to get your instances running.
Want to learn more?
We’re an AWS Advanced Consulting Partner offering secure, easy-to-use, flexible, and scalable application hosting and architecture solutions to set you up for success. Our AWS cloud experts specialize in cloud strategy, migration approach and methodolgy, big data, DevOps, and managed services. Contact us to learn more about how to get started with AWS.