7

I am trying to make use of Amazon's elasticity and bring instances online and off using the AWS cli. But as you know, every time you offline a system, and bring it back to life, it comes with a new IP address. I have AWS-cli package installed on my Centos 6 server, located elsewhere. I have been researching this for days now and am not able to find a working command, that I can issue from my Centos box and get the IP address of the instance at Amazon EC2. EC2 instance is up and running.

Most relevant information I found was

aws ec2 describe-instances

but all I am getting back from this command is something like a usage syntax output. I also found (and promptly lost) a switch --query followed by a set of keywords to extract this information. But that command gave me a response, saying --query is not a recognized argument. I checked the Amazon's cli reference for this command and only parameter it seems to accept is --filter and examples are way far from being helpful.

Does anyone know how to accomplish this ?

EDIT More on the issue I discovered over the weekend. Before my attempt to get the Public DNS info from the instance, I need a way to connect to this instance. I am unable to get information about the instances I have, no matter what I do:

$ ec2-describe-instances i-b78a096f
sanity-check: Your system clock is 50 seconds behind.
+----------------------------+---------------------------------------------+
|            Code            |                   Message                   |
+----------------------------+---------------------------------------------+
| InvalidInstanceID.NotFound | The instance ID 'i-b78a096f' does not exist |
+----------------------------+---------------------------------------------+

I know that my AWS_ACCESS_KEY and AWS_SECRET_KEY variables are correctly assigned to their proper variable names. The instance id has been copied and pasted from the AWS management console. To test, I spun up a new instance and tested the same command against it, with no different result. Although, when I run ec2-describe-regions command, I can see the regions list available to me with no problems. I am dumbfounded right now.

3
  • Flagged to close because the AWS command line tools are not platform specific, nor are any of the problems mentioned here. Apr 22, 2016 at 16:16
  • 4
    I'm voting to close this question as off-topic because this seems to be unrelated to typical UNIX tools
    – schily
    Apr 22, 2016 at 16:45
  • if it's offtopic here, where is it on topic? meta question, I suppose.
    – Thufir
    Jan 4, 2017 at 14:45

2 Answers 2

9
+50

An ec2 instance can be identified by its instance-id, which will never change, no matter how many times you stop and start the instance. So you can try this out if you have an instance id and you need its IP address.

For public IP address:

aws ec2 describe-instances --instance-ids i-b78a096f | grep PublicIpAddress | awk -F ":" '{print $2}' | sed 's/[",]//g'

For private IP address:

aws ec2 describe-instances --instance-ids i-b78a096f | grep PrivateIpAddress  | head -1 | awk -F ":" '{print $2}' | sed 's/[",]//g'

Still, I personally think that it would be much much better if you try the same thing in python. I have implemented same logic in my previous organization with the python boto library and it was much more simple and manageable.

setting up virtual env:

#!/usr/bin/env bash

set -e
HOME_DIR=/home/$(whoami)

#Dependencies
sudo apt-get install ncurses-devel patch openssl openssl-devel zlib-devel

# Install python locally
mkdir -p $HOME_DIR/src
mkdir -p $HOME_DIR/.localpython
cd $HOME_DIR/src
wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar -zxvf Python-2.7.8.tgz
cd Python-2.7.8
./configure --prefix=$HOME_DIR/.localpython 
make
make install

# Install virtualenv locally
cd $HOME_DIR/src
wget --no-check-certificate https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.11.6.tar.gz#md5=f61cdd983d2c4e6aeabb70b1060d6f49
tar -zxvf virtualenv-1.11.6.tar.gz
cd virtualenv-1.11.6/
~/.localpython/bin/python setup.py install 


# Create a test virtual environment
mkdir -p $HOME_DIR/virtualenvs
cd $HOME_DIR/virtualenvs
~/.localpython/bin/virtualenv my_virtual_env --python=$HOME_DIR/.localpython/bin/python2.7
cd my_virtual_env
source bin/activate
pip install awscli
17
  • for the commad you suggested, $ aws ec2 describe-instances --instance-ids i-46eada80 I am getting this response: --instance-ids: mispelled meta parameter? any idea why ?
    – MelBurslan
    Mar 14, 2016 at 13:31
  • @MelBurslan please post the result of aws --version command Mar 15, 2016 at 5:32
  • this link might help too. Mar 15, 2016 at 5:37
  • aws --version --version: mispelled meta parameter? Something is not right here
    – MelBurslan
    Mar 15, 2016 at 13:17
  • Looks like your installation is not correct. Can you try reinstalling the package or try same command on a different system. Mar 15, 2016 at 13:23
1

You can use start-instances and stop-instances commands to start/stop an instance as following -

aws ec2 stop-instances --instance-ids i-b78a096f
aws ec2 start-instances --instance-ids i-b78a096f

You will see that you don't specify region name on command line because AWS cli reads region information from <user_home_directory>/.aws/config file. This file looks as following -

[default]
output = json
region = us-east-1

Most propbably value of region attribute in this file is different from the region where you instance is located. So you call is going to a region different from what you think it should go. I see no other reason why it will not work.

In order to check what value of region AWS cli is working with, just invoke aws configure from command line. It will ask you about aws key, secret, region and response format. At the same time it will show the values which it will be using if you don't specify any value.

I use AWS cli in the scenarios like this where managing AWS from command line is much faster. However I have created an open source tool for visualizing many AWS resources at the same time which you may find helpful -

https://github.com/pistonportal/custom-turbine/wiki/Running-and-operating-Turbine-app 

For example, you can open VPC Designer in this tool and see what all availability zones a VPC spans to, which subnets are in which availability zones and which VMs are in which subnets. This is something which either requires many clicks in AWS console or not at all possible.

It does not have ability to start/stop an instance at this moment but this will be available in next few days.

3
  • your suggested start/stop commands along with anything starting as aws ec2 is giving me an error message as --instance-ids: mispelled meta parameter? any idea why ?
    – MelBurslan
    Mar 14, 2016 at 13:34
  • Actually the confusion is that you are using two different tools - AWS cli and AWS EC2 cli. ec2-describe-regions is an AWS ec2 cli command while ec2 describe-regions is an AWS cli command. I have given instructions for AWS cli tool. AWS cli can be used with any AWS service while AWS ec2 cli is specific to EC2 service. I use AWS cli because with this I need not to install one cli tool per AWS service.
    – Shailendra
    Mar 14, 2016 at 17:14
  • Okay, I have a Centos 5.9 and one centos 6 box. Both have python 2.4. The max I can go without hurting yum amd possibly other things is 2.6. centos 5.9 box is aws virgin. So, I d/l ed the aws cli zip file but the installation inside, needs Python 2.7. Is there any way to install AWC cli w/o this version of python ?
    – MelBurslan
    Mar 14, 2016 at 21:22

Not the answer you're looking for? Browse other questions tagged .