EC2 Lab

This project requires an application server that will runs on EC2. This will be a custom EC2 instance that has nginx, uWSGI, and the CodeDeploy agent installed. To properly build this server we need to complete the following steps.

  1. Create a Key Pair
  2. Create an IAM Role
  3. Create an AMI
  4. Create a Launch Configuration
  5. Create an Autoscaling Group

Step 1: Create a Key Pair

Key Pairs are used in place of passwords when authenticating with EC2 instances. When launching an EC2 instance you will need to specify what Key Pair you want to use. In this step you will create a Key Pair for your account.

  1. Open the Key Pair console

  2. Click the Create Key Pair button

  3. Name the Key Pair something that identifies it as yours

  4. Click the Create button; this will trigger a file download. Save the .pem somewhere safe

Note: You can only download the private key (.pem file) one time. Be sure to save it in a safe location.

Step 2: Create the CodeDeploy IAM Role

An IAM Role is required for the EC2 Instance to properly work with the CodeDeploy service. The IAM Role will provide permissions to the instance to perform software deployments. Follow these steps to create the role.

Step 2.1: Create the Role

  1. Open the IAM Role Console

  2. Click on the Create New Role button

  3. Name the role <your_name>-CodeDeployRole

  4. In the Select Role Type screen, select Amazon EC2 under the AWS Service Roles section

  5. On the Attach Policy screen, search for AWSCodeDeployRole from the list, select it, and click Next Step

  6. On the Review screen, look over the details and click Create Role

Step 2.2: Modify the Role

  1. Open the IAM Role Console
  2. Click on the role you created to open its properties
  3. When the properties page opens, click on the Trust Relationships tab
  4. Click on the Edit Trust Relationships button
  5. In the Policy Editor, update the code as follows:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "ec2.amazonaws.com",
          "codedeploy.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Step 3: Create an AMI

AMIs are used as the baseline template to launch EC2 instances from. This project requires an AMI that has the nginx and CodeDeploy applications installed. Normally, you'd launch

Step 3.1: Launch an EC2 Instance

  1. Open the EC2 Console

  2. Click on the Launch Instance button

  3. Click on the My AMIs tab on the left

  4. Under the Ownership section on the left, select the Shared with me box. Locate the AMI named AMI NGINX\/CODEDEPLOY and click Select next to it

  5. On the Choose Instance Type screen, select t2.micro

  6. On the Configure Instance Details screen, select these settings and then click Next:

    1. Number of instances: 1
    2. Network: <your_vpc>
    3. Subnet: <your_name>-public-subnet
    4. Auto-assign Public IP: Enable
  7. On the Add Storage screen, leave the settings as they are and click Next

  8. On the Tag Instance screen, set the Tag Value to <your_name>-EC2

  9. On the Configure Security Group screen, select the Create a new security group option and use these settings:

    1. Security group name: <your_name>-EC2

    2. Inbound Rule:

      1. Type: HTTP

      2. Protocol: TCP

      3. Port Range: 80

      4. Source: 0.0.0.0\/0

  1. Click Next

  2. On the Review Instance Launch screen, click the Launch button

  3. In the Key Pair dialog box that pops up, select the Key Pair that you created in this lesson

Step 3.2: Create the AMI

After launching an instance, you have the option of logging in and customzing the OS or applications. However, this instance already has all the components needed for the project. All you need to do is create your own AMI from it.

  1. Open the EC2 Console

  2. Locate the instance you launched in Step 3.1 above

  3. Right click the instance, hover over Image from the sub-menu, and select Create Image

  4. In the Create Image dialog box, use these settings:

    1. Image name: (create a name that identifies this as yours)
    2. Image description: (add a helpful description)
  5. Click the Create Image button

  6. Open the AMI Console to monitor the progress of your AMI

  7. When the AMI has been successfully created, record your AMI ID

Step 4: Create a Launch Configuration

  1. Open the Launch Configuration Console

  2. Click on the Create Launch Configuration button

  3. On the Choose AMI screen, click on the My AMI tab on the left, search for the AMI you created in Step 3.2, and click the Select button next to it

  4. On the Choose Instance Type screen, select the t2.micro and click Next

  5. On the Configure Details screen, use these settings:

    1. Name: <your_name>-LC1
    2. IAM Role: Select the role you created in Step 2
    3. Open the Advanced Details section, and select the Assign a public IP address to every instanc
  6. On the Add Storage screen, leave the settings the same

  7. On the Configure Security Group screen, select the Select an existing security group option, and then search for the security group you created in Step 3.1. Then click the Review button

  8. On the Review screen, click the Create Launch Configuration button. This will open a dialog that prompts you to select a Key Pair. Select yours from the drop down and click Launch

Step 5: Create the Autoscaling Group

The Autoscaling Group is used for two reasons. First, it can ensure a minimum number of instances are running. Second, it can be configured to add more instances should the load increase on this tier.

Step 5.1: Create the Autoscaling Group

  1. Open the Autoscaling Group Console
  2. Click the Create Autoscaling Group button
  3. On the first screen, select the Create an Auto Scaling group from an existing launch configuration option. In the list select the Launch Configuration you created in Step 4 and click Next
  4. On the Configure Autoscaling Group Details screen, use these settings and click Next

    1. Group name: <your_name>-ASG1

    2. Network: (select your VPC)

    3. Subnets: Select <your_name>-public-subnet

  1. On the Configure scaling policies screen, select the Keep this group at its initial size option and click Next

  2. On the Configure notifications screen, leave the settings as-is and click Next

  3. On the Configure Tags screen, add this tag and click Review:

    1. Key: Name
    2. Value: <your_name>-EC2
  4. On the Review screen, review details and click Create Autoscaling Group

Step 5.2: Modify the Autoscaling Group

After creating the Autoscaling Group we need to modify one setting. With the details of the Autoscaling Group open, follow these steps:

  1. Click the Edit button

  2. Locate the Min setting, and set it to 0

results matching ""

    No results matching ""