Link Search Menu Expand Document

Create a Web Server

Lesson 2

Explanation with metaphor about what SSH is. Basic, high level info about root user v ec2-user.

For a hands-on approach to SSH, we are going to turn your EC2 instance into a web server.

There are multiple ways to turn your EC2 instance into a web server. We are going to go through two ways: a) SSHing into the instance and running the commands directly, and b) adding the commands to our terraform code to take a lot of the manual work out of it. We suggest doing both! The more manual way (while you won’t use it often in this course) gives you a great introduction into running terminal commands on your EC2 instance, and allows you to actually see how to SSH.

Table of contents

  1. Create a Web Server
  2. Update your security group rules
  3. Let’s make a web server - SSH
    1. Run terraform apply
    2. SSH into your EC2
    3. Run terraform destroy
  4. Let’s make a web server - Terraform code
    1. Add user data to your instance
    2. Run terraform apply
    3. Run terraform destroy
  5. Next Steps
    1. Redo this lesson (optional)
    2. Move On

Update your security group rules

For this lesson, you will be working in the AWS console. AWS provides a nifty tool that allows you to SSH into your EC2 instance easily. From there, we will be setting up the EC2 instance as a web server, so that you can see changes in your browser.

First, though, you will need to open up you security group (think of a security group like a firewall, and we want to allow everything in for now). You’ll learn more about security groups later, so just follow along with the steps and don’t worry about memorizing everything.

  1. Log in to the AWS console.
  2. Navigate to the EC2 console (Click Services in the top left –> Search for EC2).
  3. Click on Instances from the left-hand pane, or from the main EC2 screen.
  4. Click the link to the Instance ID.
  5. Click the Security tab, then click the link to the security group underneath Security groups.
  6. Under Inbound rules, click Edit inbound rules.
  7. Click Add rule with the following parameters:
    • For Type - Choose All traffic
    • For Source - Choose 0.0.0.0/0 (this is a cidr block, which you will learn about later. This cidr block means to all traffic from anywhere (any computer, site, etc))
  8. You can also delete any rules other than the one you just added, but it isn’t necessary.
  9. Click Save rules in the bottom right.

sec-group-rules

Let’s make a web server - SSH

This is the more manual way to create an EC2 web server. You will SSH into the EC2 instance and run commands that download and update packages that make it possible to serve data through the web.

Run terraform apply

If you ran terraform destroy like we suggested at the end of the last lesson, you’ll now need to run terraform apply from your ec2-terraform project directory again. This will launch a new EC2 instance into your AWS account. For full instructions, you can go back to the previous lesson and review.

SSH into your EC2

Because your security group (or firewall) is allowing all internet and SSH traffic, you can SSH into your instance.

  1. Navigate back to your instance by clicking Instances on the left navigation pane, then click the link under Instance ID again.
  2. Now we can connect to our instance! Click Connect in the top right.
  3. Choose EC2 Instance Connect, then click Connect.
  4. You should get a screen like the one below in a new browser tab! connect-instance

Now we are going to take this "blank" EC2 and turn it into a web server with just a few commands. In the browser tab AWS opened for you when you connected, you’ll need to follow the steps and run the commands below.

  1. Run the command
     sudo su
    
    • This command will allow you to run all commands as the root user. sudo-su
  2. Run the command
     yum install update
    
    • This command will install the most updated version of all applications the current EC2 already has. yum-install-update
  3. Run the command
     yum install httpd -y
    
    • This will install the web part of the web server we are creating (it installs an apache web server). yum-install-httpd
  4. Run the command
     cd /var/www/html/
    
    • This command will change into the html directory created when we ran the previous command.
  5. Run the command
     nano index.html
    
    • This command will open up the index.html file so that you can edit it. This is just like opening up a file (word doc, notepad, etc) on your computer to edit, but you’re doing it in the terminal.
  6. Copy the below code into the index.html file you have open.
     <html><h1> Hello Worldl! </h1></html>
    
  7. To exit the file, use the key combo: crtl + X, then type y (for yes), and hit the enter key. nano-index
  8. Run the command
     service httpd start
    
    • This command will start the web server and allow you to view it in your browser!
  9. Lastly, you can type exit (to exit the SSH as the root-user), then exit again, (to exit the SSH altogether). You will no longer be connected to the instance. start-exit
  10. Now, go back to the Instances UI and click on you instance. Find the Public IPv4 address and copy it, then paste it into a new browser tab. You should see your first Hello World! web server EC2 instance! hello-world

