EKS + DynamoDB
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-createThis command creates our resources using a Terraform project
The table is created :
The repository is created :
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 :
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 voteWe open http://localhost:4000/
The site works :
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 buildThis 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 runThis command runs this simple script :
By opening http://localhost:4000/ we can see that the site no longer works :
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-envThis 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-envThis command runs this simple script :
By opening http://localhost:4000/ we see that everything is working now :
To kill the container you can run this command in a new terminal window :
# stop vote-env container
$ make vote-envSetup 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-pushThe images are now in the repository :
We create a VPC and an EKS cluster using this command :
# terraform create vpc + eks cluster
$ make vpc-eks-createThis 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 :
We deploy a first version of our application with this command:
# kubectl deploy vote
$ make kubectl-voteImportant : 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 voteAnd in another terminal window we execute this command :
# kubectl logs vote app
$ make kubectl-vote-logThis 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-balancerAn error is thrown in our terminal :
We remove the vote deployment :
$ kubectl delete deploy vote -n voteWe now deploy the vote-env image with this command :
# kubectl deploy vote-env
$ make kubectl-vote-envThis 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 :
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 voteAbout 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 :
To solve our problem you must uncomment the code of these 3 resources :
- The iam-role-for-service-accounts-eks module is used to create a role
- Then we attach our
eks-dynamopolicy to the role created by the module - 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-createAn OpenID Connect identity provider has been created :
Here are its details :
The role has been created and the policy is correctly attached :
It is interesting to see the data displayed via the Trust relationships tab :
We can see the service account added to the cluster :
$ kubectl get serviceaccount -n vote
NAME SECRETS AGE
default 1 38m
eks-dynamo 1 15mWe 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-saThis 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 :
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










