Thursday, November 21, 2024

Using Amazon Web Services

After Setting up Amazon Web Services you can follow these steps to use the EC2 CLI tools to manage EC2 instances.

As per the setup instructions you require the following environment variables to use the tools. It is recommend you add these to $HOME/.bashrc or appropriate shell startup script.

$ export EC2_HOME=$HOME/aws/ec2
$ export PATH=$EC2_HOME/bin:$PATH
# For Ubuntu
$ export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/
# For Mac OSX Use
# export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home/

Necessary Access Controls

In addition you require the various AWS access keys before running any EC2 commands.

$ cd $HOME/aws
$ mv ~/Downloads/cert-EPE36OOAUD6QZZVFJCDJTEWFAG2EPWGA.pem  cert.pem
$ mv ~/Downloads/pk-EPE36OOAUD6QZZVFJCDJTEWFAG2EPWGA.pem  pk.pem
$ export EC2_CERT=$HOME/aws/cert.pem
$ export EC2_PRIVATE_KEY=$HOME/aws/pk.pem

Two more one off tasks are needed before you can launch a new instance. These are a keypair to access the instance, and a security group to maintain firewall rules.

$ ec2-add-keypair admin | grep -v "^KEYPAIR" > admin.pem
$ chmod 600 admin.pem
$ ec2-create-group db -d "DB Servers"
$ ec2-authorize db -p 22 -s XXX.XXX.XXX.XXX/32

In this example, an SSH keypair named “admin” has been created, and a security group called “db” with SSH only access from a given IP.

You should replace XXX.XXX.XXX.XXX with your current IP address.
If you do not know your current IP Address, goto http://www.whatismyip.com/.

Launching an EC2 Image

You must start with a pre-defined image to launch, known as an Amazon Machine Image (AMI). Amazon provides a number of images as do many third parties. As this example launches a Ubuntu, we use the official AMIs listed at https://help.ubuntu.com/community/EC2StartersGuide

$ ec2-run-instances ami-baba68d3 --instance-type t1.micro --region us-east-1 --group db --key admin
RESERVATION	r-c4ee13a7	336142022409	db
INSTANCE	i-91ef87f5	ami-baba68d3			pending	admin	0		t1.micro	2012-03-16T21:40:47+0000	us-east-1c	aki-805ea7e9			monitoring-disabled					ebs		paravirtual	xen		sg-cb2ef3a3	default

You can check the start of your running instances with

$ ec2-describe-instances
RESERVATION	r-c4ee13a7	336142022409	db
INSTANCE	i-91ef87f5	ami-baba68d3	ec2-23-20-96-190.compute-1.amazonaws.com	domU-12-31-38-01-6A-F6.compute-1.internal	running	admin	0		t1.micro	2012-03-16T21:40:47+0000	us-east-1c	aki-805ea7e9	monitoring-disabled	23.20.96.190	10.253.109.8			ebs					paravirtual	xen		sg-cb2ef3a3	default
BLOCKDEVICE	/dev/sda1	vol-9bc893f7	2012-03-16T21:41:17.000Z	true

When an instance is ready it will show as “running”, and there will be an ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com hostname to connect to.

$ ssh -i admin.pem [email protected]
$ uname -a
$ free -m
$ df -h
$ exit

Removing an EC2 Instance

If the server you launched is not being used, this is costing you money (unless this is your only t1.micro instance on the free account). You can remove with

$ ec2-terminate-instances  i-91ef87f5
$ sleep 10
$ ec2-describe-instances
RESERVATION	r-c4ee13a7	336142022409	db
INSTANCE	i-91ef87f5	ami-a7f539ce			terminated	admin	0		t1.micro	2012-03-16T21:40:47+0000	us-east-1c	aki-805ea7e9			monitoring-disabled					ebs	paravirtual	xen		sg-cb2ef3a3	default

Conclusion

You have successfully configured and launched an EC2 AWS instance. You should now read up on the various Instance Types and also understand the use and benefits of the various regions and availability zones.