🎉 I'm releasing 12 products in 12 months! If you love product, checkout my new blog workingoutloud.dev

Back to home

Deploying An EKS Fargate Cluster With The AWS CDK (Part 1)

This post will demonstrate how we can deploy a Fargate on EKS Kubernetes cluster to run a simple application. Part one will focus on deploying the Kubernetes cluster with a running pod, but will not include the load balancer for the application.

This post will not be covering the basics of Kubernetes. If you want to learn more about that, I will be writing more on Kubernetes in upcoming posts.

Source code for this blog post can be found here


  1. Basic familiarity with AWS CDK for TypeScript.
  2. Familiarity with Kubernetes.

Getting started

We will clone the template repo to get started:

$ git clone https://github.com/okeeffed/using-the-aws-cdk-with-localstack-and-aws-cdk-local deploying-an-eks-fargate-cluster-with-the-aws-cdk $ cd deploying-an-eks-fargate-cluster-with-the-aws-cdk $ npm i # if install fails, remove the lockfile and try again

At this stage, we will have the app in a basic working state.

In order to create the CDN stack, we will require the following AWS CDK libraries:

@aws-cdk/core @aws-cdk/aws-eks

We can install these prior to doing any work:

npm i @aws-cdk/core @aws-cdk/aws-eks

The repository that we cloned already has a upgrade script supplied. We can use this to ensure our CDK packages are at parity:

npm run upgrade

We are now at a stage add a pattern construct to a stack.

Updating our stack for the Fargate on EKS cluster

In lib/aws-cdk-with-typescript-foundations-stack.ts, update to add the following code:

import * as eks from "@aws-cdk/aws-eks"; import * as cdk from "@aws-cdk/core"; export class AwsCdkWithTypescriptFoundationsStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // The code that defines your stack goes here const cluster = new eks.FargateCluster(this, "HelloEKS", { version: eks.KubernetesVersion.V1_20, clusterName: "fargate-demo", }); // apply a kubernetes manifest to the cluster cluster.addManifest("mypod", { apiVersion: "v1", kind: "Pod", metadata: { name: "mypod" }, spec: { containers: [ { name: "hello", image: "amazon/amazon-ecs-sample", ports: [{ containerPort: 8080 }], }, ], }, }); } }

This code will create a fargate cluster and deploy a pod mypod with a container amazon/amazon-ecs-sample running on port 8080.

We will check to ensure the pod runs, but we will not be exposing it through a load balancer in this blog post.

Deploying the cluster

$ npm run cdk synth $ npm run cdk bootstrap $ npm run cdk deploy # ... lots of output ✅ AwsCdkWithTypescriptFoundationsStack Outputs: AwsCdkWithTypescriptFoundationsStack.HelloEKSConfigCommand861347FC = aws eks update-kubeconfig --name fargate-demo --region us-east-1 --role-arn arn:aws:iam::123:role/AwsCdkWithTypescriptFound-HelloEKSMastersRole53742-123 AwsCdkWithTypescriptFoundationsStack.HelloEKSGetTokenCommandF486E67D = aws eks get-token --cluster-name fargate-demo --region us-east-1 --role-arn arn:aws:iam::123:role/AwsCdkWithTypescriptFound-HelloEKSMastersRole53742-123 Stack ARN: arn:aws:cloudformation:us-east-1:178467697118:stack/AwsCdkWithTypescriptFoundationsStack/2d451350-fa88-11eb-9655-12afb4d5f445

The Outputs log will contain a command to use to update the kubeconfig file.

In our case, this is aws eks update-kubeconfig --name fargate-demo --region us-east-1 --role-arn arn:aws:iam::123:role/AwsCdkWithTypescriptFound-HelloEKSMastersRole53742-123. Run that command to set up your kubectl config.

Checking the pod deployment

We can check the pod has deploy as expected by running the kubectl get all or kubectl get pods command:

$ kubectl get all NAME READY STATUS RESTARTS AGE pod/mypod 0/1 ContainerCreating 0 69s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP <none> 443/TCP 11m $ kubectl get pods NAME READY STATUS RESTARTS AGE mypod 1/1 Running 0 2m29s

In the above example, you can see that the pod is running on our Fargate on EKS cluster. Success!

At the time of writing, you will need to do more work to add a LoadBalancer with Fargate on EKS.

Destroying the cluster

EKS can be very expensive - be sure to tear down the cluster when you are done with this demo:

$ npm run cdk destroy > aws-cdk-with-typescript-foundations@0.1.0 cdk > cdk "destroy" Are you sure you want to delete: AwsCdkWithTypescriptFoundationsStack (y/n)? y AwsCdkWithTypescriptFoundationsStack: destroying... ✅ AwsCdkWithTypescriptFoundationsStack: destroyed


Today's post demonstrated how to deploy a Fargate on EKS cluster using the AWS CDK along with a pod and container.

Following posts will demonstrate how to add a load balancer.

Resources and further reading

Photo credit: picsbyjameslee

Personal image

Dennis O'Keeffe

  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.


Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

Deploying An EKS Fargate Cluster With The AWS CDK (Part 1)


Share this post