Import EKS Cluster using Terraform
Prerequisite
This script assumes that you have already created the necessary IAM resources to create EKS related resources through Kosmos.
For more details refer:
Introduction
This is Terraform script for importing existing Kosmos EKS Cluster using Kosmos-provider into Kosmos fleet. This is triggered by setting the value of kosmos_eksClusters.this.imported to True.
User who runs this script must have required permission on AWS since it will create new AWS role for Kosmos Operator to import and configure EKS cluster it into Kosmos fleet.
Variables and Locals
| Name | Explaination | Type |
|---|---|---|
kosmos_access_key (required) | Kosmos Access key | String |
aws_region (required) | Region where EKS cluster is deployed | String |
imported_cluster_name (required) | Name of the EKS cluster | String |
oidc_provider_arn (required) | arn for existing OIDC provider | String |
fleet_name (required) | Kosmos fleet where EKS will be improted into. For now, you must first create the fleet manually using Kosmos UI | String |
kosmos_role_arn (optional) | Existing Kosmos Service Role ARN, if empty will auto create a new role and policy | String |
Adding delay on the creation of kosmos_eksClusters resource
There’s an issue from AWS provider where the required resource (role_policy_attachment) is created but not yet ready which results in race condtion when creating kosmos_eksClusters.
The error thrown indicated that Kosmos is not authorized to assume the newly created role using the AWS AssumeRoleWithWebIdentity. This issue happens when we are importing existing EKS cluster using kosmos_eksClusters while also creating a new role, policy, and role_policy_attachment.
To overcome this we added a 5s delay in between the creation of role_policy_attachment and kosmos_eksClusters.
How to run
- Run
python scop2aws.pyto generate temporary credentials using SCOP - Run
terraform initto initialize the working directory and download Terraform providers and modules - Run
terraform plan -var="kosmos_access_key=(kosmos_access_key)"to preview the changes to be made by Terraform and read thoroughly on resources to be created - Run
terraform apply -var="kosmos_access_key=(kosmos_access_key)" -var="oidc_provider_arn=(oidc_provider_arn)" -var="fleet_name=(fleet_name)" -var="imported_cluster_name=(imported_cluster_name)" -var="aws_region=(aws_region)"and typeyeswhen prompted - To destroy all the resources, run
terraform destroy -var="kosmos_access_key=(kosmos_access_key)" -var="oidc_provider_arn=(oidc_provider_arn)" -var="fleet_name=(fleet_name)" -var="imported_cluster_name=(imported_cluster_name) -var="aws_region=(aws_region)""
Download Resources
For complete examples and the latest updates, refer to: