EKS Token

0.3.0 · active · verified Thu Apr 16

eks-token is a Python library that provides an alternative to the `aws eks get-token` CLI command, allowing programmatic generation of authentication tokens for Amazon EKS clusters. It is currently at version 0.3.0 and is actively maintained, with releases typically tied to feature enhancements or dependency updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `eks-token` to retrieve an EKS authentication token. It shows basic usage with a cluster name and illustrates how to extract the actual token string. Ensure your AWS credentials are configured (e.g., via `~/.aws/credentials` or environment variables) and you have the necessary IAM permissions to interact with EKS. The `apiVersion` in the output now reflects `v1beta1` as of `eks-token` v0.2.0.

import os
from eks_token import get_token
from pprint import pprint

# Replace with your EKS cluster name. Can also be set via an environment variable.
cluster_name = os.environ.get('EKS_CLUSTER_NAME', 'your-eks-cluster-name')

if cluster_name == 'your-eks-cluster-name':
    print("Please set the EKS_CLUSTER_NAME environment variable or update the 'cluster_name' variable in the script.")
else:
    try:
        # Get the token for the specified EKS cluster
        response = get_token(cluster_name=cluster_name)
        pprint(response)

        # Extract the token string
        token = response['status']['token']
        print(f"\nExtracted EKS Token: {token[:30]}...{token[-5:]}")

        # Example of getting token for a specific IAM role (optional)
        # role_arn = os.environ.get('EKS_ROLE_ARN', 'arn:aws:iam::123456789012:role/YourEKSViewerRole')
        # if role_arn != 'arn:aws:iam::123456789012:role/YourEKSViewerRole':
        #     token_with_role = get_token(cluster_name=cluster_name, role_arn=role_arn)['status']['token']
        #     print(f"\nToken with role: {token_with_role[:30]}...{token_with_role[-5:]}")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Ensure your AWS credentials are configured and you have permissions to describe the EKS cluster.")

view raw JSON →