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.

MelBurslan
  • 6,836
  • 2
  • 24
  • 35

2 Answers2

9

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
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Gaurav Pundir
  • 486
  • 2
  • 7
  • 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 '16 at 13:31
  • @MelBurslan please post the result of **aws --version** command – Gaurav Pundir Mar 15 '16 at 05:32
  • this [link](https://forums.aws.amazon.com/thread.jspa?threadID=138866) might help too. – Gaurav Pundir Mar 15 '16 at 05:37
  • `aws --version --version: mispelled meta parameter?` Something is not right here – MelBurslan Mar 15 '16 at 13:17
  • Looks like your installation is not correct. Can you try reinstalling the package or try same command on a different system. – Gaurav Pundir Mar 15 '16 at 13:23
  • Please see my comments on the answer below. I am having issues with the installer. My system doesn't have python 2.7 and the package installer seems to have this as a prerequisite. – MelBurslan Mar 15 '16 at 13:25
  • @MelBurslan as per your comment, I can understand that you don't want to install python2.7 system wide. The other way around is that you create a virtual environment for a particular user. For that you can install python for a particular user and then configure the virtual env for that user. In this way the python2.7 and all python packages in that virtual env will be available for that user only. and they will not affect that system wide installations. You can search google for virtual env. And I have the script ready to create the virtual env , If you want I can add it in my answer. – Gaurav Pundir Mar 15 '16 at 20:15
  • When you say `virtual environment` are you referring to a virtual machine ? If so, I may not be able to do that as I am already running on a VPS somewhere in the cloud and it has very limited resources. I don't believe I can run an additional VM layer on it. Otherwise, I am all ears – MelBurslan Mar 15 '16 at 21:06
  • No , its not a virtual machine, its just a python package, which lets a particular user install multiple python packages, without affecting other system packages, those python packages are not available until you activate that virtual environment. For example you can install aws-cli latest version in that virtual env, which has nothing to do with aws-cli installed system wide. aws-cli in that virtual env will be available for that particular user only. – Gaurav Pundir Mar 16 '16 at 07:46
  • Please update your answer and I'll test if it works under my specific setup. Thanks – MelBurslan Mar 16 '16 at 14:08
  • I have added the script for setting up virtual env. – Gaurav Pundir Mar 16 '16 at 19:35
  • first off thanks for the script. it needed `wget` and `gcc` to be included on the install line and since I'm on CentOS,, I used `yum` instead of `apt-get` but we passed that hurdle. At the end of the run, it was not clear where I needed to go, so I stepped thru the process and re-ran the virtual environment creation block and `awscli` install parts manually, another time. But I think installation was successful. I am getting this: `(my_virtual_env)[mel@serv1 my_virtual_env]$ aws --version aws-cli/1.10.13 Python/2.7.8 Linux/2.6.32-431.1.2.0.1.el6.x86_64 botocore/1.4.4` – MelBurslan Mar 16 '16 at 20:51
  • But when I inquire about my instance (I ran `aws configure` and set my keys and region correctly by the way) I am getting this: `aws ec2 describe-instances --instance-ids i-bxxxxxxf Could not connect to the endpoint URL: "https://ec2.us-west.amazonaws.com/"`I tried with us-west-2b as my region, as it was displayed such on my console with the same result. Any further wisdom ? – MelBurslan Mar 16 '16 at 20:53
  • As a note to whome ever has this misfortune of dealing with this piece of Amazon trash, if you have region names ending with `2a` or `1b`, drop the letter at the end. For instance my instance is located on `us-west-2b` according to Amazon AWS console. When you run `aws configure` and asked for `Default region name`, answer as `us-west-2` – MelBurslan Mar 16 '16 at 21:05
  • Problem finally solved. – MelBurslan Mar 16 '16 at 21:06
  • +1 for this answer. However, could you mention in your answer the platform (distro and distro version) on which it was implemented. The Canonical repo for Ubuntu 14.04.5 does not have packages: `ncurses-devel`, `openssl-devel` and `zlib-devel`. Instead I tried `libncurses5-dev`, `libssl-dev` and `zlib1g-dev`... – Cbhihe May 29 '17 at 07:04
  • for some reason I get the ip addresses of ALL instances in this way.. it seems to ignore the --instance-ids parameter – Zibri Nov 30 '18 at 15:58
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.

Shailendra
  • 111
  • 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 '16 at 13:34
  • Actually the confusion is that you are using two different tools - [AWS cli](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) and [AWS EC2 cli](http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/Welcome.html). `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 '16 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 '16 at 21:22