Chapter 3. Creating a Red Hat OpenShift Service on AWS cluster with Terraform
3.1. Creating a default Red Hat OpenShift Service on AWS cluster with Terraform Copy linkLink copied to clipboard!
Create a Red Hat OpenShift Service on AWS cluster with a Terraform cluster template that is configured with the default cluster options.
The following process for creating a cluster uses a Terraform configuration that prepares a Red Hat OpenShift Service on AWS cluster with these resources:
-
An OpenID Connect (OIDC) provider with a managed
oidc-configconfiguration - Prerequisite IAM Operator roles with associated AWS Managed Red Hat OpenShift Service on AWS Policies
- IAM account roles with associated AWS Managed Red Hat OpenShift Service on AWS Policies
- All other AWS resources required to create a Red Hat OpenShift Service on AWS cluster
3.1.1. Overview of Terraform Copy linkLink copied to clipboard!
Terraform is an infrastructure-as-code tool that provides a way to configure your resources once and replicate those resources as desired. Terraform accomplishes the creation tasks by using declarative language. You declare what you want the final state of the infrastructure resource to be, and Terraform creates these resources to your specifications.
3.1.2. Prerequisites Copy linkLink copied to clipboard!
To use the Red Hat Cloud Services provider inside your Terraform configuration, you must meet the following prerequisites:
-
You have installed the
rosaCLI tool. - You have your offline Red Hat OpenShift Cluster Manager token.
- You have installed Terraform version 1.4.6 or newer.
- You have created the ocm-role IAM role.
You have created your AWS account-wide IAM roles.
The specific account-wide IAM roles and policies provide the STS permissions required for Red Hat OpenShift Service on AWS support, installation, control plane, and compute functionality. This includes account-wide Operator policies. See the Additional resources for more information on the AWS account roles.
- You have an AWS account and associated credentials that allow you to create resources. The credentials are configured for the AWS provider. See the Authentication and Configuration section in AWS Terraform provider documentation.
You have, at minimum, the following permissions in your AWS IAM role policy that is operating Terraform. Check for these permissions in the AWS console.
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "iam:GetPolicyVersion", "iam:DeletePolicyVersion", "iam:CreatePolicyVersion", "iam:UpdateAssumeRolePolicy", "secretsmanager:DescribeSecret", "iam:ListRoleTags", "secretsmanager:PutSecretValue", "secretsmanager:CreateSecret", "iam:TagRole", "secretsmanager:DeleteSecret", "iam:UpdateOpenIDConnectProviderThumbprint", "iam:DeletePolicy", "iam:CreateRole", "iam:AttachRolePolicy", "iam:ListInstanceProfilesForRole", "secretsmanager:GetSecretValue", "iam:DetachRolePolicy", "iam:ListAttachedRolePolicies", "iam:ListPolicyTags", "iam:ListRolePolicies", "iam:DeleteOpenIDConnectProvider", "iam:DeleteInstanceProfile", "iam:GetRole", "iam:GetPolicy", "iam:ListEntitiesForPolicy", "iam:DeleteRole", "iam:TagPolicy", "iam:CreateOpenIDConnectProvider", "iam:CreatePolicy", "secretsmanager:GetResourcePolicy", "iam:ListPolicyVersions", "iam:UpdateRole", "iam:GetOpenIDConnectProvider", "iam:TagOpenIDConnectProvider", "secretsmanager:TagResource", "sts:AssumeRoleWithWebIdentity", "iam:ListRoles" ], "Resource": [ "arn:aws:secretsmanager:*:<ACCOUNT_ID>:secret:*", "arn:aws:iam::<ACCOUNT_ID>:instance-profile/*", "arn:aws:iam::<ACCOUNT_ID>:role/*", "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/*", "arn:aws:iam::<ACCOUNT_ID>:policy/*" ] }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "s3:*" ], "Resource": "*" } ] }
3.1.3. Considerations when using Terraform Copy linkLink copied to clipboard!
In general, using Terraform to manage cloud resources should be done with the expectation that any changes should be done using the Terraform methodology. Use caution when using tools outside of Terraform, such as the AWS console or Red Hat console, to modify cloud resources created by Terraform. Using tools outside Terraform to manage cloud resources that are already managed by Terraform introduces configuration drift from your declared Terraform configuration.
For example, if you upgrade your Terraform-created cluster by using the Red Hat Hybrid Cloud Console, you need to reconcile your Terraform state before applying any forthcoming configuration changes.
3.1.4. Overview of the default cluster specifications Copy linkLink copied to clipboard!
You can quickly create a Red Hat OpenShift Service on AWS cluster by using the default installation options.
| Component | Default specifications |
|---|---|
| Accounts and roles |
|
| Cluster settings |
|
| Compute node machine pool |
|
| Networking configuration |
|
| Classless Inter-Domain Routing (CIDR) ranges |
|
| Cluster roles and policies |
|
| Storage |
|
| Cluster update strategy |
|
3.1.5. Creating a default Red Hat OpenShift Service on AWS cluster using Terraform Copy linkLink copied to clipboard!
The cluster creation process outlined below shows how to use Terraform to create your account-wide IAM roles and a Red Hat OpenShift Service on AWS cluster with a managed OIDC configuration.
3.1.5.1. Prepare your environment for Terraform Copy linkLink copied to clipboard!
Before you can create your Red Hat OpenShift Service on AWS cluster by using Terraform, you must export your offline Red Hat OpenShift Cluster Manager token to grant Terraform permission to access your account.
Procedure
Optional: Because the Terraform files get created in your current directory during this procedure, you can create a new directory to store these files and navigate into it by running the following command:
$ mkdir terraform-cluster && cd terraform-cluster- Grant permissions to your account by using an offline Red Hat OpenShift Cluster Manager token.
Copy your offline token, and set the token as an environmental variable by running the following command:
$ export RHCS_TOKEN=<your_offline_token>NoteThis environmental variable resets at the end of each session, such as restarting your machine or closing the terminal.
Verification
After you export your token, verify the value by running the following command:
$ echo $RHCS_TOKEN
3.1.5.2. Creating your Terraform files locally Copy linkLink copied to clipboard!
After you configure your offline Red Hat OpenShift Cluster Manager token, you need to create the Terraform files locally to build your cluster. You can create these files by using the following code templates.
Procedure
Create the
main.tffile by running the following command:$ cat<<-EOF>main.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # terraform { required_providers { aws = { source = "hashicorp/aws" version = ">= 4.21.0" } rhcs = { version = ">= 1.6.3" source = "terraform-redhat/rhcs" } } } # Export token using the RHCS_TOKEN environment variable provider "rhcs" {} provider "aws" { region = var.aws_region ignore_tags { key_prefixes = ["kubernetes.io/"] } default_tags { tags = var.default_aws_tags } } data "aws_availability_zones" "available" {} locals { # Extract availability zone names for the specified region, limit it to 3 if multi az or 1 if single region_azs = var.multi_az ? slice([for zone in data.aws_availability_zones.available.names : format("%s", zone)], 0, 3) : slice([for zone in data.aws_availability_zones.available.names : format("%s", zone)], 0, 1) } resource "random_string" "random_name" { length = 6 special = false upper = false } locals { worker_node_replicas = var.multi_az ? 3 : 2 # If cluster_name is not null, use that, otherwise generate a random cluster name cluster_name = coalesce(var.cluster_name, "rosa-\${random_string.random_name.result}") } # The network validator requires an additional 60 seconds to validate Terraform clusters. resource "time_sleep" "wait_60_seconds" { count = var.create_vpc ? 1 : 0 depends_on = [module.vpc] create_duration = "60s" } module "rosa-hcp" { source = "terraform-redhat/rosa-hcp/rhcs" version = "1.6.3" cluster_name = local.cluster_name openshift_version = var.openshift_version account_role_prefix = local.cluster_name operator_role_prefix = local.cluster_name replicas = local.worker_node_replicas aws_availability_zones = local.region_azs create_oidc = true private = var.private_cluster aws_subnet_ids = var.create_vpc ? var.private_cluster ? module.vpc[0].private_subnets : concat(module.vpc[0].public_subnets, module.vpc[0].private_subnets) : var.aws_subnet_ids create_account_roles = true create_operator_roles = true # Optional: Configure a cluster administrator user # # Option 1: Default cluster-admin user # Create an administrator user (cluster-admin) and automatically # generate a password by uncommenting the following parameter: # create_admin_user = true # Generated administrator credentials are displayed in terminal output. # # Option 2: Specify administrator username and password # Create an administrator user and define your own password # by uncommenting and editing the values of the following parameters: # admin_credentials_username = <username> # admin_credentials_password = <password> # Optional: Enable cluster deletion protection to prevent accidental deletion. # To enable, uncomment the following parameter: # delete_protection = true # By default, clusters are created with deletion protection disabled. depends_on = [time_sleep.wait_60_seconds] } EOF-
If you want to create an administrator user during cluster creation, uncomment the appropriate parameters in the
Optional: Configure a cluster administrator usersection and edit their values. If you want to enable deletion protection during cluster creation, uncomment the corresponding parameter in the
Optional: Enable cluster delete protectionsection. When enabled, deletion protection blocks theterraform destroycommand until you setdelete_protection = falseand apply the change by runningterraform apply.Deletion protection only blocks deletion requests made through Terraform. It does not set any AWS-level resource protections, and you can still delete cluster resources directly in AWS, risking data loss or cluster outage. Additionally, deletion protection does not override the Red Hat Deletion policy. Red Hat reserves the right to delete clusters that violate service requirements.
-
If you want to create an administrator user during cluster creation, uncomment the appropriate parameters in the
Create the
variables.tffile by running the following command:NoteCopy and edit this file before running the command to build your cluster.
$ cat<<-EOF>variables.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # variable "openshift_version" { type = string default = "4.14.20" description = "Desired version of OpenShift for the cluster, for example '4.14.20'. If version is greater than the currently running version, an upgrade will be scheduled." } variable "create_vpc" { type = bool description = "If you would like to create a new VPC, set this value to 'true'. If you do not want to create a new VPC, set this value to 'false'." } # Red Hat OpenShift Service on AWS cluster info variable "cluster_name" { default = null type = string description = "The name of the Red Hat OpenShift Service on AWS cluster to create" } variable "additional_tags" { default = { Terraform = "true" Environment = "dev" } description = "Additional AWS resource tags" type = map(string) } variable "multi_az" { type = bool description = "Multi AZ Cluster for High Availability" default = true } variable "worker_node_replicas" { default = 3 description = "Number of worker nodes to provision. Single zone clusters need at least 2 nodes, multizone clusters need at least 3 nodes" type = number } variable "aws_subnet_ids" { type = list(any) description = "A list of either the public or public + private subnet IDs to use for the cluster blocks to use for the cluster" default = ["subnet-01234567890abcdef", "subnet-01234567890abcdef", "subnet-01234567890abcdef"] } variable "private_cluster" { type = bool description = "If you want to create a private cluster, set this value to 'true'. If you want a publicly available cluster, set this value to 'false'." } #VPC Info variable "vpc_name" { type = string description = "VPC Name" default = "tf-qs-vpc" } variable "vpc_cidr_block" { type = string description = "value of the CIDR block to use for the VPC" default = "10.0.0.0/16" } variable "private_subnet_cidrs" { type = list(any) description = "The CIDR blocks to use for the private subnets" default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] } variable "public_subnet_cidrs" { type = list(any) description = "The CIDR blocks to use for the public subnets" default = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] } variable "single_nat_gateway" { type = bool description = "Single NAT or per NAT for subnet" default = false } #AWS Info variable "aws_region" { type = string default = "us-east-2" } variable "default_aws_tags" { type = map(string) description = "Default tags for AWS" default = {} } EOFCreate the
vpc.tffile by running the following command:$ cat<<-EOF>vpc.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "5.1.2" count = var.create_vpc ? 1 : 0 name = var.vpc_name cidr = var.vpc_cidr_block azs = local.region_azs private_subnets = var.multi_az ? var.private_subnet_cidrs : [var.private_subnet_cidrs[0]] public_subnets = var.multi_az ? var.public_subnet_cidrs : [var.public_subnet_cidrs[0]] enable_nat_gateway = true single_nat_gateway = var.single_nat_gateway enable_dns_hostnames = true enable_dns_support = true tags = var.additional_tags } EOFYou are ready to initiate Terraform.
3.1.5.3. Use Terraform to create your Red Hat OpenShift Service on AWS cluster Copy linkLink copied to clipboard!
After you create the Terraform files, you must initiate Terraform to provide all of the required dependencies. Then apply the Terraform plan.
Do not modify Terraform state files.
Procedure
Configure Terraform to create your resources based on your Terraform files, run the following command:
$ terraform initOptional: Verify that the Terraform you copied is correct by running the following command:
$ terraform validateExample output
Success! The configuration is valid.Create your cluster with Terraform by running the following command:
$ terraform applyThe Terraform interface asks two questions to create your cluster, similar to the following:
var.create_vpc If you would like to create a new VPC, set this value to 'true'. If you do not want to create a new VPC, set this value to 'false'. Enter a value: var.private_cluster If you want to create a private cluster, set this value to 'true'. If you want a publicly available cluster, set this value to 'false'. Enter a value:Enter
yesto proceed ornoto cancel when the Terraform interface lists the resources to be created or changed and prompts for confirmation:Plan: 63 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.If you enter
yes, your Terraform plan starts, creating your AWS account roles, Operator roles, and your Red Hat OpenShift Service on AWS cluster.
Verification
Verify that your cluster was created by running the following command:
$ rosa list clustersThis example shows a cluster in the
readystate:ID NAME STATE TOPOLOGY 27c3snjsupa9obua74ba8se5kcj11269 rosa-tf-demo ready Hosted CPVerify that your account roles were created by running the following command:
$ rosa list account-rolesThis example shows the account roles that were created:
I: Fetching account roles ROLE NAME ROLE TYPE ROLE ARN OPENSHIFT VERSION AWS Managed ROSA-demo-Installer-Role Installer arn:aws:iam::<ID>:role/ROSA-demo-Installer-Role 4.14 No ROSA-demo-Support-Role Support arn:aws:iam::<ID>:role/ROSA-demo-Support-Role 4.14 No ROSA-demo-Worker-Role Worker arn:aws:iam::<ID>:role/ROSA-demo-Worker-Role 4.14 NoVerify that your Operator roles were created by running the following command:
$ rosa list operator-rolesThis example shows the Terraform-created Operator roles:
I: Fetching operator roles ROLE PREFIX AMOUNT IN BUNDLE rosa-demo 8
3.1.5.4. Configure an htpasswd identity provider with Terraform Copy linkLink copied to clipboard!
After creating your cluster with Terraform, you can permit users access to your cluster by using an htpasswd identity provider (IDP) with the Terraform tool.
Prerequisites
-
You have installed and configured the latest version of the
rosaCLI. - You have installed and configured the latest version of Terraform.
Procedure
Create the
htpasswd_idp.tffile by running one of the following commands:Option 1: To create a user with a generated, randomized password, run:
$ cat<<-EOF>htpasswd_idp.tf module "htpasswd_idp" { source = "terraform-redhat/rosa-hcp/rhcs//modules/idp" version = "1.6.2" cluster_id = "2odpb9p344hnkfvpkluo00qmgkika78l" name = "htpasswd-idp-tf-1" idp_type = "htpasswd" htpasswd_idp_users = [{ username = "pej-user-d1", password = random_password.password.result }] } resource "aws_secretsmanager_secret" "idp_password" { name = "idp-password-secret" description = "Any description here" } resource "random_password" "password" { length = 16 lower = true special = true override_special = "!#$%&*()-_=+[]{}<>:?" } # If you need to output the password, mark it as sensitive to hide from CLI logs output "password_output" { value = random_password.password.result sensitive = true } # This section sends your credentials to your AWS Secrets Manager to enable you to log in to your cluster. resource "aws_secretsmanager_secret_version" "idp_password_val" { secret_id = aws_secretsmanager_secret.idp_password.id secret_string = random_password.password.result } EOFYou must replace the
<cluster_id>placeholder with the 32-digit ID for your cluster. To find that value, runrosa list clusters | awk '{print $1}'. You also must replace the<user_name>placeholder with the username you want to create. The randomized password is then stored in your AWS Secrets manager to be used when logging in to the cluster.Run the following command to view your password after setting it:
$ terraform output password_outputThe CLI returns your generated password in plain text.
Option 2: To specify your passwords when creating a user, run:
$ cat<<-EOF>htpasswd_idp.tf module "htpasswd_idp" { source = "terraform-redhat/rosa-hcp/rhcs//modules/idp" version = "1.6.2" cluster_id = "<cluster_id>" name = "htpasswd-idp" idp_type = "htpasswd" htpasswd_idp_users = [{ username="<user_name>",password="<password>"}] } EOFYou must replace the
<cluster_id>placeholder with the 32-digit ID for your cluster. To find that value, runrosa list clusters | awk '{print $1}'. You also must replace the<user_name>placeholder with the username you want to create and a password for the<password>placeholder.
Run the following command to configure Terraform to create your resources based on your Terraform files:
$ terraform initVerify that the Terraform you copied is correct by running the following command:
$ terraform validateExample output
Success! The configuration is valid.Create your cluster with Terraform by running the following command:
$ terraform applyEnter
yesto proceed ornoto cancel when the Terraform interface lists the resources to be created or changed and prompts for confirmation:Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yesYou see a confirmation that your IDP has been created.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.NoteIf you used the randomized password template, then the generated password is stored in your AWS Secrets manager.
3.1.5.5. Deleting your Red Hat OpenShift Service on AWS cluster with Terraform Copy linkLink copied to clipboard!
Use the terraform destroy command to remove all resources you create with the terraform apply command.
Keep your Terraform .tf files unchanged before destroying your resources. These variables are matched to resources to delete.
If deletion protection is enabled on the cluster, disable it by setting delete_protection = false in the main.tf file, then run terraform apply to apply the change.
Procedure
In the directory where you ran the
terraform applycommand to create your cluster, run the following command to delete the cluster:$ terraform destroyThe Terraform interface prompts you for two variables. These should match the answers you provided when creating a cluster:
var.create_vpc If you would like to create a new VPC, set this value to 'true.' If you do not want to create a new VPC, set this value to 'false.' Enter a value: var.private_cluster If you want to create a private cluster, set this value to 'true.' If you want a publicly available cluster, set this value to 'false.' Enter a value:Enter
yesto start the role and cluster deletion:Example output
Plan: 0 to add, 0 to change, 63 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes
Verification
Verify that your cluster was destroyed by running the following command:
$ rosa list clustersExample output showing no cluster
I: No clusters availableVerify that the account roles were destroyed by running the following command:
$ rosa list account-rolesExample output showing no Terraform-created account roles
I: Fetching account roles I: No account roles availableVerify that the Operator roles were destroyed by running the following command:
$ rosa list operator-rolesExample output showing no Terraform-created Operator roles
I: Fetching operator roles I: No operator roles available
3.2. Configure AWS Spot instances for Red Hat OpenShift Service on AWS with Terraform Copy linkLink copied to clipboard!
Create a Red Hat OpenShift Service on AWS cluster or update an existing one with Terraform that is configured for AWS Spot instances.
3.2.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 installed Terraform version 1.4.6 or newer.
You have one of the following Terraform providers:
-
Red Hat Cloud Services:
terraform-provider-rhcs AWS:
terraform-provider-awsThis is for Enhanced mode Simple Queue Service (SQS) and EventBridge resources
-
Red Hat Cloud Services:
You have the required
sqs:ReceiveMessageandsqs:DeleteMessageIAM permissions. These are included in the standardROSANodePoolManagementPolicy.You do not need any additional IAM role changes. The managed policy scopes access to SQS queues tagged with
red-hat=true.- (Optional): You have AWS CLI access so that you can retrieve the NodePoolManagement role ARN, if you do not already know it.
3.2.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.
3.2.2.1. Create the SQS queue Copy linkLink copied to clipboard!
Set up the SQS queue for graceful interruption events.
Procedure
Add the following resource to your Terraform configuration:
resource "aws_sqs_queue" "spot_termination" { name = "rosa-<cluster-name>-spot" tags = { "red-hat" = "true" } }ImportantThe
red-hat=truetag is required. TheROSANodePoolManagementPolicyuses an IAM condition,aws:ResourceTag/red-hat: "true", to scope SQS permissions. Omitting this tag causes the AWS Node Termination Handler to fail when polling the queue.
3.2.2.2. Create your EventBridge rules Copy linkLink copied to clipboard!
Use EventBridge rules to route EC2 Spot events to the queue.
Procedure
Create two EventBridge rules to capture Spot instance events and route them to the SQS queue:
# Rule 1: Spot Instance Interruption Warning resource "aws_cloudwatch_event_rule" "spot_interruption" { name = "rosa-<cluster-name>-spot-interruption" description = "Capture EC2 Spot Instance Interruption Warnings" event_pattern = jsonencode({ source = ["aws.ec2"] detail-type = ["EC2 Spot Instance Interruption Warning"] }) } # Rule 2: Instance Rebalance Recommendation resource "aws_cloudwatch_event_rule" "rebalance_recommendation" { name = "rosa-<cluster-name>-rebalance-recommendation" description = "Capture EC2 Instance Rebalance Recommendations" event_pattern = jsonencode({ source = ["aws.ec2"] detail-type = ["EC2 Instance Rebalance Recommendation"] }) } # Route both rules to the SQS queue resource "aws_cloudwatch_event_target" "spot_interruption_target" { rule = aws_cloudwatch_event_rule.spot_interruption.name target_id = "SendToSQS" arn = aws_sqs_queue.spot_termination.arn } resource "aws_cloudwatch_event_target" "rebalance_recommendation_target" { rule = aws_cloudwatch_event_rule.rebalance_recommendation.name target_id = "SendToSQS" arn = aws_sqs_queue.spot_termination.arn }
3.2.2.3. Configure the SQS queue resource policy Copy linkLink copied to clipboard!
Set up the SQS resource policy to grant access to EventBridge and the cluster’s NodePoolManagement role
Procedure
Apply a resource policy to the SQS queue:
resource "aws_sqs_queue_policy" "spot_termination" { queue_url = aws_sqs_queue.spot_termination.id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "AllowNodePoolManagementRole" Effect = "Allow" Principal = { AWS = var.nodepool_management_role_arn } Action = [ "sqs:DeleteMessage", "sqs:ReceiveMessage" ] Resource = aws_sqs_queue.spot_termination.arn }, { Sid = "AllowEventBridgeToSendMessages" Effect = "Allow" Principal = { Service = "events.amazonaws.com" } Action = "sqs:SendMessage" Resource = aws_sqs_queue.spot_termination.arn } ] }) }This policy grants access to two principals:
- EventBridge
- sends interruption events to the queue.
- NodePoolManagement role
receives and deletes messages from the queue.
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.
3.2.2.4. Define your required Terraform variables Copy linkLink copied to clipboard!
Update you local Terraform variables file with the NodePoolManagement role ARN.
Procedure
(Optional): Retrieve the NodePoolManagement role ARN from an existing cluster:
$ aws iam list-roles --query 'Roles[?contains(RoleName, `kube-system-capa-controller-manager`)].Arn' --output textAdd the following variables to your
variables.tffile:variable "cluster_name" { description = "Name of the ROSA HCP cluster" type = string } variable "nodepool_management_role_arn" { description = "ARN of the NodePoolManagement IAM role for the cluster" type = string }The NodePoolManagement role ARN typically follows this pattern:
arn:aws:iam::<account-id>:role/ManagedOpenShift-HCP-ROSA-NodePoolManagement-Role
3.2.3. Create a new Red Hat OpenShift Service on AWS cluster with Spot instances (Day 0) using Terraform Copy linkLink copied to clipboard!
After you create or update your Terrform variables file, you can add the cluster resource to your main file and initiate Terraform. The process for simple mode and enhanced mode differ by the omission or inclusion respectively of the termination_handler_queue_url attribute. The machine pool configuration is identical for both simple and enhanced modes.
Each 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.
Procedure
Add the cluster resouce to your
main.tffile:resource "rhcs_cluster_rosa_hcp" "cluster" { name = var.cluster_name cloud_region = var.aws_region aws_account_id = var.aws_account_id operator_role_arn = var.operator_role_arn termination_handler_queue_url = aws_sqs_queue.spot_termination.url # Enhanced mode only }Add a Spot machine pool resource to your
main.tffile:resource "rhcs_hcp_machine_pool" "spot_pool" { cluster = rhcs_cluster_rosa_hcp.cluster.id name = "spot-pool" aws_node_pool = { instance_type = "m5.xlarge" use_spot_instances = true max_spot_price = 2.0 } subnet_id = var.private_subnet_id replicas = 2 auto_repair = true # ... }Apply the configuration:
$ terraform init && terraform plan && terraform applyThe 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.
3.2.4. Update an existing Red Hat OpenShift Service on AWS cluster with Spot machine pools (Day 2) using Terraform Copy linkLink copied to clipboard!
You can add Spot machine pools to an existing Red Hat OpenShift Service on AWS cluster managed by Terraform.
Procedure
Add a new
rhcs_hcp_machine_poolresource to your existing Terraform configuration:resource "rhcs_hcp_machine_pool" "spot_pool" { cluster = rhcs_cluster_rosa_hcp.cluster.id name = "spot-pool" aws_node_pool = { instance_type = "m5.xlarge" use_spot_instances = true max_spot_price = 2.0 } subnet_id = var.private_subnet_id replicas = 2 auto_repair = true # ... }Apply the configuration:
$ terraform plan && terraform applyWarningIf the cluster does not have the
termination_handler_queue_urlattribute configured, the Spot machine pool operates in simple mode. This means nodes will not be gracefully drained upon interruption.
3.2.5. Upgrade a Red Hat OpenShift Service on AWS cluster from simple mode to enhanced mode (Day 2) using Terraform Copy linkLink copied to clipboard!
You can upgrade an existing simple-mode cluster to enhanced mode by adding the AWS infrastructure resources and associating 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
Add the
termination_handler_queue_urlattribute to your existing cluster resource:resource "rhcs_cluster_rosa_hcp" "cluster" { name = var.cluster_name cloud_region = var.aws_region aws_account_id = var.aws_account_id operator_role_arn = var.operator_role_arn # Add this line to upgrade from simple to enhanced mode termination_handler_queue_url = aws_sqs_queue.spot_termination.url }Apply the configuration:
$ terraform plan && terraform applyTerraform performs a patch update on the cluster. Existing Spot machine pools immediately benefit from graceful termination handling.