(Optional) - Launching an EC2 Spot Instance via the RunInstances API

STOP Please note: This workshop version is now deprecated, and an updated version has been moved to AWS Workshop Studio. This workshop remains here for reference to those who have used this workshop before for reference only. Link to updated workshop is here: Launching EC2 Spot Instances.

We strongly discourage using the RunInstances API for launching Spot Instances because doesn’t allow you to specify a replacement strategy or an allocation strategy. Remember that by specifying multiple Spot capacity pools you can apply instance diversification and by using price-capacity-optimized allocation strategy, Amazon EC2 will automatically launch Spot Instances from the most optimal capacity pools with low prices. This is why we recommended to use EC2 Fleet in instant mode as a drop-in replacement for RunInstances API or use Auto Scaling group to manage the instance lifecycle.

RunInstances API allows you to launch one or more instances, using a launch template that you have previously configured. Typically you would use the RunInstances API to launch one or more instances of the same type in situations where you are not planning to replace or manage the instances as a group entity.

RunInstance example: Launching a single instance

  1. To launch a Spot Instance with RunInstances API you create below configuration file:
cat <<EoF > ./runinstances-config.json
{
    "MaxCount": 1,
    "MinCount": 1,
    "InstanceType": "c5.large",
    "LaunchTemplate": {
        "LaunchTemplateId":"${LAUNCH_TEMPLATE_ID}",
        "Version": "1"
    },
    "InstanceMarketOptions": {
        "MarketType": "spot"
    },
    "TagSpecifications": [
        {
            "ResourceType": "instance",
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "EC2SpotWorkshopRunInstance"
                }
            ]
        }
    ]
}
EoF
  1. Run this command to submit the RunInstances API request:
aws ec2 run-instances --cli-input-json file://runinstances-config.json

If the request is successful, you should see an output with the description of the instances that have been launched.

Challenges

Given the configuration you used above, try to answer the following questions. Click to expand and see the answers.

In some applications where RunInstances is used to create and manage instances replacing the RunInstance API with EC2 Fleet API helps to ensure that Spot best practices are enforced while making the minimum modifications to the application. The fact the EC2 Fleet API is also synchronous makes it more reliable than polling with asynchronous RunInstances APIs.

1. Do you know how to replace this Spot Instance with same launch parameters as above example with EC2 Fleet?