Skip to content

Commit

Permalink
EKS cluster with VPC and self managed ASG example
Browse files Browse the repository at this point in the history
  • Loading branch information
andskli committed Jan 11, 2020
1 parent ee91f30 commit 185435f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
44 changes: 44 additions & 0 deletions typescript/eks/cluster/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import autoscaling = require('@aws-cdk/aws-autoscaling');
import iam = require('@aws-cdk/aws-iam');
import ec2 = require('@aws-cdk/aws-ec2');
import eks = require('@aws-cdk/aws-eks');
import cdk = require('@aws-cdk/core');

class EKSCluster extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const vpc = new ec2.Vpc(this, 'EKSVpc'); // Create a new VPC for our cluster

// IAM role for our EC2 worker nodes
const workerRole = new iam.Role(this, 'EKSWorkerRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
});

const eksCluster = new eks.Cluster(this, 'Cluster', {
clusterName: id,
vpc: vpc,
kubectlEnabled: true, // we want to be able to manage k8s resources using CDK
defaultCapacity: 0 // we want to manage capacity our selves
});

const onDemandASG = new autoscaling.AutoScalingGroup(this, 'OnDemandASG', {
vpc: vpc,
role: workerRole,
minCapacity: 1,
maxCapacity: 10,
instanceType: new ec2.InstanceType('t3.medium'),
machineImage: new eks.EksOptimizedImage({
kubernetesVersion: '1.14',
nodeType: eks.NodeType.STANDARD // wihtout this, incorrect SSM parameter for AMI is resolved
}),
updateType: autoscaling.UpdateType.ROLLING_UPDATE
});

eksCluster.addAutoScalingGroup(onDemandASG, {});
}
}

const app = new cdk.App();
new EKSCluster(app, 'MyEKSCluster');
app.synth();
29 changes: 29 additions & 0 deletions typescript/eks/cluster/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "eks-cluster",
"version": "1.0.0",
"description": "Basic EKS cluster with VPC and self managed AutoScalingGroup",
"private": true,
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"cdk": "cdk"
},
"author": {
"name": "Andreas Lindh",
"url": "https://aws.amazon.com",
"organization": true
},
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^8.10.38",
"typescript": "^3.2.4"
},
"dependencies": {
"@aws-cdk/aws-autoscaling": "*",
"@aws-cdk/aws-ec2": "*",
"@aws-cdk/aws-eks": "*",
"@aws-cdk/aws-iam": "*",
"@aws-cdk/core": "*",
"aws-cdk": "*"
}
}
20 changes: 20 additions & 0 deletions typescript/eks/cluster/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target":"ES2018",
"module": "commonjs",
"lib": ["es2016", "es2017.object", "es2017.string"],
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization":false
}
}

0 comments on commit 185435f

Please sign in to comment.