Deploy your application on Kubernetes (Amazon EKS) using AWS Serverless (Codebuild)
Tags: aws, devops, kubernetes, serverless
Published: 2022-07-21 14:52:32

Hi folks, 
Recently I came across an use-case of deploying a microservice on EKS using CodeBuild with GitHub as source. Although I've used Jenkins numerous times to do the same thing but I haven't used AWS Serverless to deploy on EKS. 

> This blog is for you if you want to deploy your microservice to Kubernetes, or want to learn how to setup AWS CodePipeline with CodeBuild, or like to integrate CodeBuild with EKS or you are generally curious about Kubernetes and Serverless :)

## This is how I started the journey:
- I enquired about the source code and got to know it was on GitHub. For this blogpost I am going to create my own GitHub Repo with a basic deployment manifest. You can create your own repo and have the full stack of manifests starting from Ingress, to the service, deployment etc required for your microservice.

Next, I setup a CodePipeline. I'll show you how to do that:
- Navigate to Developer Tools in the console, and select Code Pipeline.
Provide your pipeline a name, select the default service role so CodePipeline can create a role on your behalf, let the advanced setting be as it is unless you have a custom location for your artifact and want to use a custom KMS Key.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/at6l4zolgri82uz6bnyw.png)

Moving on you need to specify where is your SourceCode that you want to build or deploy.
- Add a Source Provider: In our case that would be GitHub (Version2).
Connection: Select if you have an existing connection with your GitHub Account or Create a connection for GitHub, it's fairly simple.
Once you authenticate CodePipeline to connect to your GitHub Account you shall receive a CodeStar connection URL, use that.
Once you fill in all the details it should look something like this.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skg7zfrlsfxocqlodx63.png)

[My GitHub Repo](https://github.com/RajitPaul11/my_dummy_microservice_deploy_on_eks.git)

Next, we shall add a build stage.
- As a build provider we will select AWS CodeBuild. Feel free to choose the region of your choice. If you have an existing project you can select the same or else create a new project. I am going to setup a project from scratch.

- To setup a CodeBuild Project you need to provide the Project Name & Description, you can also restrict concurrent builds and provide additional tags if you want.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xial4mclsjhmwj58tzgr.png)

- Design the CodeBuild Environment, we shall go with the latest image of Amazon Linux 2 and ask CodeBuild to create a new service role on our behalf.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2u6xv7wr6ytvvxid706.png)

- Additional Environment Configuration
Select your VPC, choose a private subnet and a SG with outbound allow and then validate your VPC setting. This is where your codebuild server will be provisioned ~ No, Serverless does not mean there are no servers, it's just that, you don't have to manage them ;)
Provide appropriate compute resource to the server as per your code requirements and we are good to go. If you require you can add environment variables and filesystems for your server.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0w25wz2j3xtfk06th8j.png)

- You can leave the buildspec section empty if your buildspec file is buildspec.yml as codebuild will look for that file in your repo, if you have named your file otherwise you can mention that in the buildspec name section. Also if you have some additional requirements while building your code you can mention those in the additional build commands.
Will ignore Batch configuration as we do not require that for this blogpost.
It's best practise to export your build logs to Cloudwatch so that it's easier for you to troubleshoot. Additionally you can also export your CodeBuild logs to S3 for later analysis.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsizizg3z7y58qaotgbz.png)

Once you click on continue to CodePipeline, the CodeBuild Project will be created and you can complete your CodePipeline setup.
- In environment variables you can refer to environment values generated from CodePipeline or can add new env variables. On Build Type, we shall be executing a single build on execution.

Skip Deploy stage as codebuild will be taking care of the deployment and create your CodePipeline :)

- EKS Cluster: I shall consider you have a running EKS cluster where we shall be doing the deployment, if not, you can deploy a new EKS cluster. In case you need help, refer this previous blog of mine - [Setup your EKS Cluster from scratch](https://dev.to/aws-builders/creating-an-amazon-eks-cluster-from-scratch-using-eksctl-540i)

To allow CodeBuild to deploy on the EKS cluster we need to modify EKS RBAC by adding the CodeBuild Service Role with the required permission on the aws-auth configmap that is used to manage EKS RBAC.
[To know more about EKS RBAC](https://dev.to/rajitpaul_savesoil/read-only-access-to-specific-resources-in-aws-eks-cluster-via-eks-authentication-authorization-2i13)

## Locate your CodeBuild Service Role:
- Open your buildproject, that you shall be using.
In Build details tab, scroll down to Environment where you can see the Service Role hyperlink. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4f05qhyby9ouz5vs7ly.png)

## Update the aws-auth configmap:
- To edit the configmap run - <b>kubectl edit cm aws-auth -n kube-system</b>

Under mapRoles we shall add a new entry:
 

```
- groups:
    - system:masters
  rolearn: arn:aws:iam::xxxaccidxxx:role/codebuild- microservice-deploy-to-eks-service-role 
  username: CodeBuild Role to Access EKS
```

> **Note** If you directly copy paste the CodeBuild Role ARN from the console to the configmap you will get a "error: You must be logged in to the server (Unauthorized)", make sure your remove the /servicerole path from the ARN.

- Additionally to the CodeBuild Service Role attach a policy with eks:DescribeCluster action allowed. This will allow codebuild to download the kubeconfig file onto it's server.

## Deploy Your Application, Run the Pipeline:
- Once you have done all as I mentioned, you would have your application running on EKS with the help of CodeBuild :)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krwakndm5ktxwzzxuja2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l796hxy2afu5mpdq1ynh.png)

## Clean UP
- Delete your CodeBuild Project
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfikg5ai5hlz0wag44vj.png)

- Delete your Pipeline
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8x2mor3uspnv07gqmte4.png)

  

If you want to know more about Kubernetes, DevOps, Serverless follow me, also I would love to have a chat with you on [LinkedIn](https://www.linkedin.com/in/rajitpaul/) :)