Kumologica Container Inside AWS ECS Fargate

We have seen multiple examples and use cases of building a low code API using Kumologica and running it on AWS lambda and on Azure function. In this article, we will be taking through the build and deployment of a simple hello world service Docker container inside AWS Elastic Container Service (ECS) Fargate.

Container ship in water

Photo by Chris Linnett

Architecture

Based on the design defined in the diagram (Figure 1), we will be building Kumologica applications as a Docker container. Once the Docker image is ready, we will push it into the AWS Elastic Container Registry (ECR). ECR holds all the Docker images. The Elastic Container Service is an AWS container management platform. The workload availability, scaling, and network are managed by ECS. In order to deploy a workload to ECS, an ECS task definition needs to be created, which will define the Docker image that needs to be used, the name of the service, and the type of launch. In this case, we will be choosing the launch type as Fargate. 

Technical architecture diagram

Figure 1: Technical architecture diagram (Author Pranav K)

The incoming request traffic to the deployed containers is balanced by the AWS application load balancer. The scope of this tutorial is limited to exposing the services via application load balancer even though from a standard approach the load balancer can be further attached to an API gateway and API gateway attached to Route53.   

Implementation

In this section, we will get into the prerequisites and step-by-step implementation of build and deployment using Kumologica and ECS fargate respectively.

Prerequisites

In order to implement the architecture, we need the following platforms and tooling available.

  1. Access to an AWS account
  2. Install AWS CLI on your local machine.
  3. Download and install Kumologica.
  4. Download and install Docker.

Once the above prerequisites are met, then we can start the implementation steps.

Steps  

Building the Flow

We need to start by building a simple API service flow in Kumologica which we will later Dockerize. To do this, let's open Kumologica Designer.

Plain Text
 
Display Name : [GET] /hello
Provider : AWS
Event Source : Amazon API Gateway
Verb : GET
URL : /hello


Plain Text
 
Display Name: Log_Entry
Level : INFO
Message : Inside the service
Log format : String


Plain Text
 
Display Name : Success
Response : Http response
Status code : 200
Content-Type : application/json
Payload : {"status" : "HelloWorld"}


The flow would look as shown below in Figure 2.

Hello World API flow

Figure 2: Hello World API flow

Dockerizing the API Flow

In this section, we will see how to Dockerize the Kumologica flow. Ensure to have the Docker file and index.js file in the root folder of your Kumologica project (Figure 4). These two files are not defaulted in the Kumologica project.

Project structure

Figure 3: Project structure  (Author Pranav K)

Following is the Docker file.

Dockerfile
 
FROM node:16-alpine 
WORKDIR /app 
COPY package*.json ./ 
RUN npm install 
ENV PATH /app/node_modules/.bin:$PATH 
COPY . . 
EXPOSE 1880
CMD ["node","index.js"]


Following is the index.js file. Here, hello-world-service-flow.json is the flow file in the project folder.

JavaScript
 
const { NodeJsFlowBuilder } = require('@kumologica/runtime');
new NodeJsFlowBuilder('hello-world-service-flow.json').listen();


  1. Now we will do the Docker build with the following command: docker build . -t hello-kl-docker-app.
  2. Once the Docker build is completed, we need to push the image to AWS ECR. For this, we need to first login to ECR via the Docker client using the following command. The below command is for a private registry. It may vary for the public ECR registry: aws ecr get-login-password --region <<aws region>> | docker login -u AWS -p $(aws ecr get-login-password --region <<aws region>>)  <<aws accountid>>.dkr.ecr.<<aws region>>.amazonaws.com
  3. Once the client login is established with ECR, we will tag the image using the following command: docker tag hello-kl-docker-app <<aws accountid>>.dkr.ecr.<<aws region>>.amazonaws.com/hello-kl-docker-app:latest
  4. Create an ECR private repo in AWS either via console or via command line. The repo name should match when pushing the Docker image.
  5. Once we have the repo created, let's push the Docker image with the following command: docker push <<aws accountid>>.dkr.ecr.<<aws region>>.amazonaws.com/hello-kl-docker-app:latest.

Setting up ECS

The first step in setting up the ECS Fargate is creating a cluster by providing the cluster name.

ECS cluster

Figure 4: ECS cluster (Author Pranav K)

The next step is to create a task definition to deploy the container.  Go to AWS ECS and create a task definition. 

  1. Provide task definition family name.
  2. Select AWS Fargate as the launch type.
  3. Select the repo image URI and container name.
  4. Provide the port mapping with the container port as 1880.

Leave the memory, CPU params, and other parameters as default.

Task definition

Figure 5: Task definition (Author Pranav K)

Deploying the Service

To make a deployment to ECS we need to select either a service or a task. In this tutorial, we are deploying an API so we select service.  

Create a service under the cluster by selecting the launch type as Fargate.

  1. Provide service name.
  2. Provide the launch type as Fargate.
  3. Select the task definition that was created in the earlier step.
  4. Leave the execution role as default.
  5. Select the load balancing:
    1. Select application load balancer (Since API).
    2. Provide the listener port as HTTP 80.
    3. Provide the health port as 1880 and the resource path as the one defined in the KL flow.
  6. Leave the rest all default.

Ensure that the security group attached to the application load balancer has the inbound traffic opened for HTTP and the listener port allocated in the earlier service creation step.

Once deployed, you can go to the application load balancer section to fetch the ARecord URL DNS generated. Use the path /hello with the DNS to invoke the endpoint. This will return with the message : {"status" : "HelloWorld"}

Conclusion

I hope the tutorial gave you a thorough understanding of how to build and deploy a Kumologica API flow into ECS Fargate. However, it does not include details on script-based provisioning of the ECS cluster, task definition, or ECS service deployment. These topics will be addressed in a separate article.

 

 

 

 

Top