TL;DR
This guide shows you how to launch an EC2 Spot Instance using the AWS Management Console and CLI, saving money on compute costs. We’ll cover requesting instances, checking their status, and terminating them when finished.
Launching a Spot Instance via the AWS Management Console
- Navigate to EC2: Open the AWS Management Console and go to the EC2 service.
- Request Spot Instances: In the left-hand navigation pane, under ‘Instances’, click ‘Spot Instances’. Then, click ‘Request Spot Instances’.
- Configure Instance Details:
- Name and tags: Give your instance a descriptive name.
- AMI (Amazon Machine Image): Choose the operating system you need.
- Instance Type: Select an appropriate instance type based on your workload.
- Spot bidding strategy: Choose ‘Request with lowest price’.
- Number of instances: Specify how many Spot Instances you want to launch.
- Configure Capacity Optimised Allocation: Select whether or not to use capacity optimised allocation.
- Security Groups: Choose a security group that allows the necessary traffic to your instance.
- Key Pair (Login): Select an existing key pair, or create a new one if you don’t have one. This is essential for SSH access.
- Advanced Details: Configure user data if needed (e.g., scripts to run on startup).
- Review and Launch: Review your configuration and click ‘Request’.
Launching a Spot Instance via the AWS CLI
Before you start, make sure you have the AWS CLI installed and configured with appropriate credentials.
- Find an AMI ID: Use the
aws ec2 describe-imagescommand to find an AMI ID for your desired operating system. For example:aws ec2 describe-images --owners amazon --filters "Name=name,Values=*Amazon Linux 2*" --query 'Images[*].ImageId' --output text - Request a Spot Instance: Use the
aws ec2 request-spot-instancescommand. Replace placeholders with your values:aws ec2 request-spot-instances --image-id --instance-type --count --security-group-ids --key-name --launch-specification file://launch-spec.json - Create a Launch Specification (launch-spec.json): This JSON file defines the details of your instance.
{ "ImageId": "", "InstanceType": "", "SecurityGroupIds": [""], "KeyName": "", "UserData": "#cloud-confignpackage:n update: truenwrite_files:n - path: /tmp/startup.shn permissions: '0755'n owner: rootn group: rootnruncmd:n - /tmp/startup.sh" } - Check Spot Instance Request Status: Use the
aws ec2 describe-spot-instance-requestscommand to check the status of your request:aws ec2 describe-spot-instance-requests --request-ids
Checking Instance Status
Regardless of whether you use the console or CLI, you can check the status of your Spot Instances in the EC2 Management Console under ‘Instances’. Look for instances with a state of ‘running’ and an instance type that matches your request.
Terminating a Spot Instance
- Select the Instance: In the EC2 Management Console, select the Spot Instance you want to terminate.
- Terminate Instance: Right-click on the instance and choose ‘Instance State’ -> ‘Terminate’. Confirm your choice when prompted.