EKS sample project with CDK.
- VPC
- EKS cluster
- EKS nodegroup
- Build
- Deploy
npm install -g [email protected]
# 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 accountcdk synth
: synthesizes an AWS CloudFormation template for your appcdk diff
: compares your app with the deployed stackcdk watch
: deployment every time a file change is detected
Stack | Time | |
---|---|---|
1 | VPC | 4m |
2 | EKS cluster | 13m |
3 | EKS nodegroups | 10m |
4 | Deploy(including ALB) | 4m |
Total | 31m |
The VPC ID will be saved into the SSM Parameter Store to refer from other stacks.
Parameter Name: /cdk-eks/vpc-id
Use the -c vpcId
context parameter to use the existing VPC.
cd vpc
cdk deploy
cd ../eks-cluster-nodegroup
cdk deploy
# or define your VPC id with context parameter
cdk deploy -c vpcId=<vpc-id>
eks-cluster/lib/cluster-stack.ts
SSM parameter:
- /cdk-eks/vpc-id
Cluster Name: cluster-config.ts
cd ../eks-nodegroup
cdk deploy
SSM parameters:
- /cdk-eks/vpc-id
- /${clusterName}/openid-connect-provider-arn
- /${clusterName}/kubectl-role-arn
clusterName: eks-cluster-local, eks-cluster-dev, eks-cluster-stg
eks-nodegroup/lib/nodegroup-stack.ts
Create an ECR for sample RESTful API:
REGION=$(aws configure get default.region)
aws ecr create-repository --repository-name sample-rest-api --region ${REGION}
Build and push to ECR:
REGION=$(aws configure get default.region)
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
echo "ACCOUNT_ID: $ACCOUNT_ID"
echo "REGION: $REGION"
cd app
docker build -t sample-rest-api .
docker tag sample-rest-api:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/sample-rest-api:latest
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
docker push ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/sample-rest-api:latest
Create a YAML file for K8s Deployment, Service, HorizontalPodAutoscaler, and Ingress using a template file.
sed -e "s|<account-id>|${ACCOUNT_ID}|g" sample-rest-api-template.yaml | sed -e "s|<region>|${REGION}|g" > sample-rest-api.yaml
cat sample-rest-api.yaml
kubectl apply -f sample-rest-api.yaml
app/sample-rest-api-template.yaml
find . -name "cdk.context.json" -exec rm -f {} \;
find . -name "cdk.out" -exec rm -rf {} \;
cd eks-nodegroup
cdk destroy
cd ../eks-cluster
cdk destroy
cd ../vpc
cdk destroy
.
├── build.gradle
├── cluster-config.ts
├── ssm-prefix.ts
├── package-lock.json
├── package.json
├── tsconfig.json
├── app
│ ├── Dockerfile
│ ├── flask_api.py
│ ├── gunicorn.config.py
│ ├── requirements.txt
│ └── sample-rest-api-template.yaml
├── eks-appdeploy
├── eks-cluster
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ ├── jest.config.js
│ └── lib
│ └── cluster-stack.ts
├── eks-cluster-nodegroup
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ ├── jest.config.js
│ └── lib
│ └── cluster-nodegroup-stack.ts
├── eks-nodegroup
│ ├── bin
│ │ └── index.ts
│ ├── cdk.json
│ ├── jest.config.js
│ └── lib
│ └── nodegroup-stack.ts
└── vpc
├── bin
│ └── index.ts
├── cdk.json
└── lib
└── vpc-stack.ts