How to setup Amazon EC2 API on Linux Machine

OBJECTIVE

The objective of this how to is to enable the use of various command line tools of Amazon such as EC2 API, S3 API, Route53 API, etc.

GETTING STARTED

  • After downloading java archive on the server, we are now ready to setup java environment.
  • Create java directory
[root@mylinuxtips ~]# mkdir /usr/java/

  • Next untar jdk into /usr/java/ folder & create the soft links as follows
[root@mylinuxtips ~]# cd /usr/java/
[root@mylinuxtips java]# ll
total 4
lrwxrwxrwx 1 root root   16 Jun 10  2010 default -> /usr/java/latest
drwxr-xr-x 9 root root 4096 Jun  8  2010 jdk1.6.0_14
lrwxrwxrwx 1 root root   21 Jun 10  2010 latest -> /usr/java/jdk1.6.0_14
[root@mylinuxtips java]#
  • Now the last step is to setup JAVA_HOME in user’s environment
[root@mylinuxtips java]# cd
[root@mylinuxtips ~]# vim .bashrc
# JAVA Home
export JAVA_HOME=/usr/java/default

  • After saving the ~/.bashrc file, source it so that the changes can be added to the environment variables
[root@mylinuxtips ~]# source .bashrc
[root@mylinuxtips ~]# echo ${JAVA_HOME}
/usr/java/default
[root@mylinuxtips ~]#

  • Now the common step among almost all Amazon APIs is that they require Amazon account certificate file, private key file, Access key & secret key. So we are going to setup these 4 variables
  • We created a folder named keys in root’s home
[root@mylinuxtips ~]# mkdir /root/keys/
[root@mylinuxtips ~]# cd keys/
[root@mylinuxtips keys]# chmod 0600 *.pem
[root@mylinuxtips keys]# ll
-rw------- 1 root root  916 Sep 30  2011 cert-XUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
-rw------- 1 root root  928 Sep 30  2011 pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem

  • Now we access Amazon console -> security credentials & copied Access Key & Secret Key to some file. We setup these two keys & above two .pem files path in user’s ~/.bashrc & source it to add the changes to user’s environment
[root@mylinuxtips keys]# cd
[root@mylinuxtips ~]# vim .bashrc
export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxxxx
export AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export EC2_PRIVATE_KEY=/root/keys/pk-XXXXXXXXXXXXXXXXXXXXXXXXXX.pem
export EC2_CERT=/root/keys/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
[root@mylinuxtips ~]# source .bashrc

[root@mylinuxtips ~]# cd ~/Downloads/
[root@mylinuxtips Downloads]# wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
--2012-12-31 10:24:16--  http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
Resolving s3.amazonaws.com... 72.21.211.199
Connecting to s3.amazonaws.com|72.21.211.199|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14471342 (14M) [binary/octet-stream]
Saving to: `ec2-api-tools.zip'

100%[==============================================================================================================================>] 14,471,342  30.7M/s   in 0.4s

2012-12-31 10:24:17 (30.7 MB/s) - `ec2-api-tools.zip' saved [14471342/14471342]

[root@mylinuxtips Downloads]# unzip ec2-api-tools.zip

  • Now we are moving ec2-api-tools folder in /home/ directory as ec2
[root@mylinuxtips Downloads]# mv ec2-api-tools-1.6.6.0/ /home/ec2

  • Then again we will setup our ~/.bashrc with EC2 API commands in user’s environment
[root@mylinuxtips Downloads]# cd
[root@mylinuxtips ~]# vim .bashrc
export EC2_HOME=/home/ec2
export PATH=$PATH:/home/ec2/bin
[root@mylinuxtips ~]# source .bashrc

  • Now it’s time to test if all the setup, keys, etc are working as expected or not
