Using the Amazon EC2
Amazon Web Services (AWS) provides various services and Amazon Elastic Compute Cloud (Amazon EC2) is one of the services. Amazon EC2 may be used to create a virtual host server. Amazon EC2 provides a wide selection of instance AMIs (Amazon Machine Images) to choose from when creating a virtual server. In this Appendix we shall discuss creating and configuring Amazon EC2 instance/s for installing Docker and Docker images. Amazon EC2 instance is not a requirement to run Docker software and an alternative platform, local or remote, may be used instead.
Creating an Amazon EC2 Instance
We have used Amazon EC2 instances based on Linux for deploying Docker and Docker images. Amazon EC2 is not a requirement and an alternative such as a local Linux installation may be used instead. The Linux platform is required to support 64 bit software. We have made use of two different 64 bit (required) AMIs:
To create an Amazon EC2 Instance, an Amazon Web Services Account is required, which may be created at https://aws.amazon.com/getting-started/?nc2=h_l2_cc. To create an Amazon EC2 instance, navigate to https://aws.amazon.com/ec2/ and click on Sign In to the Console. Select EC2 from the listed Amazon Web Services. Click on INSTANCES Instances to list the Amazon EC2 instances already created in the account. Click on Launch Instance to create a new Amazon EC2 instance as shown in Figure A-1.
Figure A-1. Launching an Amazon EC2 Instance
Select an AMI to create a virtual server from. Some of the AMIs are eligible for the Free tier. For example, select the Ubuntu AMI as shown in Figure A-2.
Figure A-2. Selecting an AMI
In Choose an Instance Type different types are available differing by features such as supported capacity and virtual CPUs (vCPUs). Select one of the Instance Types, for example the General Purpose t2.micro and click on Review and Launch as shown in Figure A-3.
Figure A-3. Review and Launch
Click on Launch in Review Instance Launch as shown in Figure A-4.
Figure A-4. Launch
A dialog gets displayed to create or select an existing key pair. A key pair is required for authorization. To create a new key pair, select the “Create a new key pair” option as shown in Figure A-5.
Figure A-5. Selecting “Create a new key pair”
Specify a Key pair name and click on Download Key Pair as shown in Figure A-6. The Key pair gets created and downloaded. The key pair selected for an Amazon EC2 instance when creating the instance is required when connecting to the instance, as is discussed later in this Appendix.
Figure A-6. Download Key Pair
Alternatively, select the option “Choose an existing key pair” and click on Launch Instances as shown in Figure A-7.
Figure A-7. Choose an existing Key Pair
The Launch Status gets displayed. Click on the instance id to display the instance as shown in Figure A-8.
Figure A-8. Launch Status
The instance gets listed and is initially in the “pending” state as shown in Figure A-9.
Figure A-9. Amazon EC2 Instance in Pending State
When an instance has launched completely, the Instance State becomes “running” as shown in Figure A-10.
Figure A-10. Running Instance
Creating a Key Pair
As mentioned previously, a key pair is required to connect to a Amazon EC2 instance. A key pair may be created while creating an instance or separately. To create a key pair separately select Network & Security Key Pairs as shown in Figure A-11.
Figure A-11. Network & Security Key Pairs
The key pairs already created get listed. A key pair may be deleted by selecting the key pair and clicking on Delete. Click on Yes in the dialog as shown in Figure A-12.
Figure A-12. Delete Key Pair
To create a new key pair, click on Create Key Pair as shown in Figure A-13.
Figure A-13. Create Key Pair
Specify a Key pair name and click on Create button as shown in Figure A-14.
Figure A-14. Create Button
A new key pair gets created as shown in Figure A-15.
Figure A-15. New Key Pair
Starting an Amazon EC2 Instance
When a new Amazon EC2 instance is created and Launch is selected, the instance gets started. A stopped instance may be started by selecting the checkbox adjacent to the instance and selecting Actions Instance State Start as shown in Figure A-16.
Figure A-16. Actions Instance State Start
In Start Instances dialog click on Yes, Start as shown in Figure A-17.
Figure A-17. Starting an instance
Connecting to an Amazon EC2 Instance
An instance that has been started may be connected to from a local machine such as a local Linux instance without as much RAM and a different Linux distribution than the instance being connected to. The ssh command to use to connect to a running instance may be obtained by clicking on Connect as shown in Figure A-18.
Figure A-18. Connect
In the Connect To Your Instance dialog, the ssh command is displayed. The “docker.pem” is the key pair used to create an instance and also downloaded to the local instance from which the Amazon EC2 instance is to be connected. The username for an Ubuntu instance is “ubuntu” as shown in Figure A-19 and for a Red Hat instance is “ec2-user”.
Figure A-19. Connect To Your Instance dialog
The IP Address shown in the ssh command is the Public IP Address of the Amazon EC2 instance.
Finding the Public IP Address
The Public IP Address may also be obtained from the EC2 Console as shown in Figure A-20.
Figure A-20. Public IP Address
Finding the Public DNS
To connect to an Amazon EC2 instance process such as the HelloWorld application in Chapter 1 from a remote browser, the Public DNS is required. The Public DNS may also be obtained from the EC2 Management Console as shown in Figure A-21.
Figure A-21. Public DNS
The Public DNS may not get displayed initially. To display the Public DNS, select Services VPC in the EC2 Management Console as shown in Figure A-22. VPC is a virtual private cloud assigned to a user.
Figure A-22. Services VPC
In the VPC Dashboard, select Your VPCs as shown in Figure A-23.
Figure A-23. Your VPCs
Select the VPC listed as shown in Figure A-24.
Figure A-24. Selecting the VPC
From Actions, select Edit DNS Hostnames as shown in Figure A-25.
Figure A-25. Edit DNS Hostnames
In the Edit DNS Hostnames dialog, select Yes for the DNS Hostnames, and click on Save as shown in Figure A-26.
Figure A-26. Edit DNS Hostnames Dialog
Adding the default Security Group
To be able to connect from a remote browser, the Inbound and Outbound rules are required to be set to allow all traffic using any protocol on all ports in the range 0-65535 from any source. The “default” security group is configured by default to allow all traffic. We need to assign the “default” security group to the Amazon EC2 instance running Docker. Select the instance and select Actions Networking Change Security Groups as shown in Figure A-27.
Figure A-27. Actions Networking Change Security Groups
In the Change Security Groups panel, the “default” group might not be selected as shown in Figure A-28.
Figure A-28. The “default” group not selected
Select the checkbox for the “default” security group and click on Assign Security Groups as shown in Figure A-29.
Figure A-29. Assign Security Groups
The default security group gets assigned to the Amazon EC2 instance. To find the available security groups and their inbound/outbound rules, click on Network & Security Security Groups as shown in Figure A-30.
Figure A-30. Network & Security Security Groups
The “default” security group should be listed. Select the “default” group. Select the Inbound tab. The Type should be listed as “All Traffic”, the Protocol as “All”, the Port Range as All and Source as 0.0.0.0. To edit the inbound rules, click on Inbound Edit as shown in Figure A-31.
Figure A-31. Inbound Edit
The inbound rules get displayed and should be kept as the default settings as shown in Figure A-32. Click on Save.
Figure A-32. Edit inbound rules dialog
Similarly, select the Outbound tab. The Type should be listed as “All Traffic”, the Protocol as “All”, the Port Range as All and Destination as 0.0.0.0. Click on Edit as shown in Figure A-33.
Figure A-33. Outbound Edit
The default settings for the Outbound rules get displayed and should be kept as the default as shown in Figure A-34. Click on Save.
Figure A-34. Edit outbound rules dialog
The security groups assigned to an instance are listed in the Security Groups column as shown in Figure A-35.
Figure A-35. Security Groups column
Stopping an Amazon EC2 Instance
To stop an Amazon EC2 instance select the instance and select Actions Instance State Stop as shown in Figure A-36.
Figure A-36. Actions Instance State Stop
Multiple instances may be selected and stopped together as shown in Figure A-37.
Figure A-37. Stopping Multiple Instances
In the Stop Instance dialog, click on Yes, Stop as shown in Figure A-38.
Figure 38. Stop Instance dialog
The instance/s get stopped.
Changing the Instance Type
To increase or decrease the capacity of an instance, it may be required to change the instance type, such as from a micro instance to a medium instance. An instance must first be stopped before changing its type and later restarted after modifying the type. To change the instance type, select the instance and select Actions Instance Settings Change Instance Type as shown in Figure A-39.
Figure 39. Actions Instance Settings Change Instance Type
In the Change Instance Type dialog, select the Instance Type to apply, for example, m3.medium as shown in Figure A-40.
Figure A-40. Change Instance Type dialog
Click on Apply as shown in Figure A-41.
Figure A-41. Applying a new Instance Type
The instance type gets upgraded to m3.medium as shown in Figure A-42. Keep in consideration that upgrading an instance type could make the instance not eligible for the free tier.
Figure A-42. Upgraded Instance Type
Summary
In Appendix A we discussed creating an Amazon EC2 instance based on an AMI, starting an instance, connecting to an instance, finding the Public IP Address, finding the Public DNS, changing the instance type and stopping an instance.