/eks-cdk

EKS sample project with CDK

Primary LanguageTypeScript

EKS with CDK

Build Quality Gate Status Lines of Code

eks-cdk

Table of Contents

  1. VPC
  2. EKS cluster
  3. EKS nodegroup
  4. Build and push to ECR
  5. Deploy

Prerequisites

npm install -g aws-cdk@2.32.1

# install packages in the root folder
npm install
cdk bootstrap

Use the cdk command-line toolkit to interact with your project:

  • cdk deploy: deploys your app into an AWS account
  • cdk synth: synthesizes an AWS CloudFormation template for your app
  • cdk diff: compares your app with the deployed stack
  • cdk watch: deployment every time a file change is detected

CDK Stack

Stack Time
1 VPC 3m 30s (optional)
2 EKS cluster 13m
3 EKS nodegroups 10m
4 Build image and push to ECR 4m
5 Deploy(including ALB) 4m
Total 31m (34m 30s with a new VPC)

Deploy

Step 1: VPC

The VPC ID will be saved into the SSM Parameter Store named /eks-cdk/vpc-id to refer from other stacks.

Create the SSM Parameter if you want to use the existing VPC:

aws ssm put-parameter --name "/eks-cdk/vpc-id" --value "{existing-vpc-id}" --type String 
cd vpc
cdk deploy

01-vpc/lib/vpc-stack.ts

Step 2: EKS cluster

cd ../eks-cluster-nodegroup
cdk deploy 

# or define your VPC id with context parameter
cdk deploy -c vpcId=<vpc-id>

02-eks-cluster/lib/cluster-stack.ts

SSM parameter:

  • /eks-cdk/vpc-id

Cluster Name: config.ts

Step 3: EKS nodegroup

cd ../03-eks-nodegroup
cdk deploy 

SSM parameters:

  • /eks-cdk/vpc-id
  • /${clusterName}/openid-connect-provider-arn
  • /${clusterName}/kubectl-role-arn
clusterName: eks-cluster-local, eks-cluster-dev, eks-cluster-stg

03-eks-nodegroup/lib/nodegroup-stack.ts

Step 4: Build the SpringBoot Ping API

Build and push to ECR:

cd ../04-ecr
cdk deploy 

04-ecr/lib/ecr-stack.ts

Step 5: Deploy the API

Create a YAML file for K8s Deployment, Service, HorizontalPodAutoscaler, and Ingress using a template file.

cd ../app
sed -e "s|<account-id>|${ACCOUNT_ID}|g" ping-api-template.yaml | sed -e "s|<region>|${REGION}|g" > ping-api.yaml
cat ping-api.yaml
kubectl apply -f ping-api.yaml

app/ping-api-template.yaml

Cleanup

find . -name "node_modules" -exec rm -rf {} \;
find . -name "cdk.context.json" -exec rm -f {} \;
find . -name "cdk.out" -exec rm -rf {} \;
find . -name "build" -exec rm -rf {} \;

cd 04-ecr
cdk destroy

cd ../03-eks-nodegroup
cdk destroy

cd ../02-eks-cluster
cdk destroy

cd ../01-vpc
cdk destroy

Structure

.
├── build.gradle
├── config.ts
├── package-lock.json
├── package.json
├── tsconfig.json
├── 01-vpc
│   ├── bin
│   │   └── index.ts
│   ├── cdk.json
│   ├── jest.config.js
│   └── lib
│       └── vpc-stack.ts
├── 02 eks-cluster
│   ├── bin
│   │   └── index.ts
│   ├── cdk.json
│   ├── jest.config.js
│   └── lib
│       └── cluster-stack.ts
├── 03-eks-nodegroup
│   ├── bin
│   │   └── index.ts
│   ├── cdk.json
│   ├── jest.config.js
│   └── lib
│       └── nodegroup-stack.ts
├── 04-ecr
│   ├── bin
│   │   └── index.ts
│   ├── cdk.json
│   └── lib
│       └── ecr-stack.ts
├── eks-cluster-nodegroup
│   ├── bin
│   │   └── index.ts
│   ├── cdk.json
│   ├── jest.config.js
│   └── lib
│       └── cluster-nodegroup-stack.ts
├── app
│   ├── Dockerfile
│   ├── build.gradle
│   ├── build.sh
│   ├── ping-api-template.yaml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── sample
│           │           ├── SampleApplication.java
│           │           └── SampleController.java
│           └── resources
│               └── application.yaml

Reference

CDK Lib