We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Securing Kubecost access with Amazon Cognito
Introduction
Kubecost provides real-time cost visibility and insights for teams using Kubernetes. It has an intuitive dashboard to help you understand and analyze the costs of running your workloads in a Kubernetes cluster. Kubecost is built on
Amazon EKS optimized bundle of Kubecost
Earlier last year, Amazon Elastic Kubernetes Service (
Using Kubecost’s intuitive dashboard, customers can monitor, analyze, and allocate cluster costs. When customers deploy Kubecost in a cluster, the dashboard is secured by
Solution overview
We make the Kubecost dashboard accessible outside the cluster by exposing it using an ingress, which uses Application Load Balancer (ALB). Integrating Amazon Cognito with the ALB, the solution adds support for authenticating and authorizing users to the Kubecost dashboard. To learn more about how ALB and Cognito integrate, please see
In this post, we use the
-
Application Load Balancer ,Amazon Cognito , and a Transport Layer Security (TLS) Certificate onAmazon Web Services Certificate Manager (ACM) withAmazon Route 53 hosted zone to authenticate users to Kubecost - Deployment of Kubecost application using
Kubecost add-on for EKS CDK Blueprints - Kubernetes Ingress with annotations for Amazon Cognito and a TLS Certificate (using
Amazon Certificate Manager ) for securely authenticating user to Kubecost
Customers can use this pattern to manage multiple clusters across environments with GitOps. Please see
The
Prerequisites
You need the following to complete the steps in this post:
- Amazon Web Services Command Line Interface (
Amazon Web Services CLI) version 2 -
Amazon Web Services CDK version 2.80.0 or later -
Node version 20.0.0 or later -
NPM version 8.19.2 or later -
kubectl version 1.24 or later -
Git - An Amazon Route 53 public hosted zone
Let’s start by setting a few environment variables:
Clone the
The
Bootstrap CDK
The first step to any
Execute the commands below to bootstrap the Amazon Web Services environment in your Region:
Deploy Kubecost with secured access
In this solution, we’ll allow access to the Kubecost dashboard based on user email addresses. You can control access to the dashboard by allow-listing an entire domain or individual email addresses.
Users are required to sign-up before they can access the Kubecost dashboard. The
First, we’ll create an Amazon Web Services Systems Manager (SSM) parameter to store the value of the email domain that users use to sign up. Next, we’ll create an environment variable to store the domain name that hosts the Kubecost dashboard. The email domain and the domain used to host the Kubecost dashboard can be same or different. For example, you may choose to host the dashboard at
Create below parameters with allowed email addresses and domains in the
If you’d like to limit access to the dashboard by email addresses, then you can also create a parameter to store allowed email addresses and add a logic to the pre authentication Lambda trigger as shown
Next, create a secret in
The CDK code expects the allowed domain and subdomain names in the CDK context file (cdk.json).
Create two environment variables. The PARENT_HOSTED_ZONE variable contains the name of your Route 53 public hosted zone. The DEV_SUBZONE_NAME will be the address for your Kubecost dashboard.
Generate the cdk.json file:
Run the below command from the root of this repository to deploy the solution:
This blueprint will deploy the following:
-
Amazon Virtual Private Cloud (Amazon VPC) with public and private subnets, Network Address Translation (NAT) gateways in each availability zone (AZ), and an Internet Gateway - An Amazon EKS cluster with the following Kubernetes add-ons
-
Metrics Server -
Cluster Autoscaler - Amazon Elastic Block Store (Amazon
EBS) Container Storage Interface (CSI) Driver Amazon EKS -
Amazon Web Services Load Balancer Controller -
Amazon VPC CNI -
ExternalDNS -
Kubecost -
Argo CD
-
-
Amazon Cognito user pool, user pool client, domain and alsopre-sign-up and pre-authentication lambda triggers to run custom logic to validate users before allowing them to either sign-up or authentication.
Once the deployment is complete, you will see the output similar to shown below in your terminal:
To update your Kubernetes configuration for your new cluster, copy and run the aws eks update-kubeconfig command (the second command in the output) in your terminal.
Validate the access to your Amazon EKS cluster using below kubectl listing all namespaces:
You should see the following namespaces in the cluster:
The stack deploys Kubecost resources in the kubecost namespace.
Testing the authentication
Point your browsers to the URL you associated with the DEV_SUBZONE_NAME key from the CDK context to access the Kubecost dashboard.
The value is also stored as an environment variable:
Your browser will be redirected to an Amazon Cognito hosted User Interface (UI) sign-in page. Since this is your first time accessing the application, select sign up .
The Pre sign-up Amazon Web Services Lambda trigger for Amazon Cognito User pool is configured to allow users to register only from certain allow-listed email domains. The allow-listed email domains are configured as an environmental variable in the Amazon Web Services Lambda function. Let us try sign up a new user using an email id, whose domain is not part of the allow list.
You’ll get an error since the domain is not allow-listed.
Let’s sign up as a new user with one of the allow-listed email domains. This time, you’ll get a prompt to confirm your account. Get the verification code sent to your email and confirm your account.
After verifying email address, sign in to access the Kubecost dashboard
Once you sign in, the ALB will redirect you to the Kubecost dashboard:
Cleaning up
You continue to incur cost until deleting the infrastructure that you created for this post. Use the commands below to delete resources created during this post:
Conclusion
In this post we showed you how to secure the Kubecost dashboard while making it accessible to users without needing access to the Kubernetes cluster. We used an ALB to expose the dashboard and secured access using Cognito. We also created a record in Route 53 so users can easily access the dashboard.
We used
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.