Configuring Amazon SQS and Amazon EventBridge to receive Amazon EC2 interruption events
Before you enable Amazon Spot Instances, you must set up Amazon Simple Queue Service (SQS) and Amazon EventBridge to receive Amazon EC2 interruption events.
For hosted control planes to be able to provide a graceful shut down, it needs to poll the SQS queue and cordon or drain nodes before they are deleted. For more information about Amazon SQS and Amazon EventBridge, see "Getting started with Amazon SQS" and "Getting started: Create an Amazon EventBridge bus rule".
-
Create an Amazon SQS queue to receive Spot Instance notifications by entering the following command:
CLUSTER_NAME="my-cluster" AWS_REGION="us-east-1" $ aws sqs create-queue \ --queue-name "${CLUSTER_NAME}-spot-interruption-queue" \ --region "${AWS_REGION}"-
Be sure to replace
my-clusterandus-east-1with your cluster name and AWS region. -
Take note of the URL from the output. You need the URL when you create the hosted cluster.
-
-
Create Amazon EventBridge rules to route Amazon EC2 Spot interruption warnings and rebalance recommendations to the SQS queue.
-
Set the attributes by entering the following command:
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) QUEUE_URL="https://sqs.${AWS_REGION}.amazonaws.com/${ACCOUNT_ID}/${CLUSTER_NAME}-spot-interruption-queue" QUEUE_ARN=$(aws sqs get-queue-attributes \ --queue-url "${QUEUE_URL}" \ --attribute-names QueueArn \ --query 'Attributes.QueueArn' --output text) -
Set the interruption warning by entering the following command:
$ aws events put-rule \ --name "${CLUSTER_NAME}-spot-interruption-warning" \ --event-pattern '{"source":["aws.ec2"],"detail-type":["EC2 Spot Instance Interruption Warning"]}' \ --region "${AWS_REGION}" -
Set the target for the interruption warning by entering the following command:
$ aws events put-targets \ --rule "${CLUSTER_NAME}-spot-interruption-warning" \ --targets "Id=1,Arn=${QUEUE_ARN}" \ --region "${AWS_REGION}" -
Set the recommendations by entering the following command:
$ aws events put-rule \ --name "${CLUSTER_NAME}-rebalance-recommendation" \ --event-pattern '{"source":["aws.ec2"],"detail-type":["EC2 Instance Rebalance Recommendation"]}' \ --region "${AWS_REGION}" -
Set the target for the recommendations by entering the following command:
$ aws events put-targets \ --rule "${CLUSTER_NAME}-rebalance-recommendation" \ --targets "Id=1,Arn=${QUEUE_ARN}" \ --region "${AWS_REGION}"
-
-
Configure the Amazon SQS queue policy to allow EventBridge to send messages to the queue by entering the following command:
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) QUEUE_URL="https://sqs.${AWS_REGION}.amazonaws.com/${ACCOUNT_ID}/${CLUSTER_NAME}-spot-interruption-queue" $ aws sqs set-queue-attributes \ --queue-url "${QUEUE_URL}" \ --attributes '{ "Policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"events.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"'${QUEUE_ARN}'\"}]}" }'