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