AWS Command Line Interface

Quite recently the AWS CLI tools were upgraded to v1.0 and I decided to take them for a spin for my latest deployment expedition.

Install it

AWS CLI tools are written in python. You can find the full source on Github

Needless to say, you need to have Python v2.7+ installed and also need either easyinstall or pip installed. I'm on Mountain Lion and I'm lucky to have both Python and easyinstall active & kicking in terminal.

I installed using easy_install like this

easy_install awscli

I created a simple shell script to make this installation step even more easier. The script installs awscli if you have the dependencies present else exits with an error.

awscli install gist

Configure aws cli tools

Just like how you configure any other unix tool, you can configure awscli by specifying your configuration in a simple text file. Place this file named config inside .aws folder inside you home directory. So the full path should look like this

~/.aws/config

Feed in the essential aws configuration like your access key and access key secret.

[default]
aws_access_key_id=xxxxxxxxxxxxxxxxx
aws_secret_access_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
region=us-east-1

[profile other]
aws_access_key=xxxxxxxxxxx
aws_secret_access_key=xxxxxxxxxxxxx
region=us-west-1

awscli support multiple aws profiles. You can manage multiple accounts or regions like this. Any other profile other than the default profile needs to have that profile keyword before the profile name.

Setting up shell completion

The awscli commands are long & soon you'll surround yourself with confusing command names like import-keypair or import-key-pair. Thats why setting up shell completion will boost your productivity around this.

To set this up, run this command

complete -C aws_completer aws

If you get this long Python stack trace while doing this

ValueError: unknown locale: UTF-8

Export these two environment variables and try again

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Add this to your ~/.profile and source it using source ~/.profile. This would ensure every time you login these environment variables are set automatically.

Getting help

With awscli under the hood of your terminal, you can do almost anything imaginable on Amazon Web Services. It packs as much power as you might never need.

Lots of power is good, but you need to know how to get help when you need it.

To get various help

# basic top level help
aws help

# use a profile, region etc
aws --profile other --region us-west-1

To get help for a specific awscli command run aws <command-name> help

# see what you can do with ec2
aws ec2 help

# see options for the run-instances ec2 command
aws ec2 run-instances help

Stopping a running instance

First get the instance id of the running instance. To stop the running instance run this command

aws ec2 stop-instances --instance-ids i-595c2720

Create AMI image

Its always a good idea to save your progress when working. Creation of AMI image will do just that. Its a good idea to stop the running instance before creating the image, your data will not get a chance to corrupt. Create your image by running this in your terminal

aws ec2 create-image --instance-id i-595c2720 --name acme --description "acme system image"

It takes up to a few minutes for the AMI creation process to complete.

List AMI images

To list your newly created AMI, run the describe-images command

aws ec2 describe-images

Start stopped instances

If some of your instances are just sleeping without a good reason, you can start them like this

aws ec2 start-instances --instance-ids i-595c2720

You can of course specify multiple instance ids above. It will take a while for the instances to become available.

Create a new instance from your AMI

We'll launch a new instance using the previously created AMI.

aws ec2 run-instances --image-id ami-add385c4 --count 1 --instance-type t1.micro --key-name acme --security-groups acme

Delete a volume

Delete a volume by referring by its volume id

aws ec2 delete-volume --volume-id vol-f7b0fa9a

References