Build Docker Container for Java App and Deploying it on Amazon EKS

Github Link https://github.com/getramki/Deploy-JavaApp-On-EKS.git

This repo contains a Sample Spring Boot Java App with the dockerfile which uses Amazon Corretto 17 as base image and manifestes for creating an Amazon EKS cluster and deploying the sample app to the cluster as a container and exposing it with a service and classic load balancer.

Prerequisites

Docker, AWS Account and IAM user with necessary permissions for creating EKS Cluster, aws cli, configure IAM user with necessary programmatic permissions, eksctl cli, kubectl Please install and configure above before going further

  • You can incur charges in your AWS Account by following this steps below
  • The code will deploy in us-west-2 region, change it where ever necessary if deploying in another region

After downloading the repo in the terminal CD to repo directory and follow the steps for

  1. Building a Docker Image for a Java App and Pushing it to Amazon ECR.
  2. Creating an Amazon EKS cluster with eksctl
  3. Deploying the sample app to the EKS cluster.

Steps for Building a Docker Image and Pushing it to Amazon ECR

  • Change directory to sample
cd sample
  • Run docker daemon
sudo dockerd 
  • Build an image
docker build --tag sample . 
  • View local images
docker images
  • docker build build stage
docker build -t sample-build --target build . 
  • docker build production stage
docker build -t sample-production --target production . 
  • Get ECR Login and pass it to docker
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin Replace-With-AWS-Account-ID.dkr.ecr.us-west-2.amazonaws.com
  • Create ECR repo
aws ecr create-repository --repository-name sample-repo --image-scanning-configuration scanOnPush=true --region us-west-2
  • Tag the image
docker tag sample-production:latest Replace-With-AWS-Account-ID.dkr.ecr.us-west-2.amazonaws.com/sample-repo
  • Push the Image to ECR Repo
docker push Replace-With-AWS-Account-ID.dkr.ecr.us-west-2.amazonaws.com/sample-repo

Create EKS Cluser

Create an Amazon EKS cluster in us-west-2 region with 2 t3.micro instances Creation of EKS cluster can take up to 20 minutes

eksctl create cluster -f devcluster-addons-us-west-2.yaml

Deploy Image to EKS Cluster

Update Image URL in deployment.yaml file Replace-With-AWS-Account-ID

  • Deploy Java Sample-App
kubectl apply -f deployment.yaml
  • Deploy Java Sample-App Service
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
  • Get Deployments
kubectl get deployment sample-app
kubectl get deployments
kubectl get service sample-app -o wide
kubectl get pods -n default

Delete Resources

  • Delete Deployments
kubectl delete deployment sample-app
  • Delete services
kubectl delete service sample-app
  • Delete ingress if you have created it
kubectl delete ingress sample-app
  • Delete Amazon EKS Cluster
eksctl delete cluster -f devcluster-addons-us-west-2.yaml

Query Lambda for RDS MySQL Private Database

Github link https://github.com/getramki/QueryLambda.git

It is important to create a database in private subnets in a VPC and not to expose it to internet, however it is challenging to connect to a private database instance and create the initial Schema and seed the database. This Query Lambda addresses this consern. This repo contains code for a Lambda function written in NodeJS and a SAM template to deploy it.

The Lambda function makes use of best practices of getting the secrets from Secrets Manager and using Layers for MySQL Package.

Prerequisites

AWS Account and IAM user with necessary permissions for creating Lambda, aws cli, SAM cli, configure IAM user with necessary programmatic permissions, RDS MySQL database in a VPC. Please install and configure above before going further

  • You can incur charges in your AWS Account by following this steps below
  • The code will deploy in us-west-2 region, change it where ever necessary if deploying in another region

After downloading the repo in the terminal Change Directory to repo directory and follow the steps for

  • Change Directory into Layer/nodejs folder and run
npm install mysql --save 

or Manually Create the Lambda function and create a layer and add it to Lambda function

  • Create Secret for RDS MySQL Database you have created in the Secrets Manager (in the same region)

Lambda Function Usage

Once lambda is deployed you can make use of Testing built in the Lambda console to interact with database. The function expects three inputs Quesry String – querystr, Database Name – dbname, Secret Manager’s Secret – secret

You can configure test events as follows

{"querystr": "CREATE DATABASE sampledb2", "dbname": "sampledb", "secret": "dbsecret"}
{"querystr": "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))", "dbname": "sampledb","secret": "dbsecret"}
{"querystr": "INSERT INTO customers (name, address) VALUES ('Rama', 'Whitefield Bangalore')", "dbname": "sampledb", "secret": "dbsecret"}
{"querystr": "SELECT * FROM customers","dbname2": "sampledb","secret": "dbsecret"}