https://github.com/moulick/cache-eks-token
Script to cache eks token and make kubectl commands faster
https://github.com/moulick/cache-eks-token
aws cache eks fast kubectl kubernetes
Last synced: 2 months ago
JSON representation
Script to cache eks token and make kubectl commands faster
- Host: GitHub
- URL: https://github.com/moulick/cache-eks-token
- Owner: Moulick
- License: apache-2.0
- Created: 2025-07-20T16:45:55.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-22T17:25:30.000Z (12 months ago)
- Last Synced: 2025-10-04T19:49:04.303Z (9 months ago)
- Topics: aws, cache, eks, fast, kubectl, kubernetes
- Language: Shell
- Homepage:
- Size: 422 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache AWS EKS Token
This is a script that is used to speed up kubectl when using AWS EKS. This works by caching the eks token from AWS.
The speed increase for a simple `kubectl get pods` is ~73%. It's really noticeable.
The default way is to use `aws eks get-token` command, but it is not cached and it is not very fast.
It's slow because:
1. Every kubectl command will do a `aws eks get-token` which makes an api call to AWS
2. `aws` is python. When running kubectl in a script, you can very clearly see how much performance is lost generating tokens for each kubectl invocation in Activity Monitor.
Here is a quick comparison before and after
| Before | After |
| --------------------- | --------------------- |
|  |  |
# How to use
1. Clone repo
2. cd into the repo and `chmod +x cached-aws-eks-token.sh`. Necessary as git clone removes the executable flag from files.
3. Simply replace the `command: aws` with `command: ../cache-eks-token/cached-aws-eks-token.sh` in your `~/.kube/config` files.
1. The command might differ based on where you placed the script.
> [!IMPORTANT]
> Remember to update your kubeconfig with the above script whenever you create a new kubeconfig or use `aws eks update-kubeconfig` to generate/update as it will replace it back with `command: aws`, wasting time in your life :)
## How it works
This script will caches the token generated by `aws eks get-token` in a file based on cluster name in the `$HOME/.kube/cache` directory.
AWS issues token for at-most 15 mins. This script will check the expiry and return existing token if it's till valid.
The token will be refreshed if it's expiring within 30 seconds.
## Errors
1. `Unable to connect to the server: getting credentials: exec: ... permission denied`
1. simply run `chmod +x cached-aws-eks-token.sh`
2. If error like `Unable to connect to the server: getting credentials: ... no such file or directory`
1. Make sure to make the command path is relative to wherever the script exists. You can use `../` in the `command` to move up directories.
3. `Failed conversion of ``'' using format ``%Y-%m-%dT%H:%M:%SZ''`
1. Happens when unable to get the date from the cache file. Try deleing the token cache files from `$HOME/.kube/cache`
2. The cache file generated by the script should looks like this
```json
{
"kind": "ExecCredential",
"apiVersion": "client.authentication.k8s.io/v1beta1",
"spec": {},
"status": {
"expirationTimestamp": "2025-07-20T17:38:15Z",
"token": "k8s-aws-v1."
}
}
```
## Dependencies
1. jq `brew install jq`