aws-cdk-k8s
v0.5.5
Published
CDK Infrastructure as Code for Self Hosted Kubernetes on AWS
Downloads
22
Maintainers
Readme
IaC Accelerator - Self Hosted Kubernetes cluster in AWS
Objectives
This is under development with the following objectives
✅ Deploy Enterprise grade Production cluster on Day 1
✅ Highly Scalable
✅ Highly Available
✅ CI/CD Compatibilty
✅ Event driven deployment with minimal maintenance
Milestones
| Version | Expected Month | Release Date | Release Type | Features | Use Cases |
| ------- | -------------- | ------------ | ---------------- | ------------------------------------------------------------------- | -------------------- |
| v0.5.0 | May 2026 | 30-May | Preview | Single Control Plane with multiple worker nodes | K8S Learning POC |
| |
| v1.0.0 | Jan 2027 | | Production Grade | Auto Scaling, Multiple Control Plane nodes, Event Driven Deployment | POC Beta |
Prerequisities
In order to use this accelerator, following are needed.
- AWS Account with VPC (default or custom)
- Log into AWS locally
- Node.JS installed in your system
- AWS CDk installed
npm i -g aws-cdk - CDK Bootstrapped region. Refer to this page on how to bootstrap your account/region
- Basic knowledge of CDK and Typescript is recommended
Steps / Commands to Deploy Cluster
- Create a folder locally
mkdir my-project cd my-projectcdk init --language typescriptnpm i aws-k8s- Open file
./bin/my-project.ts - Replace the pre-populated code with the following code
import { App, StackProps } from "aws-cdk-lib";
import { K8sStack } from "../lib/k8s-stack";
import { K8sClusterProps } from "../lib/types";
import { InstanceSize,SubnetType } from "aws-cdk-lib/aws-ec2";
const app = new App();
const clusterProps: K8sClusterProps = {
vpcId: "vpc-11111111111111111", // replace with your vpc id
amiParamName: "/ami/amazon-linux",// See section 'Important Considerations'
associatePublicIpAddress: true,// See section 'Important Considerations'
// All the following attributes are optional
subnetType: SubnetType.PUBLIC,// See section 'Important Considerations'
keyPairName: "ec2-instances",
Considerations'
clusterName: "k8s",
namePrefix: "learning",
envTag: "dev",
controlPlaneInstance: {
size: InstanceSize.MEDIUM,
ingressRules: [
{
port: {
lowerRange: 6443,
upperRange: 6443,
},
peerType: "AnyIpv4",
},
],
},
};
const stackProps: StackProps = {
stackName: "k8s-stack",
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: "<my-region>",
},
tags: {
dept: "platform",
"cost-centre": "12345",
},
};
new K8sStack(app, "k8s-stack", clusterProps, stackProps);
- Log into AWS locally
- Set AWS profile with the following commands. Powershell:
$env:AWS_PROFILE='my-profile'; Bash:export AWS_PROFILE=my-profile - Run command
cdk deploy - Wait for the deployment to finish
- Once deployment done, note down Control Plane instance ID from the output
- Wait for 5-10 minutes after deployment is finished as the current version is not CI/CD compatible. This will allow EC2 instances to complete predefined userdata that installs Kubernetes and join the worker nodes to cluster
- Log into Control Plane node by running the following command
aws ssm start-session --target $args[0] --region <my-region> --document-name AWS-StartInteractiveCommand --parameters command="/bin/bash"Replace<my-region>with actual AWS Region - Above command will log you into Cluster instance
- Run this command
sudo -i - Run this command
kubectl get nodesto see the nodes running. You should see an output like the following:
Important Considerations
Attribute
amiParamName:- You should supply your own AMI ID that will be used for EC2 instance. AMI shoule be based on Red Hat based distribution. This is tested with Amazon Linux AMI. Hence, I recommend to use the same.
- Create a parameter in AWS with data type as
aws:ec2:imageand provide the ami id as the value. ex:ami-050b6e407a84b6284 - I have used Amazon Linux image
ami-050b6e407a84b6284from regionap-south-2for testing of this library. You may use a value depending on your region
Attribute
associatePublicIpAddress: You may set it totrueonly for education / training purpose. Otherwise, it is highly recommnded to set it tofalse. When this isfalse, ensure the following for proper connectivity- Create following 3 VPC Endpoints
SSM com.amazonaws.<region>.ssmEC2 Messages com.amazonaws.<region>.ec2messagesSSM Messages com.amazonaws.<region>.ssmmessages
- Security group attached to VPC shoud have inbound rule to allow port
443with source CIDR same as VPC CIDR. ex:10.0.0.0/16. This will allow Session Manager to register with EC2 SSM agent and you will be able to connect to EC2
- Create following 3 VPC Endpoints
Attribute
subnetType: This is an optional attribute and defaults to Public. You may select other values, but ensure EC2 has necessary internet connection to install all required dependenciesHelp document for other attributes: As this is a typescript project, there is a help documentation embedded for each attribute. Feel free to hover the mouse on an attribute, which will pop up the documentation. You should use IDE tool that supports intellisense ex: VS Code
