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

> Deploy a Kubernetes Fargate on EKS cluster using the AWS CDK.

- Canonical: https://dennisokeeffe.com/blog/2021-08-12-deploying-an-eks-fargate-cluster-with-the-aws-cdk
- Published: 2021-08-12
- Updated: 2021-08-12
- Tags: aws, aws-cdk, typescript, kubernetes

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](https://kubernetes.io/). 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](https://github.com/okeeffed/deploying-an-eks-fargate-cluster-with-the-aws-cdk)

## Prerequisites

1. Basic familiarity with [AWS CDK for TypeScript](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-typescript.html).
2. Familiarity with [Kubernetes](https://kubernetes.io/).

## Getting started

We will clone the template repo to get started:

```s
$ 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:

```s
@aws-cdk/core
@aws-cdk/aws-eks
```

We can install these prior to doing any work:

```s
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:

```s
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:

```ts
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

```s
$ 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:

```s
$ 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   172.20.0.1   <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](https://aws.amazon.com/premiumsupport/knowledge-center/eks-alb-ingress-controller-fargate/).

## Destroying the cluster

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

```s
$ 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
```

## Summary

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

1. [AWS CDK for TypeScript](https://docs.aws.amazon.com/cdk/latest/guide/work-with-cdk-typescript.html)
2. [Project code](https://github.com/okeeffed/deploying-an-eks-fargate-cluster-with-the-aws-cdk)
3. [Kubernetes](https://kubernetes.io/)
4. [EKS Application Load Balancer Ingress Controller for Fargate](https://aws.amazon.com/premiumsupport/knowledge-center/eks-alb-ingress-controller-fargate/)

_Photo credit: [`picsbyjameslee`](https://unsplash.com/@picsbyjameslee)_
