Hands-On Lab820 words

Hands-On Lab: Design Cost-Optimized Compute Solutions on AWS

Design cost-optimized compute solutions

Hands-On Lab: Design Cost-Optimized Compute Solutions on AWS

Welcome to this guided lab on designing and deploying cost-optimized compute solutions in AWS. As discussed in the AWS Certified Solutions Architect exam guide, leveraging the right purchasing options—such as Spot Instances—can drastically reduce your compute spend compared to On-Demand instances.

Prerequisites

Before starting this lab, ensure you have the following ready:

  • AWS Account: An active AWS account with administrative or AmazonEC2FullAccess permissions.
  • AWS CLI: Installed and configured (aws configure) with your Access Key, Secret Key, and a default region (e.g., us-east-1).
  • Prior Knowledge: Basic understanding of Amazon EC2 instance types and SSH concepts.

Learning Objectives

By completing this 30-minute lab, you will be able to:

  1. Use AWS pricing tools to compare On-Demand and Spot Instance costs.
  2. Provision a cost-optimized EC2 Spot Instance using the AWS CLI and Management Console.
  3. Validate the lifecycle state and purchasing option of your compute resources.

Architecture Overview

This lab provisions an EC2 instance using the Spot market, which utilizes spare AWS capacity at a significant discount.

Loading Diagram...

Step-by-Step Instructions

Step 1: Create an SSH Key Pair

To securely access your compute resource, you first need to create an SSH key pair.

bash
aws ec2 create-key-pair \ --key-name cost-optimized-key \ --query "KeyMaterial" \ --output text > cost-optimized-key.pem

[!TIP] On Linux/macOS, restrict permissions to secure the file by running: chmod 400 cost-optimized-key.pem

Console alternative
  1. Navigate to EC2 Dashboard > Key Pairs.
  2. Click Create key pair.
  3. Enter cost-optimized-key as the name, choose RSA, and .pem format.
  4. Click Create key pair to download the file.

📸 Screenshot: EC2 Create Key Pair form with name entered.

Step 2: Retrieve the Latest Amazon Linux 2 AMI

Instead of hardcoding an AMI ID (which changes per region), use the AWS Systems Manager (SSM) parameter store to dynamically fetch the latest Amazon Linux 2 AMI.

bash
aws ssm get-parameters \ --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 \ --query "Parameters[0].Value" \ --output text

[!TIP] Copy the output ami-0xxxxxxxxxxxxxxxxx to your clipboard; you will need it for the next step.

Console alternative
  1. Navigate to EC2 Dashboard > AMI Catalog.
  2. Under Quick Start AMIs, locate Amazon Linux 2 AMI (HVM).
  3. Note down the AMI ID shown below the title.

📸 Screenshot: AMI Catalog highlighting the Amazon Linux 2 AMI ID.

Step 3: Launch a Cost-Optimized Spot Instance

Now, launch an EC2 instance requesting the Spot market purchasing option. This is the core of cost-optimizing variable compute workloads.

bash
aws ec2 run-instances \ --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 \ --instance-type t3.micro \ --key-name cost-optimized-key \ --instance-market-options '{"MarketType":"spot"}' \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=CostOptimizedCompute}]'

[!TIP] The --instance-market-options flag is what instructs AWS to fulfill this request using spare, discounted Spot capacity instead of On-Demand capacity.

Console alternative
  1. Navigate to EC2 Dashboard > Instances > Launch instances.
  2. Name: Enter CostOptimizedCompute.
  3. Application and OS Images: Select Amazon Linux 2.
  4. Instance type: Select t3.micro.
  5. Key pair: Select cost-optimized-key.
  6. Expand Advanced details.
  7. Check the box for Request Spot Instances.
  8. Click Launch instance.

📸 Screenshot: The "Advanced details" section with "Request Spot Instances" checked.

Checkpoints

Let's verify that your compute resource was successfully provisioned as a Spot instance and is running correctly.

Checkpoint 1: Verify Instance State

Run the following command to check if the instance is actively running:

bash
aws ec2 describe-instances \ --filters "Name=tag:Name,Values=CostOptimizedCompute" \ --query "Reservations[*].Instances[*].State.Name" \ --output text

Expected Output: running

Checkpoint 2: Verify Purchasing Lifecycle

Ensure that you are actually receiving the cost-optimized Spot pricing by checking the instance lifecycle attribute:

bash
aws ec2 describe-instances \ --filters "Name=tag:Name,Values=CostOptimizedCompute" \ --query "Reservations[*].Instances[*].InstanceLifecycle" \ --output text

Expected Output: spot

Clean-Up / Teardown

[!WARNING] Remember to run the teardown commands to avoid ongoing charges. Even though Spot instances are highly discounted, they still incur costs per hour.

Step 1: Terminate the Spot Instance

Find your Instance ID and terminate it to stop billing:

bash
# Find Instance ID aws ec2 describe-instances \ --filters "Name=tag:Name,Values=CostOptimizedCompute" "Name=instance-state-name,Values=running" \ --query "Reservations[*].Instances[*].InstanceId" \ --output text # Terminate Instance (Replace <YOUR_INSTANCE_ID> with output from above) aws ec2 terminate-instances --instance-ids <YOUR_INSTANCE_ID>

Step 2: Delete the Key Pair

Remove the SSH key pair from your account:

bash
aws ec2 delete-key-pair --key-name cost-optimized-key

Troubleshooting

If you run into issues during the lab, reference this table for common errors and their solutions:

Error Message / BehaviorCauseSolution
InvalidKeyPair.NotFoundThe CLI cannot locate the key pair you specified.Ensure you ran the create-key-pair command in the exact same region where you are launching the instance.
InsufficientInstanceCapacityAWS does not have enough spare t3.micro capacity in your target Availability Zone for a Spot instance.Try changing the instance type to t2.micro or t3.small, or try again in a few minutes.
Instance immediately transitions to terminatedYour default bid price may be lower than the current Spot market price.AWS automatically bids the On-Demand price by default, but if you specified a custom maximum price, remove it to ensure fulfillment.
UnauthorizedOperationYour IAM user lacks permissions to launch EC2 instances.Attach the AmazonEC2FullAccess policy to your IAM user or role.

Ready to study AWS Certified Solutions Architect - Associate (SAA-C03)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free