← Cloud Practitioner study path

Practice in your account · 30 min

Launch and terminate a web server on Amazon EC2

In this lab you will launch an EC2 instance that installs a web server when it boots, open its page in your browser and terminate it so it stops charging.

In this lab you will launch an EC2 instance, which is a virtual machine: a computer that AWS lends you inside its own computers and that you use over the internet. You will launch it with Amazon Linux, which is its , and with a that installs a web server when it boots. A web server is a program that delivers pages to whoever asks for them from a browser. For example, when you open an online store's page, a web server sends it to you; in this lab, the server will send a simple page that says where in AWS it runs.

Then you will open that server's page from your browser, and at the end you will terminate the instance so it stops charging. With that you go through the states of an instance, from when you launch it until you delete it, and you will know in which ones it charges.

EC2 instance states

EC2 instance states and when you are charged

An instance goes through several states during its life, and instance usage is only charged in the running state, which means it is on, whether you use it or not. When you stop it, it moves to stopped, which is like turning it off: instance usage stops being charged, but its EBS volume, which is its disk, still keeps your files and keeps charging. When you terminate it, it moves to terminated: the instance is deleted along with its root volume, which is the disk that holds its operating system, and that action can't be undone.

For example, if an online store uses an instance for testing and stops it on Friday, on Monday it starts it again and finds it just as it was, with the files it had. If instead it terminates it on Friday, on Monday it no longer exists, and to keep testing the store has to launch a new instance and install everything again.

What do you do with the instance?

You are going away for two weeks and when you come back you want to keep using the same instance. In the meantime you want to pay as little as possible. What do you do?

Before you start

Do the zero spend budget lab first, so the email warns you if something starts charging. In the full account, check the Region at the top right and use the same one when you delete the instance, because the console only shows you the instances in the selected Region. For example, if you launch the instance in Ohio and another Region is selected when you finish the lab, you won't see it in the list, even though it is still on. In the new experience, your project is already in us-east-2 and there is no selector.

On the free plan, this lab spends credit and doesn't charge you. On the paid plan and with no credit, EC2 charges from the moment you launch the instance until you terminate it, even if it is idle.

cloud path tip: if you do this lab from the Explore AWS widget in Console Home, launching the instance counts as one of the activities that give you extra credit.

Steps in the console

The console's launch wizard is a single page with several sections, and in each one you decide one part of the instance: its name, its operating system, its size, how it connects to the internet and what it does when it boots. Fill them in order:

  1. Open the Amazon EC2 console (https://console.aws.amazon.com/ec2/).
  2. In the Launch instance panel, choose Launch instance.
  3. In Name and tags, enter cloudpath-lab05-web as the name. Choose "Add additional tags" and add the tag cloudpath:lab with the value lab05. The tag is a note that helps you find the instance later.
  4. In Application and OS Images, leave Quick Start with Amazon Linux and an marked Free tier eligible.
  5. In Instance type, choose t3.micro. The type decides how big the computer is, that is, how much memory and how much computing capacity it has. For accounts created since July 15, 2025, the free tier eligible types are t3.micro, t3.small, t4g.micro, t4g.small, c7i-flex.large and m7i-flex.large.
  6. In Key pair (login), choose "Proceed without a key pair (Not recommended)". The key pair is used to connect to the instance over , and in this lab you don't need it, because you will only look at its web page.
  7. In Network settings, check that the default is selected and that auto-assign public IP is enabled, so the instance has an address on the internet. In Firewall (security groups), create a new group, remove the SSH rule and check "Allow HTTP traffic from the internet" ( 80). A security group is the list of connections the instance lets in; with this one, anyone can see the web page, but nobody can get in to control the instance.
  8. Open Advanced details and paste this into User data:

bash #!/bin/bash dnf install -y httpd TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") ZONA=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone) echo "<h1>cloud path, lab 5</h1><p>Esta instancia corre en la zona $ZONA</p>" > /var/www/html/index.html systemctl enable --now httpd

This script runs only once, on the first boot. It installs Apache, which is the web server program, asks the instance which it is in and writes that zone on the page. 9. Review the Summary and choose Launch instance. 10. In the success message, choose the instance ID. Wait for the state to change from pending to running and for the status checks to pass (Status and alarms tab), which are the tests AWS uses to check that the instance booted correctly. 11. Copy the instance's Public IPv4 DNS and open it in your browser with http:// in front, not https://. That name is the instance's name on the internet, and you use http:// because the server only opened port 80. The page may take a couple of minutes to respond while the instance finishes installing Apache.

In a new experience account, your project may not have a default VPC. If no VPC appears in step 7, cancel the launch and use the CloudFormation option, which creates its own network.

CloudFormation option

CloudFormation creates resources from a text file called a template, and unlike the console wizard, which uses the default VPC, the [lab05-ec2-web.yaml](/roadmaps/plantillas/lab05-ec2-web.yaml) template creates its own network. That network is made up of a VPC, a public subnet with its route table to the internet gateway and a security group that only lets HTTP in (no SSH). The subnet is a part of the network, the route table says which way connections go out and the internet gateway is the VPC's exit to the internet. For example, without the internet gateway the instance would exist, but nobody could open its page from a browser.

Inside that network, the template launches the t3.micro instance with the same script, and everything carries the tag cloudpath:lab = lab05. To use it, follow these steps:

  1. In the CloudFormation console, choose Create stack and With new resources (standard).
  2. Choose Upload a template file, upload lab05-ec2-web.yaml and choose Next.
  3. Stack name: cloudpath-lab05-ec2. Leave TipoDeInstancia set to t3.micro.
  4. Choose Next twice and then Submit.
  5. When the stack reaches CREATE_COMPLETE, go to the Outputs tab and open UrlDelServidor.

How to know you are done

The page has to say "cloud path, lab 5" and the zone where the instance runs, for example us-east-2a. You can also check the instance from CloudShell, a window inside the console where you type commands, with this command, which looks for the instances that carry the lab's tag:

aws ec2 describe-instances \
  --filters "Name=tag:cloudpath:lab,Values=lab05" \
  --query "Reservations[].Instances[].[InstanceId,State.Name,InstanceType,Placement.AvailabilityZone,PublicDnsName]" \
  --output table

With the DNS that command returns, check that the page responds:

curl -s http://<DNS público>

That command requests the page just like the browser does, but shows you its text in CloudShell. After you delete the instance, the same describe-instances has to show it as terminated. It may also no longer appear, because terminated instances drop off the list after a while.

Delete what you created

The instance keeps charging while it is running, so terminate it when you finish the lab. If you launched it from the console, follow these steps:

  1. In Instances, select cloudpath-lab05-web.
  2. Choose Instance state and Terminate (delete) instance.
  3. Confirm with Terminate (delete).

The root volume is deleted with the instance, because it was created when you launched it. Since terminating can't be undone, if you want to use the instance again another day, stop it instead of terminating it: instance usage stops being charged, although the volume keeps charging. For example, if you plan to repeat the lab next week with the same page, stopping it saves you from installing Apache again; if you won't use it anymore, terminate it so everything stops charging.

If you used the template, delete the stack cloudpath-lab05-ec2. When you delete it, CloudFormation terminates the instance and deletes the VPC, the subnet and the security group.

True or false?

0 of 4
  1. Stopping the instance stops everything in the lab from charging.

  2. The root volume is deleted when you terminate the instance.

  3. If you terminate the instance by mistake, you can recover it.

  4. Deleting the template's stack also deletes the VPC, the subnet and the security group.

Next

  • Get started with Amazon EC2: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html
  • cloud path practice exams: /examenes?lang=en