Skip to content

Commit

Permalink
feat: Add EKS cluster with VPC and self managed ASG example (aws-samp…
Browse files Browse the repository at this point in the history
…les#224)

Adds a typescript example EKS cluster with a VPC & auto-scaling group.
  • Loading branch information
andskli authored and NGL321 committed Jan 21, 2020
1 parent a88f312 commit 331427b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions typescript/eks/cluster/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "node index"
}
43 changes: 43 additions & 0 deletions typescript/eks/cluster/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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', {
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": "^10.17.0",
"typescript": "~3.7.2"
},
"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 331427b

Please sign in to comment.