[root@mylinuxtips ~]# ec2-
ec2-add-group                              ec2-delete-spot-datafeed-subscription      ec2-detach-vpn-gateway
ec2-add-keypair                            ec2-delete-subnet                          ec2-disassociate-address
ec2-allocate-address                       ec2-delete-volume                          ec2-download-bundle
ec2-ami-tools-version                      ec2-delete-vpc                             ec2-fingerprint-key
ec2-associate-address                      ec2-delete-vpn-connection                  ec2-get-console-output
ec2-associate-dhcp-options                 ec2-delete-vpn-gateway                     ec2-get-password
ec2-attach-volume                          ec2-deregister                             ec2-migrate-bundle
ec2-attach-vpn-gateway                     ec2-describe-addresses                     ec2-migrate-image
ec2-authorize                              ec2-describe-availability-zones            ec2-migrate-manifest
ec2-bundle-image                           ec2-describe-bundle-tasks                  ec2-modify-image-attribute
ec2-bundle-instance                        ec2-describe-customer-gateways             ec2-modify-instance-attribute
ec2-bundle-vol                             ec2-describe-dhcp-options                  ec2-modify-snapshot-attribute
ec2-cancel-bundle-task                     ec2-describe-group                         ec2-monitor-instances
ec2-cancel-spot-instance-requests          ec2-describe-image-attribute               ec2-purchase-reserved-instances-offering
ec2-cmd                                    ec2-describe-images                        ec2-reboot-instances
ec2-confirm-product-instance               ec2-describe-instance-attribute            ec2-register
ec2-create-customer-gateway                ec2-describe-instances                     ec2-release-address
ec2-create-dhcp-options                    ec2-describe-keypairs                      ec2-request-spot-instances
ec2-create-image                           ec2-describe-regions                       ec2-reset-image-attribute
ec2-create-snapshot                        ec2-describe-reserved-instances            ec2-reset-instance-attribute
ec2-create-spot-datafeed-subscription      ec2-describe-reserved-instances-offerings  ec2-reset-snapshot-attribute
ec2-create-subnet                          ec2-describe-snapshot-attribute            ec2-revoke
ec2-create-volume                          ec2-describe-snapshots                     ec2-run-instances
ec2-create-vpc                             ec2-describe-spot-datafeed-subscription    ec2-start-instances
ec2-create-vpn-connection                  ec2-describe-spot-instance-requests        ec2-stop-instances
ec2-create-vpn-gateway                     ec2-describe-spot-price-history            ec2-terminate-instances
ec2-delete-bundle                          ec2-describe-subnets                       ec2-unbundle
ec2-delete-customer-gateway                ec2-describe-volumes                       ec2-unmonitor-instances
ec2-delete-dhcp-options                    ec2-describe-vpcs                          ec2-upload-bundle
ec2-delete-group                           ec2-describe-vpn-connections               ec2-version
ec2-delete-keypair                         ec2-describe-vpn-gateways
ec2-delete-snapshot                        ec2-detach-volume

[root@mylinuxtips ~]# ec2-describe-snapshots
SNAPSHOT        snap-1a840552   vol-a449b5c9    completed       2012-11-22T06:10:32+0000        100%    019966889705    10      Created by CreateImage(i-6bfe7908) for ami-25b4324c from vol-a449b5c9
SNAPSHOT        snap-1f386c57   vol-a449b5c9    completed       2012-11-25T14:16:43+0000        100%    019966889705    10      Created by CreateImage(i-6bfe7908) for ami-53d6573a from vol-a449b5c9
SNAPSHOT        snap-2e07f966   vol-a449b5c9    completed       2012-11-18T17:50:32+0000        100%    019966889705    10      Created by CreateImage(i-6bfe7908) for ami-5164e338 from vol-a449b5c9
SNAPSHOT        snap-7b864731   vol-c809abb7    completed       2012-12-10T11:24:40+0000        100%    019966889705    10      Created by CreateImage(i-4274dd3c) for ami-1ff27076 from vol-c809abb7
SNAPSHOT        snap-8fda81c4   vol-d9f2a0a5    completed       2012-12-06T06:08:32+0000        100%    019966889705    10      Created by CreateImage(i-eb195794) for ami-e593108c from vol-d9f2a0a5
[root@mylinuxtips ~]#

WOHOOOOO ALL IS WORKING PERFECTLY. HAVE FUN!!!

Leave a Reply

Your email address will not be published. Required fields are marked *