Value driven web development

Amazon provides two types of tool.

1. Operational tools that use to startup and shutdown AMI instances

2. Packaging tool to pack the AMI that created , or modify on existing public AMI and rebundle to new private AMI

Operational tool

Create a new AWS Access Identifiers if you don't already have.

$ mkdir ~/.ec2

download both the Private Key file and X.509 Certificate file to ~/.ec2

rename the pk-xxxxxxxxxx.pem one to PrivateKey.pem
rename the cert-xxxxxxxxx.pem one to 509certificate.pem
( xxxxxxxxxx stand for the random name generate by amazon )

visit the following to download the packaged file to Download folder

http://developer.amazonwebservices.com/connect/entry.jspa?externalID=351&categoryID=88

$ mkdir ~/ec2

$ cp ~/Downloads/ec2-api-tools.zip ~/ec2/

$ cd ~/ec2/

$ unzip ec2-api-tools.zip

$ nano ~/.profile

append the follow line to the end

export JAVA_HOME=/Library/Java/Home
export PATH=~/ec2/ec2-api-tools-1.2-13740/bin:$PATH
export EC2_HOME=~/ec2/ec2-api-tools-1.2-13740
export EC2_PRIVATE_KEY=~/.ec2/PrivateKey.pem
export EC2_CERT=~/.ec2/509certificate.pem

restart the terminal to active the changes on bash profile

$ ec2-describe-images -o amazon IMAGE ami-20b65349 ec2-public-images/fedora-core4-base.manifest.xml amazon available public
IMAGE ami-22b6534b ec2-public-images/fedora-core4-mysql.manifest.xml amazon available public
IMAGE ami-23b6534a ec2-public-images/fedora-core4-apache.manifest.xml amazon available public
IMAGE ami-25b6534c ec2-public-images/fedora-core4-apache-mysql.manifest.xml amazon available public
IMAGE ami-26b6534f ec2-public-images/developer-image.manifest.xml amazon available public
IMAGE ami-2bb65342 ec2-public-images/getting-started.manifest.xml amazon available public
IMAGE ami-36ff1a5f ec2-public-images/fedora-core6-base-x86_64.manifest.xml amazon available public
IMAGE ami-bd9d78d4 ec2-public-images/demo-paid-AMI.manifest.xml amazon available public A79EC0DB

If install sucessfully , will show the list of the AMI that amazon provide

$ ec2-add-keypair demo-keypair KEYPAIR ankoder d9:d9:85:95:5f:ef:19:c9:33:5b:78:e6:e7:82:cc:e2:6a:9f:70:76
-----BEG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qFvbGmce9H1fVnPaOKsZsMl3g86dEC
gYEAvQWGFlKFHGEB+mc3MPK3y0Q5oNC6sb2d+S5lGwS/jGYAbkbwtI0Myf2ouIm+wV2KIW0A04x1
shDO4cKwOeJKxeWXxEjU2DxRmWIB4FXTwgMOCVM/wkyshlW3mcn58iLrZELn9qXreZW5V75zwpbl
WPjaU1/YSUS1gFxAZO/rzAsCgYA3/w+OBSQqmzxlF/VE76ZPVWfknqH8iEoRg7wgPPBK3txfEeqA
AV5ph5A4lipBt0QpyIwJNJwIaId23s9tZSgSYq69jVsQG2USEMCCwTlMYczZ5IOtaLh2ck0E/y3W
oct2+DXf6gbDjEXlxN3YzU02/XStGUhF3/kCiBqF1vlT4QKBgFtaVjtc1VO/t0+vQ8CmTn4Y03Ml
pCORn4081m1w5PhfEbei+qzAY66r8AkB1Du9CT/NhrGXMsTTrsTiaApjoJ2HMUW3Pr/m6x/QIpTf
/4cFSHaDKnb5plNB3+DwPjjtDx/37PC6XAVTOPxaANr/lEkkST0vub+MkkQ/RxShLxKg
-----END RSA PRIVATE KEY-----

copy the above text including -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- to ~/.ssh/id_rsa-demo-keypair

$ nano ~/.ssh/id_rsa-demo-keypair

paste all the content to it and save

$ ec2-run-instances ami-25b6534c -k demo-keypair

RESERVATION r-b7fc17de 119012238391 default
INSTANCE i-6ea05007 ami-25b6534c pending ankoder 0 m1.small
2008-01-07T05:49:15+0000

if the terminal return the message similar to this , you have successfully started a AMI instance.

To show all the instances currently running can use the ec2-describe-instances command

$ ec2-describe-instances
INSTANCE i-6ea05007 ami-25b6534c ec2-67-202-24-116.compute-1.amazonaws.com
domU-12-31-38-00-29-48.compute-1.internal running ankoder m1.small
2008-01-07T05:49:15+0000

To shutdown a running instance, can first find the instance id by using `ec2-describe-instances` , the one that in format of i-xxxxxxxx is the instance id

$ ec2-terminate-instances i-6ea05007
INSTANCE i-6ea05007 running shutting-down

Allow the instance to be accessable from outside

$ ec2-authorize default -p 22
PERMISSION default ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0

$ ec2-authorize default -p 80
PERMISSION default ALLOWS tcp 80 80 FROM CIDR 0.0.0.0/0

$ ec2-authorize default -p 21
PERMISSION default ALLOWS tcp 80 21 FROM CIDR 0.0.0.0/0

To connect to the AMI instance from SSH
first check the hostname

$ ec2-describe-instances
INSTANCE i-55a0503c ami-25b6534c ec2-67-202-2-199.compute-1.amazonaws.com
domU-12-31-38-00-42-55.compute-1.internal running ankoder m1.small
2008-01-07T06:02:38+0000

using the private key that you generated to login
$ ssh root@ec2-67-202-2-199.compute-1.amazonaws.com -i ~/.ssh/id_rsa-demo-keypair

you can do whatever you like in this instance

install ec2onrails support tool
$ sudo gem install ec2onrails

As the Amazon EC2 tool is in rpm format, and MacOSX doesn't come with it.
so we have to use DarwinPorts to install rpm first

Install the rpm

$ sudo port install rpm

Install Amazon packaging tool

$ wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm

$ sudo rpm -i ec2-ami-tools.noarch.rpm --nodeps --ignoreos

Also, there is a gem for access the EC2 from ruby

$ sudo gem install amazon-ec2 --include-dependencies

Sorry, comments are closed for this article.