How To Deploy Fargate Using AWS CDK

data codes through eyeglasses
Reading Time: 5 minutes


What is AWS Fargate ?

In this blog we’ll see How To Deploy Fargate Using AWS CDK. As per the aws official documentation ” AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. “

In simple words Fargate allows you to run container on ECS In aws without managing the servers.Using this makes it easier to manage as one need not to worry about what server type to use or when to scale the cluster. With this the container runs without any EC2 instance.

Features of Fargate

The various features that makes fargate a unique services are stated below:-

1 – Load balancing

When launching the fargate service if you specify the IP address it registers with the Loadbalancer.

2- Networking

The fargate task are all done under VPC.It has a support for aws vpc networking mode.Using this the service becomes separated form others. Further this helps to take full control of the service .

3 – The configuration can be managed anytime

The configuration for fargate is large enough.There are multiple of combinations of CPU and memory to closely match the needs. The range is from 2 GB TO 8GB. This configuration flexibility helps to attain any configuration any time.

4- Has a support for Container registry

Using the task execution role of fargate one can easily authenticate to help pull images from ECR. Also it helps to connect with docker hub for the same.

How to deploy fargate using aws cdk ?


1- An aws account

2 – AWS cli configured

3 – npm package manager for installing the cdk

4- node installed –> version >14.0.0

Lets get with steps now :

Step 1 – Install aws cdk

To install aws cdk run the below command

npm install -g aws-cdk

After running the above command check for the version using the below command:-

cdk --version

The output is below:

Step 2 – Create a folder for deployment with the basic files

The below commands will help to create the folder.

mkdir aws-fargate
cd aws-fargate

Now in the above folder create the below files :-

a – cdk.json file

The two main objectives of the cdk. json file are: to tell the CDK CLI how to execute our CDK code. and keep track of feature flags , which enable the CDK team to safely roll out new features and bug fixes that cause breaking changes.

The code in the file is

{ "app": "node --experimental-specifier-resolution=node --loader ts-node/esm index.ts" }

b – package.json file

This is your npm module manifest. It includes information like the name of your app, version, dependencies and build scripts like “watch” and “build” .

The code is

  "name": "fargate-application",
  "version": "1.0.0",
  "description": "Running an application load balanced service on Fargate",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "cdk": "cdk"
  "author": {
    "name": "Amazon Web Services",
    "url": "",
    "organization": true
  "license": "Apache-2.0",
  "devDependencies": {
    "@types/node": "^8.10.38",
    "aws-cdk": "*",
    "typescript": "~4.6.0"
  "dependencies": {
    "aws-cdk-lib": "^2.0.0",
    "constructs": "^10.0.0",
    "ts-node": "10.7.0"

The package.json files decribes the name of application.It has got various specs in it.

  • The first spec is about the name , description and scripts.These scripts stated are run in the prompt.
  • The second spec is about author .
  • The third and fourth spec is about license and dependencies involved in the application.

c – index.ts file

This is the typescript file that basically is the main code of implementation.In our file the code written is for creating vpc and instantiating the Fargate Service with just cluster and image.

import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as ecs_patterns from "aws-cdk-lib/aws-ecs-patterns";
import * as cdk from "aws-cdk-lib";
class FirstFargate extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2 });
    const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
    new ecs_patterns.ApplicationLoadBalancedFargateService(this, "FargateService", {
      taskImageOptions: {
        image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
const app = new cdk.App();
new FirstFargate(app, 'FirstService');

So the above code will do the following :

  • import the needed libraries.
  • vpc and fargate cluster will be create.
  • the constructor is create to call the various props.
  • image is pull from the registry.

d – tsconfig.json

The tsconfig. json file specifies the root files and the compiler options required to compile the project.

The code below shows the use of the above file.

    "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "lib": ["es2016", "es2017.object", "es2017.string"],
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterlop": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
"ts-node": {
    "esm": true
"lib": ["esnext"]

The above snippet is showing the compiler option .It basically is showing all the option while compiling the above project.

Till now these were the files and the need for the demo.To deploy the fargate lets move forward

Step 3 – Deploying the above stack

To deploy the above stack run the below command in the same directory where the files are:

npm install

The above command will install all the dependencies mentioned in the stack file.


Next run the below command:

cdk bootstrap

The above command will prepare the environment. The above command will make the cloud formation template in the AWS. It contains the all the resources that are to be make while deploying.


The above output shows that the environment has been bootstrap. After verifying the changes , let’s deploy the application.

To deploy the stack run the below command:

cdk deploy

This command deploys the fargate service.

Output 1

CDK Deploy

Output 2

As you can see that the stack has been deploy.

Below is the output of stack created on AWS.

NOTE: Don’t forget to run the “cdk destroy” command as the resources being use will incur a high cost.

What we achieve from the Above Deployment ?

  • It will automatically configure a load balancer.
  • Create a security group and enable the load balancer to integrate with it.
  • It adds permission for ECR.
  • It prevents an instance from being delete automatically.
  • Automatic scaling is achieve from the above demo.


In conclusion, we saw how we can run container without managing server. Further using fargate made the whole process automated. Fargate is compatible with both Elastic Container Service and Elastic Kubernetes Service.

Happy Learning !!!

Thank You !!!

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .