EC2

From cwhite's wiki
Jump to: navigation, search

Accessing EC2 Instance with Key Pair

Download the key pair you created and change permissions to 400:

chmod 400 test.pem

Run the following SSH command to access your instance. Make sure you replace the variables in the command with your key pair name and public DNS name:

ssh -i "test.pem" ec2-user@ec2-12-34-567-8.us-west-2.compute.amazonaws.com

Create AMI from instance

If you need to deploy multiple machines that require the same packages and / or configurations, it can be cumbersome to login to each instance and install all the packages and make the configurations necessary to get the machines running. AWS offers AMIs which allow you to take an image of a machine and use that image to deploy new instances. When creating an image, you can chose to shutdown the instance and take the image or take an image while the instance is running. It is best to allow the AMI service to shutdown the instance to take the image since creating an image of a running instance increases the chance of affecting the file systems integrity.

Detailed guide here.

  1. Go to your EC2 Dashboard and click Instances.
  2. Select the instance you want to create an AMI from, drop down Actions, hover over Image and click on Create Image.
  3. Fill out a name for the new AMI image, change the volume if you'd like and click Create Image.

Setup AutoScaling

You can find documentation on more AutoScaling CLI commands here.

  1. Create an AS Launch Configuration
    aws autoscaling create-launch-configuration --launch-configuration-name yourconfigname --image-id yourami-id --instance-type t2.micro --key yourkeypairname --security-groups yoursgid
  2. Create an AS Group
    For Classic LBs use
    aws autoscaling create-auto-scaling-group --auto-scaling-group-name yourgroupname --launch-configuration-name yourconfigname --region yourregion --availability-zones yourAZ --min-size 1 --max-size 3 --desired-capacity 2 --load-balancer-names your-classiclb-name
    For Application LBs use
    aws autoscaling create-auto-scaling-group --auto-scaling-group-name yourgroupname --launch-configuration-name yourconfigname --region yourregion --availability-zones yourAZ --min-size 1 --max-size 3 --desired-capacity 2 --target-group-arns your-applb-arn

When specifying availability zones, use the name of the zone and not the subnet IDs. To specify multiple availability zones, put each zone in double quotes separated by spaces.

A guide on setting up AutoScaling is here.
AutoScaling Groups are explained here.