> ## Documentation Index
> Fetch the complete documentation index at: https://cloud-architect.ipoint-labs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Launch a Spot Instance with Amazon EC2

> Hands-on lab · 45m

Launch this lab in the IP Lab Portal, then follow the steps below in the AWS console.

<a className="ip-lab-portal-btn" href="https://labs.intellectualpoint.com/labs/launch-a-spot-instance-with-amazon-ec2" target="_blank" rel="noreferrer">
  Open IP Lab Portal
</a>

## Overview

### Lab details

1. This lab walks you through the steps to launch an EC2 Spot Instance using the AWS Management Console. It will help learners to understand the differences between higher and lower spot instance prices and how they can set the maximum price for spot instances
2. Duration: **1 hour**
3. AWS Region: **US East (N. Virginia) us-east-1**

### Introduction

#### What is EC2 Spot Instance

* EC2 Spot Instances allow you to bid on unused EC2 capacity in a specific AWS region.
* The hourly cost of an EC2 Spot Instance can fluctuate depending on supply and demand.
* You can save up to 90% on EC2 instance costs compared to On-Demand pricing.
* EC2 Spot Instances can be terminated by AWS at any time if the Spot price goes above your bid price, or if AWS needs the capacity for other purposes.
* You can use EC2 Spot Instances for fault-tolerant and flexible workloads that can handle interruptions, such as batch processing, data analysis, and testing environments.
* EC2 Spot Instances can be launched using the same process as regular EC2 instances, and can be integrated with other AWS services like Auto Scaling Groups and Spot Fleets.
* If your EC2 Spot Instance is terminated by AWS, you will not be charged for the partial hour of usage. However, if you terminate the instance yourself, you will be charged for the entire hour.

### Architecture diagram

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/launch-a-spot-instance-with-amazon-ec2/001.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=516f7ba460636f2efd3d0dcef2b5e7f7" alt="" width="1841" height="1050" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/001.png" />

### Task details

1. Sign into AWS Management Console.
2. Select an Amazon Linux Spot Instance from an Amazon Linux AMI.
3. Setting the price of a spot instance to Higher and lower values compared to a given value.
4. Launch the Spot Instance, to understand the difference between higher and lower prices.
5. Explore the Spot request, Saving Summary, and Pricing history options.
6. Test HTML page is launched or not using public IP.
7. Validation of the lab

### Launching the lab environment

1. To launch the lab environment, click on the **Launch lab** button.
2. Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
3. Once the Lab is started, you will be provided with **IAM username**, **Password**, **Access** **Key**, and **Secret** **Access** **Key**.

<Note>
  You can only start one lab at any given time
</Note>

## Lab guide

### Lab steps

#### Task 1: Sign in to AWS Management Console

1. Click on the **Open console** button, and you will get redirected to AWS Console in a new browser tab.
2. On the AWS sign-in page,

   * Leave the Account ID as default. Never edit/remove the 12-digit Account ID present in the AWS Console. Otherwise, you cannot proceed with the lab.
   * Now copy your **Username** and **Password** in the Lab Console to the **IAM Username and Password** in AWS Console and click on the **Sign-in** button.
3. Once Signed In to the AWS Management Console, make the default AWS Region as **US East (N. Virginia) us-east-1.**

#### Task 2: Launching an EC2 Instance

In this task, we will launch and manage an EC2 Spot Instance, while also gaining an understanding of how spot pricing works and learning how to set a maximum price for instances in order to save costs

1. Make sure you are in **US East (N. Virginia) us-east-1** Region.
2. Navigate to **EC2** by clicking on the **Services** menu in the top, then click on **EC2** in the **Compute** section.
3. Navigate to **Instances** on the left panel and click on **Launch instances** button.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/launch-a-spot-instance-with-amazon-ec2/002.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=21c13312641a28bf81c4cec669247e06" alt="" width="1615" height="316" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/002.png" />