Run terraform destroy

Now that you’ve seen your EC2 in AWS, you should run terraform destroy. It’s always a good idea to clean up after yourself (and terraform makes it easy!) so that you aren’t charged unnecessarily for resources you don’t need running all the time.

  • In your terminal, make sure you are still in your ec2-terraform directory by typing pwd. It should print something similar to home/yourname/ec2-terraform. If you aren’t, cd into it.
  • Run the command:
    terraform destroy
    

    terraform-destroy

Let’s make a web server - Terraform code

Now that you’ve gotten your hands dirty once, we’re going to let terraform do the heavy-lifting from now on. In the previous example, we SSHed into our instance and manually typed out the commands necessary to turn our EC2 into a web server. With terraform, we can instead create an instance that will launch and run those commands itself!

Add user data to your instance

  • Go to your code editor and open up your ec2-terraform project.
  • Open the ec2.tf file.
  • In the "aws_instance" resource, underneath the key_name parameter, paste the following new parameter, user_data:
    user_data = <<-EOF
    #! /bin/bash
    yum update -y
    yum install httpd -y
    cd /var/www/html
    echo "<html><h1> Hello! Welcome to Learn the Cloud! </h1></html>" > index.html
    service httpd start
    EOF
    

    User data is information (such as a bash script) that will run when you launch an instance. You can enter this user data when you launch an EC2 instance in the AWS Console (on the Configuration step) or by SSHing into the instance and typing out the commands yourself, one by one (like you did previously in this lesson).

    Note: EOF: This is how terraform knows that the next information is not a new parameter, but part of the current parameter. You use EOF when you need to pass large amounts of text (like something that would be in a file) to a terraform parameter. In this case, we are passing in the content of a bash script. You need to start with <<-EOF and end with EOF to tell terraform everything in between should be considered text.

    Note: #! /bin/bash: The first line, #! /bin/bash, is the first line of all bash scripts (a bash script is just a set of commands that run consecutively in the terminal). Notice that all the other commands should be familiar, as they are the same ones you ran previously in this lesson.

Run terraform apply

You’ll now need to run terraform apply from your ec2-terraform project directory again. This will launch a new EC2 instance into your AWS account. For full instructions, you can go back to the previous lesson and review.

Now, just like before, you can go back to the Instances UI and click on you instance. Find the Public IPv4 address and copy it, then paste it into a new browser tab. You should see Hello! Welcome to Learn the Cloud! - a web server EC2 instance! hello-world

Run terraform destroy

Now that you’ve seen your EC2 in AWS, you should run terraform destroy. It’s always a good idea to clean up after yourself (and terraform makes it easy!) so that you aren’t charged unnecessarily for resources you don’t need running all the time.

  • In your terminal, make sure you are still in your ec2-terraform directory by typing pwd. It should print something similar to home/yourname/ec2-terraform. If you aren’t, cd into it.
  • Run the command:
    terraform destroy
    

    terraform-destroy

Next Steps

In this lesson, you:

  1. Launched an EC2 instance with terraform
  2. Updated your security group rules (you’ll learn more about this later, don’t worry!)
  3. SSh-ed into your EC2 instance and ran commands as the root user to set up a web server
  4. Learned how to use user data in terraform to automate #3!

Redo this lesson (optional)

If you would like to try this lesson again to get more comfortable with the process, you’ll need to do one thing:

  • Go delete the user_data parameter from your ec2.tf file, and save the file.

From there (as long as you ran terraform destroy when the lesson said to) you are good to go!

You can now go back to the start of the Lesson to practice!

Challenge: Instead of Hello World!, make the web server “print”, Hello, YourNameHere!!!.

Move On

If you feel good about this lesson and want to keep moving, go ahead and start on the next Lesson: Variables.