0
0
AWScloud~5 mins

Spot Instances for cost savings in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Spot Instances let you use spare cloud computing power at a lower price. They help save money but can be stopped by the cloud provider when needed.
When running batch jobs that can pause and resume without issues.
When testing applications that do not need constant uptime.
When you want to save money on flexible workloads like data analysis.
When running background tasks that can handle interruptions.
When you want to reduce cloud costs for development environments.
Config File - spot-instance-launch-template.json
spot-instance-launch-template.json
{
  "LaunchTemplateName": "my-spot-launch-template",
  "LaunchTemplateData": {
    "InstanceType": "t3.medium",
    "ImageId": "ami-0abcdef1234567890",
    "InstanceMarketOptions": {
      "MarketType": "spot",
      "SpotOptions": {
        "MaxPrice": "0.03",
        "SpotInstanceType": "one-time",
        "InstanceInterruptionBehavior": "terminate"
      }
    },
    "KeyName": "my-key-pair",
    "SecurityGroupIds": ["sg-0123456789abcdef0"]
  }
}

This JSON file defines a launch template for an AWS EC2 Spot Instance.

  • InstanceType: The size of the virtual machine.
  • ImageId: The ID of the operating system image.
  • InstanceMarketOptions: Configures the instance as a Spot Instance with a maximum price and interruption behavior.
  • KeyName: The SSH key to access the instance.
  • SecurityGroupIds: The firewall rules applied to the instance.
Commands
This command creates a launch template for Spot Instances using the JSON configuration file.
Terminal
aws ec2 create-launch-template --cli-input-json file://spot-instance-launch-template.json
Expected OutputExpected
{ "LaunchTemplate": { "LaunchTemplateId": "lt-0abcd1234efgh5678", "LaunchTemplateName": "my-spot-launch-template", "CreateTime": "2024-06-01T12:00:00.000Z", "CreatedBy": "arn:aws:iam::123456789012:user/my-user", "DefaultVersionNumber": 1 } }
--cli-input-json - Specifies the JSON file with launch template details
This command starts one Spot Instance using the launch template created earlier.
Terminal
aws ec2 run-instances --launch-template LaunchTemplateName=my-spot-launch-template --count 1
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "InstanceType": "t3.medium", "State": { "Code": 0, "Name": "pending" }, "LaunchTime": "2024-06-01T12:05:00.000Z" } ] }
--launch-template - Specifies the launch template to use
--count - Number of instances to launch
This command checks the status and details of the Spot Instance to confirm it is running.
Terminal
aws ec2 describe-instances --instance-ids i-0123456789abcdef0
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0123456789abcdef0", "InstanceType": "t3.medium", "State": { "Code": 16, "Name": "running" }, "LaunchTime": "2024-06-01T12:05:00.000Z" } ] } ] }
--instance-ids - Specifies which instance to describe
Key Concept

If you remember nothing else from this pattern, remember: Spot Instances save money by using spare cloud capacity but can stop anytime, so use them only for flexible tasks.

Common Mistakes
Trying to use Spot Instances for critical applications that must always run.
Spot Instances can be stopped by AWS at any time, causing downtime.
Use Spot Instances only for tasks that can handle interruptions or use On-Demand Instances for critical workloads.
Not setting a maximum price for Spot Instances.
Without a max price, you might pay more than expected or fail to get instances if the price rises.
Always set a reasonable max price in the launch template to control costs.
Not monitoring Spot Instance interruptions.
You might lose data or progress if the instance is terminated without preparation.
Use AWS tools or scripts to detect interruption notices and save work before termination.
Summary
Create a launch template specifying Spot Instance options and max price.
Launch Spot Instances using the created launch template.
Check instance status to confirm it is running and ready.
Remember Spot Instances can stop anytime, so use them for flexible workloads.