From 185435ff54c860867b30d5634e550e95a306ab6e Mon Sep 17 00:00:00 2001 From: Andreas Lindh Date: Fri, 10 Jan 2020 16:41:23 +0100 Subject: [PATCH 1/4] EKS cluster with VPC and self managed ASG example --- typescript/eks/cluster/index.ts | 44 ++++++++++++++++++++++++++++ typescript/eks/cluster/package.json | 29 ++++++++++++++++++ typescript/eks/cluster/tsconfig.json | 20 +++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 typescript/eks/cluster/index.ts create mode 100644 typescript/eks/cluster/package.json create mode 100644 typescript/eks/cluster/tsconfig.json diff --git a/typescript/eks/cluster/index.ts b/typescript/eks/cluster/index.ts new file mode 100644 index 000000000..d0656e2a5 --- /dev/null +++ b/typescript/eks/cluster/index.ts @@ -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(); diff --git a/typescript/eks/cluster/package.json b/typescript/eks/cluster/package.json new file mode 100644 index 000000000..cc620bc01 --- /dev/null +++ b/typescript/eks/cluster/package.json @@ -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": "*" + } +} diff --git a/typescript/eks/cluster/tsconfig.json b/typescript/eks/cluster/tsconfig.json new file mode 100644 index 000000000..544b446da --- /dev/null +++ b/typescript/eks/cluster/tsconfig.json @@ -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 + } +} \ No newline at end of file From 7b19380b486f93e5369b37f3974bac2046e32318 Mon Sep 17 00:00:00 2001 From: Andreas Skarmutsos Lindh Date: Sun, 12 Jan 2020 21:51:37 +0100 Subject: [PATCH 2/4] Removed clusterName property Co-Authored-By: Elad Ben-Israel --- typescript/eks/cluster/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/typescript/eks/cluster/index.ts b/typescript/eks/cluster/index.ts index d0656e2a5..41db96019 100644 --- a/typescript/eks/cluster/index.ts +++ b/typescript/eks/cluster/index.ts @@ -16,7 +16,6 @@ class EKSCluster extends cdk.Stack { }); 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 From 07b255415edb91f12f8c2d8a1446a7dd25363d74 Mon Sep 17 00:00:00 2001 From: Noah Litov Date: Tue, 21 Jan 2020 15:16:51 -0800 Subject: [PATCH 3/4] update ts version --- typescript/eks/cluster/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typescript/eks/cluster/package.json b/typescript/eks/cluster/package.json index cc620bc01..b874d3b7d 100644 --- a/typescript/eks/cluster/package.json +++ b/typescript/eks/cluster/package.json @@ -15,8 +15,8 @@ }, "license": "Apache-2.0", "devDependencies": { - "@types/node": "^8.10.38", - "typescript": "^3.2.4" + "@types/node": "^10.17.0", + "typescript": "~3.7.2" }, "dependencies": { "@aws-cdk/aws-autoscaling": "*", From 34ab95b0f4bde9ecf7a260fdb9c61e4ff8a5522c Mon Sep 17 00:00:00 2001 From: Noah Litov Date: Tue, 21 Jan 2020 15:22:23 -0800 Subject: [PATCH 4/4] add basic cdk.json --- typescript/eks/cluster/cdk.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 typescript/eks/cluster/cdk.json diff --git a/typescript/eks/cluster/cdk.json b/typescript/eks/cluster/cdk.json new file mode 100644 index 000000000..cc2ccbdf8 --- /dev/null +++ b/typescript/eks/cluster/cdk.json @@ -0,0 +1,3 @@ +{ + "app": "node index" +}