NLB and ECS Service with CDK

Sathish S
Nggawe Nirman Tech Blog
4 min readMar 25, 2020

The AWS CDK is a framework for modeling cloud infrastructure as reusable components and then provisioning through declarative AWS CloudFormation. It supports multiple languages. It orchestrates the building of cloudformation stacks.

AWS CDK Architecture

ApplicationExecutable programs and is used to render and deploy CFN templatesStack(s)Deployable units and it knows about region and accountConstruct(s)Representations of AWS resources and can form a hierarchical tree structure

Advantages with AWS-CDK based code

  • CDK is a developer-friendly version of Cloud Formation
  • Apply software development best practices to infrastructure management
  • With AWS CDK we can write less code
  • Easy to share and reuse your infrastructure as a library
  • Easy to use logic (if statements, for-loops, etc.) to define your infrastructure
  • Easy to integrate with the code review workflow
  • Code completion within your IDE
  • Easy to integrate with our CI/CD process

AWS CDK Example : NLB based ECS Fargate Service

With a sample example in github, check out the CDK Install and configure it. Then check out the fargate-service-nlb example from github and follow the steps from readme.

AWS local configurations for CDK

$ aws configure

Install aws-cdk

$ npm install -g aws-cdk

Initialize the CDK

$ mkdir fargate-service-nlb && cd fargate-service-nlb$ cdk init --language=typescript

Build code

$ npm run build

Synthesize the cloud formation template code

$ cdk synth

Deploy the Infra on AWS

$ cdk deploy

To Run the Service Deployed

Create an API Gateway manually to test the service which is avialable via NLB through the VPC link integration. (See below screen shots)

Destroy and cleanup

$ aws cloudformation delete-stack --stack-name search-api-service

Implementation Summary

AWS VPC , Role and Permissions

ECS Cluster (Fargate type), Task Definition with Container from ECR, Log Group Def

Network Load Balancer (NLB) , Security Group , and ECS Fargate Service

Target group and application to call the Stack and in turn it calls constructs

CDK Deployment on AWS (Check)

ECS Fargate Cluseter : Social API Cluster with Search Service
Network Load Balancer with listeners for the Example Service API
Target Group for the NLB and ECS Cluster Service API
Example Service API logs indicating starting of the service and ready to serve

An APIGateway with VPCLink created manually to test the NLB based ECS Service (fargate)

ECS fargate with NLB via API gateway (vpc-link)
Example Search API Service endpoint test results

Conclusion

  • The AWS CDK is a powerful tool that gives the developers the flexibility and ease of maintaining the application’s infrastructure code as they would any other application’s code. By simply abstracting the markup or declarative template files into readable code.
  • When our CDK based PoC was ready and it was a pretty good learning experience. Having said that, there are always some flaws, like debugging the code for some time because there was a new release with breaking changes, but it’s natural for a tool under heavy development.
  • Cloudformation template from the synth is still easily readable by a human. One can take the cloudformation template and edit it.
  • When you are doing the IaaC try with the CloudFormation, see how it works out, then try to convert it into AWS CDK. You will be best judge to decide whether you would want continue with CloudFormation or shift to either AWS cDK or tools such as Terraform.

One thing to note is there is no one thing fits all , it all depends on your requirements, team, project and your company.

Where can you start ?

--

--