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.
- Create a Key Pair
- Create an IAM Role
- Create an AMI
- Create a Launch Configuration
- 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.
Open the Key Pair console
Click the Create Key Pair button
Name the Key Pair something that identifies it as yours
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
Open the IAM Role Console
Click on the Create New Role button
Name the role <your_name>-CodeDeployRole
In the Select Role Type screen, select Amazon EC2 under the AWS Service Roles section
On the Attach Policy screen, search for AWSCodeDeployRole from the list, select it, and click Next Step
On the Review screen, look over the details and click Create Role
Step 2.2: Modify the Role
- Open the IAM Role Console
- Click on the role you created to open its properties
- When the properties page opens, click on the Trust Relationships tab
- Click on the Edit Trust Relationships button
- 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
Open the EC2 Console
Click on the Launch Instance button
Click on the My AMIs tab on the left
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
On the Choose Instance Type screen, select t2.micro
On the Configure Instance Details screen, select these settings and then click Next:
- Number of instances: 1
- Network: <your_vpc>
- Subnet: <your_name>-public-subnet
- Auto-assign Public IP: Enable
On the Add Storage screen, leave the settings as they are and click Next
On the Tag Instance screen, set the Tag Value to <your_name>-EC2
On the Configure Security Group screen, select the Create a new security group option and use these settings:
Security group name: <your_name>-EC2
Inbound Rule:
Type: HTTP
Protocol: TCP
Port Range: 80
Source: 0.0.0.0\/0
Click Next
On the Review Instance Launch screen, click the Launch button
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.
Open the EC2 Console
Locate the instance you launched in Step 3.1 above
Right click the instance, hover over Image from the sub-menu, and select Create Image
In the Create Image dialog box, use these settings:
- Image name: (create a name that identifies this as yours)
- Image description: (add a helpful description)
Click the Create Image button
Open the AMI Console to monitor the progress of your AMI
When the AMI has been successfully created, record your AMI ID
Step 4: Create a Launch Configuration
Open the Launch Configuration Console
Click on the Create Launch Configuration button
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
On the Choose Instance Type screen, select the t2.micro and click Next
On the Configure Details screen, use these settings:
- Name: <your_name>-LC1
- IAM Role: Select the role you created in Step 2
- Open the Advanced Details section, and select the Assign a public IP address to every instanc
On the Add Storage screen, leave the settings the same
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
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
- Open the Autoscaling Group Console
- Click the Create Autoscaling Group button
- 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
On the Configure Autoscaling Group Details screen, use these settings and click Next
Group name: <your_name>-ASG1
Network: (select your VPC)
Subnets: Select <your_name>-public-subnet
On the Configure scaling policies screen, select the Keep this group at its initial size option and click Next
On the Configure notifications screen, leave the settings as-is and click Next
On the Configure Tags screen, add this tag and click Review:
- Key: Name
- Value: <your_name>-EC2
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:
Click the Edit button
Locate the Min setting, and set it to 0