School of Computer Science Dr.
COMP5349: Cloud Computing Sem.1 /2022
Week 2: AWS EC2 Tutorial
Objectives
Copyright By PowCoder代写 加微信 powcoder
03.03.2022
In this tutorial, we will practice using the AWS user interface, launching an EC2 instance, and accessing this instance using ssh. The objective is to learn about the different life cycles of an instance and storage.
Question 1: Open AWS Console
Login to AWS Academy LMS and open “AWS Academy Learner Lab – Foundation Services [15226]” course. Click “Modules” to locate the “AWS Academy Learner Lab Student guide”. Follow the instructions in the stu- dent guide to start the lab (see the red rectangles in Figures 1 and 2). This lab interface is similar to the lab interface in Cloud Foundations course. You will be using different IAM accounts to access AWS and the resources you created in one account cannot be accessed in another.
Figure 1: Start Lab button
Figure 2: AWS console link
Question 2: Launching Linux EC2 Instance
a) Creating a key pair
• IntheAWSManagementConsoleontheServicesmenu,choose
• Choose Key Pairs under Network & Security on the left pane.
• Click the Create key pair button at the top right corner.
• Enter a name for the key pair. We assume it is called “comp5349- lab-key” in this and the following lab. Keep the key pair type as “RSA”. Select the appropriate Private key file format: Mac and Linux users should choose “.pem”; Windows users should choose “.ppk” format.
• Click the Create key pair button. Your private key will be down- loaded immediately in the default download folder, which is usually the “Downloads” directory.
b) Requesting an instance as Web server
• Return to the EC2 dashboard and choose Launch Instance. Then
select Launch Instance.
• Choose Select next to “Amazon Linux 2 AMI” (at the top of the
list). This will bring you to Step 2: Choose an Instance Type .
• Select the default t2.micro instance type. Click Next: Configure
Instance Details.
• You are now at the Step 3: Configure Instance Details page. Scroll down to the bottom of page and expand Advanced Details. Paste the following text in the User Data field
[Note:] This is the same script used in AWS Cloud Foundation Lab 3.
• At Step 5: Add Tags, add the tag Key “Name” with value “Web Server”;
• At Step 6: Configure Security Group, configure: 2
#!/bin/bash
yum -y install httpd
systemctl enable httpd
systemctl start httpd
echo ‘
Hello From Your Web Server!
‘ >/var/www/html/index.html
– Security group name: Web Server security group
– Description: Security group for my web server
• Then click Add Rule and use the drop down list to add an HTTP
• Click Review and Launch. This will take you to Step 7: Review Instance Launch. Click Launch to launch your instance. On the launch pop up widow, select the key pair you just created. Click Launch Instances.
c) Connect to the Web server Once your instance has been launched, click View Instances to go to the EC2 instance window. Locate the public DNS address and copy that to your browser to visit the Web server. You should see a Web page displaying “Hello From Your Web Server!”
Question 3: Attaching an EBS volume
When starting the instance, we use the default storage setting with a boot EBS volume of 8 GB. This volume will be deleted after the instance is terminated. A typical way to persist your data and/or code developed on EC2 is to put them on a separate EBS volume that can be unmounted and detached from an instance. An EBS volume can have its own life cycle that is independent of any instance. They can be detached from one instance and reattached to another instance. An instance can specify multiple EBS volumes at requesting time. It is also possible to attache an EBS volume to a running instance. Attaching a new EBS volume to a running instance involves the following steps:
1. Create a volume with the desired capacity from AWS management console.
2. Attach it to an instance.
3. Inside the instance, format the volume with an appropriate file system format.
4. Mount it to a directory in the instance’s file system
In this section, we will go through the above steps.
a) Creating an EBS volume FromtheAWSEC2dashboard,clickVolumesundertheElastic Block Store heading.
Click the Create Volume button to create a new EBS volume. The volume creation page allows us to configure various properties of the new volume. In this lab, we use default values for most properties. However, there is one property where we cannot rely on its default value: the availability zone property. An EBS volume can only be seen and attached to instances in the same availability zone. You need to make sure the newly created EBS volume is in the same availability zone as your instance. The availability zone information of an instance can be found from the EC2 instances section. You may change the default volume size to a different number. You may also tag the EBS volume with
• Key: Name
• Value: user-data
After updating the necessary information, click Create Volume to cre- ate the volume. This should not take a long time and you will be brought back to the main page. We will call this newly created vol- ume “user-data” as shown in Figure 3. Notice also that the state of this volume is Available, meaning it can be attached to an instance. A volume already attached to an instance has a state called in-use.
Figure 3: EBS Volume Status
b) Attaching a Volume to an Instance Select the newly created EBS Volume and click Attach volume from the Action drop down menu list. Select the instance id or name (if you have given your instance a name) in the following window to specify the target instance for this volume.
After attaching the volume, you will be able to see it from the EC2 instance description tab as a block device.
Question 4: Using the EBS Volume
After attaching the volume, we need to mount it to the file system. For a new volume, it is necessary to format it before mounting. This needs to be done inside the instance (e.g. in the SSH login window)
a) Connecting to the Instance
For Mac and Linux users, the instructions to connect can be found by selecting the instance you just created and click Connect. You can copy the SSH command and paste it on your shell window to make the connection.
Windows users should first install Putty from Page. Then follow the instruction1 to connect to the instance.
Figure 4: AWS Instance Overview
Figure 5: AWS Instance Connection b) Format and Mount the EBS Volume
The lsblk command will show all disks attached to this instance. Be- low is a typical output. The name of the disk is “xvdf”.
1You can skip the puttygen step if you have downloaded the key in ppkformat
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk
|– xvda1 202:1 0 8G 0 part / xvdf 202:80 0 10G 0 disk
The EBS volume needs to be formatted before we can use it. Use the following command to format it using the ext3 file system:
sudo mkfs -t ext3 /dev/xvdf
c) Mount the Volume to File System
The following commands create a directory called “public html” under the current user’s home directory and mount the device /dev/sdf to that directory.
mkdir ~/public_html
sudo mount /dev/xvdf ~/public_html/
Both commands do not return anything if successful. To make it writable for the current user, change the directory’s owner to ’ec2-user’ using the following command under your home directory:
sudo chown ec2-user ~/public_html
d) Using the
Once mounted, you can treat the directory in the same way as any
other directory in the file system.
To test that it works properly, create a simple html file in the directory:
cd ~/public_html
echo ‘
Hello From ec2-user!
‘ > index.html
“public html” is the default directory for each user to put place personal HTML files served by the Web server. This directive is set to disabled by default. We can enable it by editing the “userdir.conf” configuration file.
sudo nano -w /etc/httpd/conf.d/userdir.conf
You should comment out the line “UserDir disabled” and uncomment the line “UserDir public html” as shown in Figure 6.
Use the following command to stop and restart the Web server to en- able the configuration change to take effect.
sudo systemctl stop httpd
sudo systemctl start httpd
We also need to set the correct permission for the user directories as follows:
chmod 711 /home/ec2-user
chmod 755 /home/ec2-user/public_html
Figure 6: UserDir Configurtion The html page can be visited at the following address:
Question 5: Detaching an EBS volume
An instance can be terminated or stopped. Terminating an instance would delete all its attached volumes. Stopping an instance, on the contrary, does not delete its volumes. All volumes will be kept and indicated as in-use by the stopped instance. If we only want to keep some volumes, we can detach these from an instance before terminating it. We need to unmount these volumes from the file system before detaching the devices from the instance. The simple command for umount is:
sudo umount ~/public_html
Note: The above command will not work if your present working directory is the same as the directory you are trying to unmount the volume from. If this is the case, use cd to change the present working directory before using umount.
To detach a volume, go back to AWS’s EC2 dashboard and find the Volumes page. This will show all volumes in the default availability zone. Click Actions and select Detach Volume to detach it from the instance. The status should change to “available”.
Question 6: Terminate the instance
Terminate the instance by clicking the Instance state button at the top of the instance overview page and choose the Terminate instance menu item.
Question 7: Re-attach the EBS volume to another instance
Now try to request a bigger instance, e.g. t2.small with the same AWS Amazon Linux image. Attach the “public html” volume to this instance and mount it under “ec2-user” home directory in the file system. You should be able to visit the html page from the new instance.
Question 8: Closing the lab
When you are finished, be sure to terminate all instances and remove any volumes that you no longer need. You should also click on the “End Lab” button on the AWS LMS Learner Lab page and answer yes when asked to confirm that you wish to end your lab session (see Figures 7 and 8).
Figure 7: End Lab button
Figure 8: End Lab Confirmation
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com