Sitemap
jerome.decoster

jerome.decoster

Cloud Engineer — Cloud Architecture — DevOps — 3x AWS Certified — http://jeromedecoster.github.io

EKS + DynamoDB

7 min readJul 10, 2022

--

Interract with DynamoDB from an EKS cluster with a Service Account.

The Goal

  • Create a voting app
  • Create a DynamoDB table to store the votes
  • Run the application from a docker image
  • Create an ECR repository and host our docker images
  • Create an EKS cluster
  • Use a service account to be able to interact with the DynamoDB table from the cluster

Setup the table and the repository

You can fork this project

We create the DynamoDB table, the ECR repository and an IAM user using this command :

# terraform create dynamo table + ecr repo + iam user
$ make dynamo-ecr-create

This command creates our resources using a Terraform project

The table is created :

Press enter or click to view image in full size

The repository is created :

Press enter or click to view image in full size

The IAM user is created :

A Policy is attached to the user. It gives him the possibility to read and write in this dynamodb table :

An access key associated with this user has also been created :

Press enter or click to view image in full size

And finally, 4 files have been created at the root of our project. Each file contains an important variable :

  • .env_AWS_ACCOUNT_ID
  • .env_AWS_ACCESS_KEY_ID
  • .env_AWS_SECRET_ACCESS_KEY
  • .env_REPOSITORY_URL

The vote application

You can launch the voting application via this command :

# run vote website using npm - dev mode
$ make vote

We open http://localhost:4000/

Press enter or click to view image in full size

The site works :

Press enter or click to view image in full size

We see that the data is saved in the table :

Running the application in a docker image

We create a docker image :

# build vote image
$ make build

This command runs this simple script :

docker image build \
--file Dockerfile \
--tag vote \
.

The Dockerfile executes the index.js file :

FROM node:18.4-slim# ...CMD ["node", "index.js"]

We start the image we just built with this command :

# run vote image
$ make run

This command runs this simple script :

By opening http://localhost:4000/ we can see that the site no longer works :

Press enter or click to view image in full size

An error is thrown in the terminal :

Indeed, the default behavior of the DynamoDBClient library is to get the identifiers via the ~/.aws/credentials local file

The error is thrown because this file is no longer available within the docker image

Here is a snippet of the application code :

To solve this problem we use the credential-providers package

We use the fromEnv function

Here is a snippet of the updated application code :

We create another docker image :

# build vote-env image
$ make build-env

This command runs this simple script :

docker image build \
--file Dockerfile.env \
--tag vote-env \
.

The Dockerfile executes the index-env.js file :

FROM node:18.4-slim# ...CMD ["node", "index-env.js"]

We start the image we just built with this command :

# run vote-env image
$ make run-env

This command runs this simple script :

By opening http://localhost:4000/ we see that everything is working now :

Press enter or click to view image in full size

To kill the container you can run this command in a new terminal window :

# stop vote-env container
$ make vote-env

Setup the VPC and the EKS cluster

We push these 2 images to ECR with this command :

# push vote + vote-env image to ecr
$ make ecr-push

The images are now in the repository :

Press enter or click to view image in full size

We create a VPC and an EKS cluster using this command :

# terraform create vpc + eks cluster
$ make vpc-eks-create

This command creates our resources by using a new Terraform project

Get jerome.decoster’s stories in your inbox

Join Medium for free to get updates from this writer.

Our cluster has been created :

Press enter or click to view image in full size

We deploy a first version of our application with this command:

# kubectl deploy vote
$ make kubectl-vote

Important : this command uses the kyml application which must be installed

Here is a video demonstration of using kyml

We use the kyml tmpl subcommand to inject variables into our template :

Note : we are launching the vote image, the starting one, which does not contain the management of credentials

Here is our manifest :

Our application is deployed after a few seconds

We can watch in a new terminal window the state of our resources by using this command :

$ watch kubectl get all -n vote

And in another terminal window we execute this command :

# kubectl logs vote app
$ make kubectl-vote-log

This command displays the logs of our pod continuously :

We get the address of the Load Balancer to start the application in the browser :

# get load balancer url
$ make load-balancer

An error is thrown in our terminal :

We remove the vote deployment :

$ kubectl delete deploy vote -n vote

We now deploy the vote-env image with this command :

# kubectl deploy vote-env
$ make kubectl-vote-env

This command runs this script :

The deployment-vote-env.yaml manifest has 2 additional environment variables :

By reloading our browser we see that our application now works :

Press enter or click to view image in full size

But managing our accesses in this way is not the most elegant way

This is also a problem of security and flexibility

We remove the vote deployment again :

$ kubectl delete deploy vote -n vote

About service account

  • When you access the cluster, as a user, the kube-apiserver is used to authenticate you and check the roles and actions you can perform : authentication + authorization
  • User accounts are for humans. Service accounts are for processes, which run in pods
  • Service account are used to provide authentication + authorization mechanism to pods
  • A service role is attached to a pod and allows it to perform certain actions

Setup a service account + deploy the app

We will use the IAM roles for service accounts module to improve our application :

Note : a good quick demonstration video, IAM Roles for Service Accounts & Pods

We remember that an eks-dynamo policy had been created previously :

Press enter or click to view image in full size

To solve our problem you must uncomment the code of these 3 resources :

  1. The iam-role-for-service-accounts-eks module is used to create a role
  2. Then we attach our eks-dynamo policy to the role created by the module
  3. We create a service account within the kubernetes cluster using the Terraform resource kubernetes_service_account

We apply these 3 uncommented resources with this command :

# terraform create vpc + eks cluster
$ make vpc-eks-create

An OpenID Connect identity provider has been created :

Press enter or click to view image in full size

Here are its details :

Press enter or click to view image in full size

The role has been created and the policy is correctly attached :

Press enter or click to view image in full size

It is interesting to see the data displayed via the Trust relationships tab :

Press enter or click to view image in full size

We can see the service account added to the cluster :

$ kubectl get serviceaccount -n vote
NAME SECRETS AGE
default 1 38m
eks-dynamo 1 15m

We can display the YAML content of the service account :

We will now deploy a version of our application that uses this service account :

# kubectl deploy vote with service-account
$ make kubectl-vote-sa

This command runs this script :

Note : we are using the vote docker image again

The manifest just adds an extra line serviceAccountName:eks-dynamo :

By reloading our browser we see that the site is working correctly :

Press enter or click to view image in full size

This demonstration is over, it is important to remove all resources :

# terraform destroy vpc + eks cluster
$ make vpc-eks-destroy
# terraform destroy dynamo table + ecr repo + iam user
$ make dynamo-ecr-destroy

--

--

Written by jerome.decoster

Cloud Engineer — Cloud Architecture — DevOps — 3x AWS Certified — http://jeromedecoster.github.io