CDK FARGATE LOAD-BALANCED-SERVICE USING TYPESCRIPT using custom defined VPC


In earlier my blog post, I have created the AWS CDK application for Fargate load-balanced service, and CDK has basically created all the infrastructure .

But their might be a scenario where you need to create resources inside already provided infrastructure resources as you might not have access to create network resources or it the new Far gate service has to be running within other services in the already created VPC resources.

So here in this post, I am using predefined VPC that is already created and named as "MyVPC".

Here is the structure of this VPC as per CDK application called as cdk.context.json.



The Fargate service via CDK will only work if you have public and private sub-nets in all the availability zones, meaning that for example in Sydney region which has following availability zones.

      "ap-southeast-2a",
      "ap-southeast-2b",
      "ap-southeast-2c"
 
You should have subnets for both public and private type. Public subnet should have route to internet via Internet gateway.

Also, the private subnets should have route to Internet via NAT gateway.

 Here is code of my application.

lib/cdkfargatetypescript-stack.ts


Main part of the code so that  ec2.Vpc.fromLookup(this, 'MyVPC',  code snippet works is the changes that has to be done in the Application at  bin/cdkfargatetypescript.ts.



Please note how we have to provide an environment variable that has the accountId of AWS and region ( in my case it is Sydney) and then it has to be pass on to the Stack as a variable which will create then a file as cdk.context.json in the root of the project folder.

So these are the heavy lifting of the project, once this is completed we just follow the drill and create our Fargate load-balanced service as usual. I will outline the steps one by one that I done to deploy my stack and then destroy it.

Step 1.

My AWS_PROFILE is already step as an environment variable so, I am not mentioning here explicitly.  Deploying the stack using cdk deploy command.



I got the load balancer A-record which I have colored out.

Step 2.

Checking the cloud formation console that stack is created successfully.



Step 3.

Verifying that Sample Fargate service is working in the browser.




 Step 4.

Then destroying the stack.



Step 5.

Verifying it via cloud formation console.


Comments

Popular Posts