4. Enter name as **MySpotInstance**
5. **Choose an Amazon Machine Image (AMI):** Search for **Amazon Linux 2023 AMI** in the search box and click on the **select** button.
6. **Choose an Instance Type:** Select ***t2.micro***

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/launch-a-spot-instance-with-amazon-ec2/003.gif?s=df827cebafcdcdf7346e484e53017276" alt="" width="1000" height="434" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/003.gif" />

7. For Key pair : Choose **Proceed without a key pair.**

8. Under **Network Settings,** Click on **Edit** button

* Auto-assign public IP : Select **Enable**
* Choose **Create security group**.
* Security group name: Enter **MyEC2Server\_SG**
* Description: Enter **Security Group to allow traffic to EC2**
* To add **SSH**,

  1. Choose Type: **SSH**
  2. Source: Select **Anywhere**
* For HTTP, Select Add Security rule Button

  1. Choose Type: **HTTP**
  2. Source:  Select **Anywhere**

     <img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/launch-a-spot-instance-with-amazon-ec2/004.gif?s=cb3b76ca368ef2c7c7bb760192382eda" alt="" width="1000" height="434" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/004.gif" />

9. Under **Advanced Details,**

* Purchasing option: Check the **Spot instance** option
* Click on ***Customize Spot instance options.***

<img src="https://mintcdn.com/ip-cloud-architect-pathway/f-UHpNVYYLl5zeE0/images/labs/launch-a-spot-instance-with-amazon-ec2/005.png?fit=max&auto=format&n=f-UHpNVYYLl5zeE0&q=85&s=5f395cc875281341e33be38eb17cbe8a" alt="" width="629" height="205" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/005.png" />

* Maximum price : Choose **Set your maximum price(per instance/hour)**
* Set \$ **0.003** as the price.
* Request Type : Choose **One-time**
* Valid to : Choose **No request expiry date.**

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/006.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=31d5eaacf9d04795f3e03484db448f95" alt="" width="786" height="447" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/006.png" />

* Persistent request: **we will not choose the option**, but you should know what that means. Below is the explanation

  A persistent Spot Instance request remains active until it expires, or you cancel it, even if the request is fulfilled. If capacity is not available, your Spot Instance is interrupted. After your instance is interrupted, when capacity becomes available again, the Spot Instance is started if stopped or resumed if hibernated. You can stop a Spot Instance and start it again if capacity is available. If the Spot Instance is terminated (irrespective of whether the Spot Instance is in a stopped or running state), the Spot Instance request is opened again and Amazon EC2 launches a new Spot Instance.

10. Under the **User data** section, enter the following script, (which creates an HTML page served by Apache)

\#!/bin/bash

```python theme={null}
sudo su
yum update -y
yum install httpd -y
echo "<html><h1> Welcome to Whizlabs Server</h1><html>" >> /var/www/html/index.html
systemctl start httpd
```

systemctl enable httpd

11. Keep rest thing Default and Click on **Launch Instance** Button.
12. **If you get an error message stating that your Maximum price is less than the current price shown in the Availability zone and Current Price table, click on "Edit instance config" and set the Maximum price to \$0.01, then click on "Launch Instances."**

    <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/007.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=49485f451f03dcf1994b85db3ff934ac" alt="" width="1800" height="500" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/007.png" />
13. Wait for the instances to launch. Navigate to the Instances page from the left menu and wait for the status of the EC2 Instance to change to "**running**" and the health check status to change to “**2/2 checks passed**.”

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/008.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=e68995c2e4f09c601498d02cb6e2d11f" alt="" width="1752" height="216" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/008.png" />

14. To test if the HTML page is published, copy the **Public IPv4 Address** of your EC2 Instance and run it on the browser. Make sure the URL Protocol is **http** and not **https**.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/009.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=55999357a5990a28e7f3e0f719dc4f7f" alt="" width="1018" height="226" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/009.png" />

#### Task 3: View the Spot Request

1. Click on **Spot Requests** in the left sidebar, you will see the Spot requests.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/010.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=76e5f45cdeb244faac77856f76476312" alt="" width="2108" height="208" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/010.png" />

