AWS - EC2
Use hardware virtual machine (HVM) images if possible. They are newer generation that provide faster access to underlying hardware. AWS EC2 uses Xen hypervisor and HVM uses Intel VT-x platform.
Use at least 3.8 Linux server kernel Amazon Machine Image (AMI) doesn't include kernel. AMI contains OS and potentially some pre-installed software. You propably never need this information but kernel is in fact a property of Amazon Kernel Image (AKI), which are separate from AMIs.
Surely Good Images:
Amazon Linux 13.09+, Ubuntu 14.04+, RHEL7+
Make sure to pick the HVM version.
You can change instance type when it's stopped. EC2 > Select Instance > Actions > Change Instance Type. But if you would've started it using a CloudFormation template, you wouldn't need to wait for it to stop, just update the template using the Update Stack
button.
You can associate multiple IPs to same machine on EC2.
- Start an instance with 1 allocated public IP address.
- EC2 > Network Interfaces > Create > Name it and select the instance's subnet.
- Wait for Available > Attach > Select Instance
- EC2 > Elastic IPs > Allocate New Address > Select the network interface
- SSH to instance >
ipconfig
will show the private IPs that you can use
# /etc/httpd/conf.d/a.conf
<VirtualHost 172.31.11.11:80>
DocumentRoot /var/www/html/a
</VirtualHost>
# /etc/httpd/conf.d/b.conf
<VirtualHost 172.31.22.22:80>
DocumentRoot /var/www/html/b
</VirtualHost>
sudo service httpd restart
AWS provides three ways to buy instances:
- On-demand: high price, high flexibility, medium reliability
- Reserved: medium price, low flexibility, high reliability
- Spot: low price, medium flexibility, low reliability
Start with on-demand instances, when everything is stable,
buy enough reserved instances to keep your application running
in minimal capacity while boosting with additional on-demand instances.
Spot instances are meant for asynchronous work and should rarely be used for normal web servers. You specify max bid and pay the market price, when market price goes over your max bid, your instance will be terminated in 2 minutes. Spot instance requests should remove when terminating the instance, but sometimes they do not.
Utilize EC2 user data for simple initialization. EC2 instances can have "user data" field in their configurations. This is a max 16KB script that is executed on the instance at the end of the boot process. It's implemented so that the instance downloads the script from address http://169.254.169.254/latest/user-data
, where each user data is accessible only by the instance it's related to.
# You can get user data of an instance doing the following inside the instance:
curl -s http://169.254.169.254/latest/user-data
curl -s http://169.254.169.254/latest/meta-data/local-ipv4
curl -s http://169.254.169.254/latest/meta-data/public-ipv4
To debug user data scripts: EC2 -> Right click instance -> Instance Settings -> Get System Logs if you SSH into the instance, same log will be found at /var/logs/cloud-init-output.log
Useful user datas for Amazon Linux images:
# update everything at startup
#!/bin/bas -ex
yum -y update
# only install security updates
#!/bin/bas -ex
yum -y --security update
Useful EC2 scripts:
#!/bin/bash -ex
# List all running instances:
INSTANCES=$(aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query "Reservations[].Instances[].PublicDnsName" \
--output text)
# Run security updates on all running instances:
for INSTANCE in $INSTANCES; do
ssh -i /Users/ruksi/.ssh/aws-skeleton-key.pem -t -o \
StrictHostKeyChecking=no ec2-user@$PUBLICNAME \
"sudo yum -y --security update"
done
DEFAULT_VPC=$(aws ec2 describe-vpcs \
--filter "Name=isDefault, Values=true" \
--query "Vpcs[0].VpcId" \
--output text)
FIRST_SUBNET=$(aws ec2 describe-subnets \
--filters Name=vpc-id,Values=$vpc \
--query Subnets[0].SubnetId \
--output text)
# now you can use this id for cloudformation templates