Chapter 10. Configure AWS Spot instances for Red Hat OpenShift Service on AWS using the CLI
Create a Red Hat OpenShift Service on AWS cluster or update an existing one using the ROSA command-line interface (CLI) (rosa) that is configured for AWS Spot instances.
10.1. Prerequisites Copy linkLink copied to clipboard!
Before configuring Spot instances for Red Hat OpenShift Service on AWS clusters, you must meet the following prerequisites:
- You have available AWS service quotas.
- You have enabled Red Hat OpenShift Service on AWS in the AWS console.
-
You have installed and configured the latest
rosaCLI on your installation host. Runrosa versionto see your currently installed version of therosaCLI. If a newer version is available, the CLI provides a link to download this upgrade. -
You have logged into your Red Hat account by using the
rosaCLI. - You have created an OIDC configuration.
- You have verified that the AWS Elastic Load Balancing (ELB) service role exists in your AWS account.
- You have installed and configured the AWS CLI with appropriate credentials.
10.2. Spot instances on Red Hat OpenShift Service on AWS Copy linkLink copied to clipboard!
AWS Spot instances allow you to use spare EC2 capacity at significantly reduced costs compared to on-demand instances. Red Hat OpenShift Service on AWS supports Spot instances for worker node machine pools, enabling cost optimization for fault-tolerant and stateless workloads.
Red Hat OpenShift Service on AWS provides two operational modes for Spot instance termination handling:
| Mode | Description | AWS infrastructure required | Graceful drain |
|---|---|---|---|
| Simple |
Spot instances work with no additional AWS setup. When AWS reclaims a Spot instance, the node is terminated abruptly. A | None | No |
| Enhanced |
Requires a customer-configured SQS queue and EventBridge rules. The AWS Node Termination Handler receives an approximately 2m warning before interruption and gracefully drains pods, respecting | SQS queue, EventBridge rules, SQS resource policy | Yes |
Enhanced Spot termination handling requires three AWS components:
- A Simple Queue Service (SQS) queue to receive interruption events.
- EventBridge rules to route EC2 Spot events to the queue.
- An SQS queue resource policy that grants access to EventBridge and the cluster’s NodePoolManagement role.
Use enhanced mode for any workload that benefits from graceful shutdown before Spot interruptions.
10.3. Set up the AWS infrastructure for Spot instances automatically Copy linkLink copied to clipboard!
You can set up the required AWS infrastructure using the automated rosa CLI helper. This helper command creates all the required AWS resources in a single step.
These resources include:
- The SQS queue
-
The
red-hat=truetag - The EventBridge rules
- The SQS queue resource policy
This process differs from the manual process when setting up the EventBridge rules: the automated rosa CLI helper combines the necessary interruption warning and rebalance recommendation into a single rule using CloudFormation. Setting up the AWS infrastructure manually requires defining two separate rules: one for interruption handling, and one for the rebalance recommendation.
Procedure
Run the following command to create the necessary AWS resources:
$ rosa create spot-termination-queue --name <cluster-name> --nodepool-management-role-arn <arn> --mode autoNoteThis command produces a queue URL that is neccessary for cluster creation,
rosa create cluster --spot-termination-queue-url, or updating your cluster,rosa edit cluster --spot-termination-url, so that your SQS queue and cluster are associated.
10.4. Set up the AWS infrastructure for Spot instances manually Copy linkLink copied to clipboard!
You can set up the required AWS infrastructure manually by creating the AWS resources individually.
This process differs from the automated process when setting up the EventBridge rules: the automated ROSA command-line interface (CLI) (rosa) helper combines the necessary interruption warning and rebalance recommendation into a single rule using CloudFormation. Setting up the AWS infrastructure manually requires defining two separate rules: one for interruption handling, and one for the rebalance recommendation.
Procedure
Create the SQS queue by running the following command:
$ QUEUE_URL=$(aws sqs create-queue \ --queue-name "$<cluster-name>-spot-interruption-queue" \ --region "us-east-1" \ --output text --query 'QueueUrl')Tag the SQS queue with the
red-hat=truetag:$ aws sqs tag-queue-resource \ --queue-url "${QUEUE_URL}" \ --tags Key=red-hat,Value=trueImportantYou must include this tag. Omitting this tag will cause the AWS Node Termination Handler to fail when polling the queue.
Retrieve the queue ARN:
$ ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) && \ QUEUE_ARN="arn:aws:sqs:us-east-1:${ACCOUNT_ID}:<cluster-name>-spot-interruption-queue"Create two separate EventBridge rules:
Create one rule for Spot interruption warnings:
$ aws events put-rule \ --name "<cluster-name>-spot-interruption" \ --event-pattern '{"source": ["aws.ec2"], "detail-type": ["EC2 Spot Instance Interruption Warning"]}' \ --region "us-east-1"Create another rule for rebalance recommendations:
$ aws events put-rule \ --name "<cluster-name>-rebalance-recommendation" \ --event-pattern '{"source": ["aws.ec2"], "detail-type": ["EC2 Instance Rebalance Recommendation"]}' \ --region "us-east-1"
Set the EventBridge target so both rules are routed to the SQS queue:
$ aws events put-targets \ --rule "<cluster-name>-spot-interruption" \ --targets "Id=1,Arn=${QUEUE_ARN}" \ --region "us-east-1"$ aws events put-targets \ --rule "<cluster-name>-rebalance-recommendation" \ --targets "Id=1,Arn=${QUEUE_ARN}" \ --region "us-east-1"Apply the SQS queue resource policy:
This policy grants access to two principals:
- EventBridge
- to send interruption events to the queue
- NodePoolManagement role
to receive and delete messages from the queue
Determine the NodePoolManagement role ARN. The ARN typically follows this pattern:
arn:aws:iam::<account-id>:role/ManagedOpenShift-HCP-ROSA-NodePoolManagement-RoleOptional: Retrieve the ARN from an existing cluster:
$ aws iam list-roles --query 'Roles[?contains(RoleName, `kube-system-capa-controller-manager`)].Arn' --output textSet the role ARN and apply the policy:
$ NODEPOOL_ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/ManagedOpenShift-HCP-ROSA-NodePoolManagement-Role"$ cat > /tmp/sqs-policy.json <<EOF { "Version": "2012-10-17", "Statement": [ { "Sid": "AllowNodePoolManagementRole", "Effect": "Allow", "Principal": { "AWS": "${NODEPOOL_ROLE_ARN}" }, "Action": [ "sqs:DeleteMessage", "sqs:ReceiveMessage" ], "Resource": "${QUEUE_ARN}" }, { "Sid": "AllowEventBridgeToSendMessages", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sqs:SendMessage", "Resource": "${QUEUE_ARN}" } ] } EOF$ aws sqs set-queue-attributes \ --queue-url "${QUEUE_URL}" \ --attributes "Policy=$(cat /tmp/sqs-policy.json)"NoteThis resource policy provides defense-in-depth alongside the managed IAM policy. The IAM policy restricts access by the tag
red-hat=true. The resource policy restricts access by principal ARN.
10.5. Create a new Red Hat OpenShift Service on AWS cluster using the CLI with Spot instances Copy linkLink copied to clipboard!
After you have set up your AWS resources including the SQS queue, EventBridge rules, and associated policy, you can create a new cluster. The process for simple mode and enhanced mode differ by the omission or inclusion respectively of the --spot-termination-queue-url flag.
Each Red Hat OpenShift Service on AWS cluster must use a dedicated Spot termination queue. Do not reuse the same SQS queue URL for multiple clusters. Create a separate queue for each cluster using the rosa create spot-termination-queue command.
Procedure
To create a cluster with Spot instances in simple mode, no additional AWS resources are required. Create the cluster without the
--spot-termination-queue-urlflag:$ rosa create cluster --cluster-name=<cluster-name> \ --mode=auto --hosted-cp [--private] \ --operator-roles-prefix <operator-role-prefix> \ --external-id <external-id> \ --oidc-config-id <id-of-oidc-configuration> \ --subnet-ids=<public-subnet-id>,<private-subnet-id>WarningIn simple mode, Spot instances are not gracefully drained upon interruption. Nodes are terminated abruptly when AWS reclaims capacity. A
MachineHealthCheckprovisions replacement nodes reactively.To create a cluster with Spot instances in enhance mode, create the cluster with a reference to the SQS queue:
$ rosa create cluster --cluster-name=<cluster-name> \ --mode=auto --hosted-cp [--private] \ --operator-roles-prefix <operator-role-prefix> \ --external-id <external-id> \ --oidc-config-id <id-of-oidc-configuration> \ --subnet-ids=<public-subnet-id>,<private-subnet-id> \ --spot-termination-queue-url "${QUEUE_URL}"The AWS Node Termination Handler is automatically created in the hosted control plane namespace once the cluster is ready and at least one Spot NodePool exists.
10.6. Create a Spot machine pool Copy linkLink copied to clipboard!
You can create Spot machine pools for new clusters, or existing ones. This process is identical for both simple and enhanced modes.
Procedure
Create a Spot machine pool with fixed replicas:
$ rosa create machinepool --cluster=<cluster-name> \ --name spot-workers \ --instance-type m5.xlarge \ --replicas 3 \ --use-spot-instances \ --spot-max-price 0.50Alternatively, create a Spot machine pool with autoscaling:
$ rosa create machinepool --cluster=<cluster-name> \ --name spot-workers \ --instance-type m5.xlarge \ --enable-autoscaling \ --min-replicas 2 \ --max-replicas 10 \ --use-spot-instances \ --spot-max-price 0.75NoteOmitting the
--spot-max-priceflag defaults to the on-demand price.
10.7. Upgrade your Red Hat OpenShift Service on AWS cluster with Spot instances from simple to enhance mode Copy linkLink copied to clipboard!
You can upgrade an existing simple-mode cluster to enhanced mode by adding the AWS resources and associate the SQS queue URL with the cluster.
Prerequisites
- You have SQS queue configured.
- You have your EventBridge rules configured.
- You have the associated resource policy configured
Procedure
Associate the queue URL with your existing cluster:
$ rosa edit cluster --cluster=<cluster-name> \ --spot-termination-queue-url "${QUEUE_URL}"Existing Spot machine pools immediately benefit from graceful termination handling. The AWS Node Termination Handler deployment is created in the control plane namespace and begins polling the queue. No machine pool changes are required.