Having used Amazon Web Services for around 2 years, I have seen the Web Console improve and grow. But on occasions or when scripting I have used the EC2 command line tools from a windows based PC. However my primary computer is my MacBook Pro, so I thought I would have a go at getting these to run from here. I was surprised at the relative ease of this.
Download and Install AWS tools
First I created a new hidden folder under my home folder called .ec2tools, then downloaded and extracted the ec2 command line tools to this folder as shown.
Open terminal and create a hidden folder called .ec2tools
cd ~/ mkdir .ec2tools cd ~/ec2tools open .
The last open command should open the hidden folder .ec2tools in Finder, next extract the amazon tools into this newly created folder. I used the standard gui to extract the zip file then moved the extracted files in to my newly created folder.
Robs-MacBook-Pro:.ec2tools rob$ ls ~/.ec2tools bin lib
Create and x509 and copy keys
To authenticate access, Amazon uses Public / Private keys. To create a new set of keys log into you AWS account, select ‘Security Credentials’ from the top right menu and scroll down the page and select the x509 tab. From here you can create a new x509 certificate which contains the public key and its associated private key. Download both these files in the format shown below and copy them in to the new folder .ec2tools.
Update Bash Profile for EC2 Tools on a MAC
In order to make things easier to use, we can define a number of commonly used environment variables, such as the AWS region you normally use, the location of your public / private keys and the path to the tools themselves. This can be done in a simple text file called a bash_profile. This file sits in the root of the users directory and it is unique per user on the computer. By default this file does not exist on a MAC, the easiest way to create this, is from the command line using the ‘touch’ command in terminal as shown below.
cd ~/ touch .bash_profile open -e .bash_profile
The open command will now allow you to use TextEdit to create a file similar to the one below.
1 2 3 4 5 6
export EC2_HOME=~/.ec2tools export PATH=$PATH:$EC2_HOME/bin export EC2_PRIVATE_KEY=`ls $EC2_HOME/pk-*.pem` export EC2_CERT=`ls $EC2_HOME/cert-*.pem` export EC2_URL=http://ec2.eu-west-1.amazonaws.com export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home/
My file below defines the default EC2_URL (region) to be Ireland, as I mainly use ireland, I define this here but I can override this at run time. You can list all the available regions with there URL’s with the command ‘ec2-describe-regions’
With the bash profile saved you need to load it up by either rebooting or typing the following command
cd ~/ source .bash_profile
Testing EC2 command line tools
You now should be able to run ec2 commands directly from the command line.
List all Snapshots
Now by default the shell knows the location of the tools, can find any required keys and also knows my default region.
List Snapshots that match a string
You can now use other command line tools to aid you. For instance use grep to filter your results. Below I’m looking for a snapshot that contain ‘d93′ in its name.
ec2-describe-snapshots | grep -i d93