Troubeshoot Creation of An Ec2 Instance
Troubeshoot Creation of An Ec2 Instance
Activity overview
In this activity, you use the AWS Command Line Interface (AWS CLI) to launch Amazon
Elastic Compute Cloud (Amazon EC2) instances.
When you create the instance, you will reference a user data script to configure
the instance to have an Apache web server, a MariaDB relational database (which is
a fork of the MySQL relational database), and PHP running on the instance.
Together, these software packages installed on a single machine are often referred
to as a LAMP stack (Linux, Apache web server, MySQL, and PHP). Using a LAMP stack
is a common way to create a website with a database backend on a single machine.
The same user data file will deploy website files and run database configuration
scripts on the instance. The result will be an instance that hosts the Café Web
Application.
The following diagram shows the architecture that you will create in this activity.
Objectives
After completing this activity, you should be able to do the following:
Wait until the message "Lab status: ready" appears, and then choose X to close the
Start Lab panel.
Next to Start Lab, choose AWS to open the AWS Management Console on a new browser
tab. The system automatically signs you in.
Tip: If a new browser tab does not open, a banner or icon at the top of your
browser will indicate that your browser is preventing the site from opening pop-up
windows. Choose the banner or icon, and choose Allow pop ups.
Arrange the AWS Management Console so that it appears alongside these instructions.
Important: Do not change the lab Region unless specifically instructed to do so.
On the AWS Management Console, in the Search bar, enter and choose EC2 to open the
Amazon EC2 Management Console.
In the navigation pane, choose Instances.
Note: If you prefer to use an SSH client to connect to the EC2 instance, see the
guidance to Connect to Your Linux Instance.
Now that you are connected to the CLI Host instance, you can configure and use the
AWS CLI to call AWS services.
Note: Amazon Linux Amazon Machine Images (AMIs) already have the AWS CLI
preinstalled.
To set up the AWS CLI profile with credentials, run the following command:
aws configure
Tip: In EC2 Instance Connect, you might need to use the context (right-click) menu
to paste values in the terminal.
AWS Access Key ID: At the top of these instructions, choose Details, and then
choose Show. Copy the AccessKey value into the terminal window.
AWS Secret Access Key: Copy the SecretKey value from the lab instructions into the
terminal window.
Default region name: Copy the LabRegion value from the lab instructions into the
terminal window.
Default output format: Enter json
To change to the directory where the script file exists and create a backup of it,
run the following commands:
cd ~/sysops-activity-files/starters
cp create-lamp-instance-v2.sh create-lamp-instance.backup
Tip: It is a good practice to back up files before you modify them.
Open the create-lamp-instance-v2.sh script file in a command line text editor, such
as VI.
Tip: To open the file in read-only mode in the VI editor, use the following
command:
view create-lamp-instance-v2.sh
Analyze the contents of the script:
Tip: If you are using VI, you can display the line numbers by typing :set number
and then pressing Enter.
Lines 7–11: The instance size is set to t3.small, which should be large enough to
run the database and web server.
Lines 16–29: The script invokes the AWS CLI describe-regions command to get a list
of all AWS Regions. In each Region, the script queries for an existing VPC that is
named Cafe VPC. After finding the VPC, the script captures the VPC ID and Region,
and breaks out of the while loop. This is the VPC where the LAMP instance for the
Café Web Application should be deployed.
Lines 31–55:
The script invokes AWS CLI commands to look up the subnet ID, key pair name, and
AMI ID values that are needed to create the EC2 instance.
Notice that line 32 ends with a backslash ( \ ) character. This character can be
used to wrap a single command on another line in the script file. This technique is
used many times in the script to make it easier to read.
Lines 57–122: The script cleans up the AWS account for situations where this script
already ran in the account and is being run again. The script checks whether an
instance that is named cafeserver already exists and whether a security group that
includes cafeSG in its name already exists. If either resource is found, the script
prompts you to delete them.
Lines 124–152: The script creates a new security group with ports 22 and 80 open.
Lines 154–168:
The script creates a new EC2 instance. Notice how this part of the script uses the
values that were set in lines 8 and 10, and the values that were collected in lines
16–57.
Notice the reference to the user data file. You will review the details of this
file in another step.
The entire call to create the instance is captured in a variable that is named
instanceDetails. The contents of this variable are then echoed out to the terminal
on line 177, and they are formatted for easier viewing by using a Python JSON tool.
Lines 179–188: The instanceId value is parsed out of the instanceDetails variable.
Then, a while loop checks every 10 seconds to see if a public IP address has been
assigned to the instance. After the check succeeds, the public IP address is
written to the terminal.
To display the contents of the user data script, run the following command:
cat create-lamp-instance-userdata-v2.txt
Notice how the user data script runs a series of commands on the instance after it
is launched. These commands will install a web server, PHP, and a database server.
./create-lamp-instance-v2.sh
The script fails and exits without successfully completing. This behavior is
expected.
Locate the line in the script that led to this error. Does anything look incorrect
in the script?
Was the correct Region value used for the run-instances command?
After you identify the issue, update the script to fix the issue, and run the
script again. If you are prompted to delete any existing security groups or
instances that were created when the script ran previously, always reply with Y. Is
the error resolved?
After you fix the issue, the run-instances command succeeds, and a public IPv4
address is assigned to the new instance.
Issue #2
The run-instances command succeeded, and a public IP address was assigned to the
new instance. However, you cannot load the test webpage.
Connect to the new LAMP instance by using EC2 Instance Connect, which is the method
that you used to connect to the CLI Host instance.
Tips to help you resolve issue #2:
If you resolved issue #2 successfully, you should see the following message: "Hello
From Your Web Server!"
Check the log file that shows whether the user data script ran as expected.
In the terminal window for the LAMP instance, run the following command to see the
log file entries as they are written:
Observe the log file entries. Notice the message that is related to the
installation of MariaDB and PHP. There should be no error messages.
You should also see messages related to files for the Café Web Application that
were downloaded and extracted to this instance, such as "Create Database script
completed".
When you are finished with your observations, to exit the tail utility, enter Ctrl-
C
Tip: To view the entire log file, run sudo cat /var/log/cloud-init-output.log
If you are successful, you should see the home page for the café website.
Congratulations!
Choose a few desserts to order, and then choose Submit Order. The Order
Confirmation page displays with line-item details.
Place another order for different items. Then, choose the Order History page. The
details of both orders were captured.
Note: The order details are being captured and stored in the database that is
running on the LAMP instance that you launched.