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
AmazonEC2FullAccesspermissions. - 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:
- Use AWS pricing tools to compare On-Demand and Spot Instance costs.
- Provision a cost-optimized EC2 Spot Instance using the AWS CLI and Management Console.
- 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.
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.
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
- Navigate to EC2 Dashboard > Key Pairs.
- Click Create key pair.
- Enter
cost-optimized-keyas the name, choose RSA, and .pem format. - 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.
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-0xxxxxxxxxxxxxxxxxto your clipboard; you will need it for the next step.
▶Console alternative
- Navigate to EC2 Dashboard > AMI Catalog.
- Under Quick Start AMIs, locate Amazon Linux 2 AMI (HVM).
- 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.
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-optionsflag is what instructs AWS to fulfill this request using spare, discounted Spot capacity instead of On-Demand capacity.
▶Console alternative
- Navigate to EC2 Dashboard > Instances > Launch instances.
- Name: Enter
CostOptimizedCompute. - Application and OS Images: Select Amazon Linux 2.
- Instance type: Select
t3.micro. - Key pair: Select
cost-optimized-key. - Expand Advanced details.
- Check the box for Request Spot Instances.
- 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:
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=CostOptimizedCompute" \
--query "Reservations[*].Instances[*].State.Name" \
--output textExpected Output: running
Checkpoint 2: Verify Purchasing Lifecycle
Ensure that you are actually receiving the cost-optimized Spot pricing by checking the instance lifecycle attribute:
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=CostOptimizedCompute" \
--query "Reservations[*].Instances[*].InstanceLifecycle" \
--output textExpected 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:
# 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:
aws ec2 delete-key-pair --key-name cost-optimized-keyTroubleshooting
If you run into issues during the lab, reference this table for common errors and their solutions:
| Error Message / Behavior | Cause | Solution |
|---|---|---|
InvalidKeyPair.NotFound | The 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. |
InsufficientInstanceCapacity | AWS 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 terminated | Your 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. |
UnauthorizedOperation | Your IAM user lacks permissions to launch EC2 instances. | Attach the AmazonEC2FullAccess policy to your IAM user or role. |