If you are command line centric guy like me and you are on Ubuntu this post is for you.Getting starting with Amazon was a pain for me although once you understand the basics it is relativly easy. BTW: there are of course also other cloud systems like Rackspace or Azure.
If you want the official Ubuntu LTS Server (currently 10.04) running in the Amazon Cloud you can do:
ec2-run-instances ami-c00e3cb4 --region eu-west-1 --instance-type m1.small --key amazon-key
or go to this page and pick a different AMI. Hmmh, you are already sick of all the wording like AMI, EC2 and instances? Ok,
lets digg into the amazon world.
Let me know if I have something missing or incorrect:
- AMI: Amazon Machine Image. This is a highly tuned linux distribution in our case and we can choose from a lot of different types – e.g. on this page.
- EC2: Elastic Compute Cloud – which is a highly scalable hosting solution where you have root access to the server. You can choose the power and RAM of that instance (‘a server’) and start and stop instances as you like. In Germany Amazon is relative expensive compared to existing hosting solutions (not that case in the US). And since those services can also easy scale there is nearly no advantage of using Amazon or Rackspace.
- EBS: Elastic block storage – This is where we store our data. An EBS can be attached to any instance but in my case I don’t need a separate volume I just can use the default EBS mounted at /mnt with ~150 GB or even the system partition / with ~8 GB. From wikipedia:
EBS volumes provide persistent storage independent of the lifetime of the EC2 instance, and act much like hard drives on a real server.
Also if you choose storage of type ‘ebs’ your instance can be stopped. If it is of type instance-store you could only clone the AMI and terminate. If you try to stop it you’ll get “The instance does not have an ‘ebs’ root device type and cannot be stopped.”
- A running instance is always attached to one key (a named public key). Once started you cannot change it.
- S3: Simple Storage Service. Can be used for e.g. backup purposes, has an own API (REST or SOAP). Not covered in this mini post.
- Availability zone: The datacenter location e.g. eu-west-1 is Ireland or us-west-2 is Oregon. The advantage of having different regions/zones is that if one datacenter crashes you have a fall back in a different. But the big disadvantage of different zones is that e.g. transfering your customized AMIs to a different region is a bit complex and you’ll need to import your keys again etc.
But even now after ‘understanding’ of the wording it is not that easy to get started and e.g. the above command will not work out of the box.
To make the above command working you’ll need:
- An Amazon Account and a lot of money😉 or use the micro instance which is free for one year and for a fresh account IMO
- The ec2 tools installed locally: sudo apt-get install ec2-api-tools
- The amazon credentials stored and added to your ssh-agent:
- Test the functionality via
ec2-describe-instances –region eu-west-1
- Now you need to create a key pair and import the public one into your account (choose the right availability zone!)
Aws Console -> Ec2 -> Network & Security -> Key Pairs -> Import Key Pair and choose amazon-key as name
- Then feed your local ssh-agent with the private key:
- Now you should be able to run the above command. To view the instance from the web UI you’ll have to refresh the site.
- Open port 22 for the default security group:
Aws Console -> Ec2 -> Network & Security -> Security Groups -> Click on the default one and then on the ‘inbound’ Tab -> type ’22’ in port range -> Add Rule -> delete the other configurations -> Apply Rule Changes
- Now try to login
For the official amazon AMIs you’ll have to use ec2-user as login
That was easy No?
Ok, now you’ll have to configure and install software as you like e.g.
sudo apt-get update && sudo apt-get upgrade -y
To proceed further you could
- Attach a static IP to the instance so that external applications do not need to be changed after you moved the instance – or use that IP for your load balancer – or use the Amazon load balancer etc:
Aws Console -> Ec2 -> Network & Security -> Elastic IPs -> Allocate New Address
- Open some more ports like port 80
- Or you could create an AMI of your already configured system. You can even publish this custom AMI.
- Run ElasticSearch as search server in the cloud e.g. even via a debian package which makes it very easy.
Now if you have several instance and you want to
update software on all machines.
How would you do that? Here is one possibility
ips=`ec2-describe-instances --region eu-west-1 | grep running | cut -f17 | tr '\n' ' '` for IP in $ips do echo UPDATING $IP; ssh -A ubuntu@$IP "cd /somewhere; bash ./scripts/update.sh"; done