2. Click on the Request ID, to see more details about your spot request.

* **Max price:** The maximum price you are willing to pay per hour for the Spot instance. Spot instances are available at variable prices, and your request will be fulfilled as long as the current price is below your specified maximum price.
* **Instance ID:** This is the unique identifier for the specific instance you are requesting. It can be specified if you have a preference for a particular instance type or configuration.
* **Interruption:** Interruptions occur when the Spot price goes above your specified maximum price or when Amazon EC2 needs the capacity back for On-Demand instances. When an interruption occurs, your Spot instance will be terminated, and you will receive a notification before it happens.
* **Availability Zone:** Current Price of the Spot instance varies by Availability Zone, There might be a possibility that in other AZ you can get the same instance at a lower price.

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/011.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=84c6b4145d2a3f719c34e5ce85d7adee" alt="" width="1518" height="660" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/011.png" />

#### Task 4: Explore the Saving Summary and Pricing history

In this task, we are going to explore the different cost-saving options available when using EC2 Spot Instances

1. Click on the **Spot Requests** in the Left sidebar and you will see the Spot requests.

2. Let’s explore two options named **Spot saving summary** and **Pricing history.**

3. Below the Spot Request you'll be able to see the **Spot Saving Summary.** You will save a total of **75%** as compared to on-demand instances. Details are also available.

<Note>
  It's important to note that Spot prices are dynamic and can vary significantly over time. Therefore, savings percentages may continue to fluctuate based on market conditions and the utilization of your Spot instance.
</Note>

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/012.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=4797f417ed90ef80a29c37562e72d27f" alt="" width="1920" height="834" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/012.png" />

4. Now click on the **Pricing history** option on the top right.

5. Here you will see the pricing history, but wait a minute, it’s showing for some other Instance size, let’s modify the options.

* Graph: Availability Zones
* Instance type: Enter **t2.micro**
* Platform\*\*: Linux/UNIX\*\*
* Date Range\*\*: Select 3 months\*\*

<img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/013.png?fit=max&auto=format&n=sjxZZnuQfFzzjiqS&q=85&s=397422461c0b54a3ff2ef0c3c9ab0607" alt="" width="1449" height="728" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/013.png" />

<Tip>
  **Do you know?**

  Amazon announced a new feature called "AWS Spot Instance Draining" that helps customers manage stateful workloads running on Spot Instances. This feature enables customers to configure their instances to gradually wind down workloads instead of being abruptly terminated, giving applications time to complete important tasks and minimize disruption. This can be especially useful for applications that store data on local instance storage or perform other stateful operations.
</Tip>

#### Task 5: Validation Test

1. Once the lab steps are completed, please click on the **Check my work** button on the left side panel.
2. This will validate the resources in the AWS account and displays whether you have completed this lab successfully or not.
3. Sample output :

   <img src="https://mintcdn.com/ip-cloud-architect-pathway/sjxZZnuQfFzzjiqS/images/labs/launch-a-spot-instance-with-amazon-ec2/014.gif?s=33fc5af9813acc0498b067fc45553656" alt="" width="993" height="526" data-path="images/labs/launch-a-spot-instance-with-amazon-ec2/014.gif" />

### Completion and conclusion

1. You have successfully created and launched Amazon EC2 Spot Instance.
2. You have successfully created a webpage and published it.

### End lab

1. Sign out of AWS Account.
2. You have successfully completed the lab.
3. Once you have completed the steps, click on **End lab** in the IP Lab Portal.

## What gets checked

When you press **Check my work**, the platform verifies each of these:

* **Launch EC2 AMI type Amazon Linux** — Check whether the EC2 instance is launched using an Amazon AMI.
* **Install Apache Web Server** — Check whether Apache Web Server is installed in EC2 Instance or not.

## Related help

* [FAQs and Troubleshooting](/aws-cp/support/faqs-and-troubleshooting)
* [SSH into EC2 Instance](/aws-cp/support/ssh-into-ec2-instance)
