Terraform with TypeScript

mkdir typescript-k8s && cd $_cdktf init --template=typescript --local

The cdktf init command is generating the necessary files to get started with:

  • cdktf.json: containing the CDKTF configuration stating explicitly the providers to use as well (they can be both official or community provided ones — last ones need to be manually installed though).
  • typescript related files: package.json, main.ts, etc…

What we could do for demonstration, it’s a simple deployment of Kubernetes resources into an already created cluster (e.g. local Kind instance). Following code is re-using an official example stated at on the official CDK Github Page or if you want to learn more, head toward the official page tutorials.

First thing, let’s add Kubernetes provider to our cdktf.json as our main provider (and replace the one coming with the boilerplate):

The beauty of CDKTF is that we can download missing providers (listed in the official providers’ list) and automatically generate typings/implementation with the following command (which works also as initial bootstraping):

cdktf get

A .gen folder is created and it contains all the typings/implementation related to the desired providers.

.gen folder with created typings and Typescript files

Now it’s time to edit the “main.ts” which includes a minimalistic approach to help at the beginning:

This sample code does nothing, it is just instantiating MyStack (which extends TerraformStack) and calls the “synth” method, doing the init-plan-apply under the hood.

For instance, we can deploy the Nginx controller on our cluster (namespace, deployment, and related service):

The code embedded in the main.ts file, it creates 3 things:

  • The Namespace which will hold our resources
  • The Deployment with the specified Nginx Image
  • The Service to expose Nginx

If we want to deploy these resources, let’s invoke the following command:

cdktf deploy

And below you can see the related output:

If we run basic commands with kubectl, such as:

+ kubectl get namespaces
NAME STATUS AGE
default Active 3d5h
kube-node-lease Active 3d5h
kube-public Active 3d5h
kube-system Active 3d5h
local-path-storage Active 3d5h
tf-cdk-nginx Active 1m
+ kubectl get deployments -n tf-cdk-nginx
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-example-tf 2/2 2 2 1m

Everything has been created correctly!

We could even destroy the created resources if we wish, by only invoking the following command:

cdktf destroy

Or, if we are getting crazy, we can exactly deploy resources back executing the deploy command again. Unbelievable!!!

Thanks to these tools provided by HashiCorp, a new world opens up!

Now, with a Typescript environment, we could wire in our preferred testing tools.

In this article (and for my personal choice), I’d recommend Jest!

Let’s start installing the dependencies and setup the testing environment:

npm install --save-dev jest babel-jest @babel/core @babel/preset-env @babel/preset-typescript @types/jest

And create the babel.config.js:

To invoke the tests, we can actually replace the existing “test” script in the package.json:

{
"name": "typescript-k8s",
...
"scripts": {
...
"test": "jest",
...
},
...
}

After this, let’s add a simple test for our Cluster Stack and implement some small refactorings to the actual code we wrote:

And if “npm run test” is executed, the output is the following:

Disclaimer: this is for demonstration only and it might test configuration only (which is not a very cool practice). But, this shows how easy it is now to actually write some tests for the logic which prepares and bakes the desired cloud resources.

If you want to play with it, you can find this example codebase on Github.

Author: Shantun Parmar

2,134 thoughts on “Terraform with TypeScript

  1. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  2. You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.

  3. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  4. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  5. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  6. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  7. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  8. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  9. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  10. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  11. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  12. Unquestionably imagine that that you said. Your favorite justification appeared to be on the web the simplest thing to understand of.
    I say to you, I definitely get irked while other people consider
    worries that they just do not recognise about. You controlled to hit the nail upon the
    top and also defined out the entire thing with no need side-effects ,
    other folks can take a signal. Will likely be again to get more.
    Thanks

  13. Taxi moto line
    128 Rue la Boétie
    75008 Paris
    +33 6 51 612 712  

    Taxi moto paris

    I think the admin of this web site is actually working hard in support of his web page, because here every data
    is quality based information.

  14. I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to create my own blog and would like to find out where u got this
    from. kudos

  15. Hello! Someone in my Myspace group shared this site with us so I came to look it over.
    I’m definitely enjoying the information. I’m bookmarking and will be
    tweeting this to my followers! Fantastic blog and amazing design and
    style.

  16. [url=https://ettaloans.com/]best consolidation loans[/url] [url=https://zailoans.com/]loan cash[/url] [url=https://sniloans.com/]advance[/url] [url=https://paydaylo.com/]need a loan today[/url]

  17. Oh my goodness! Amazing article dude! Thank you, However I am having issues with your RSS. I don’t know the reason why I can’t join it. Is there anyone else having the same RSS issues? Anyone who knows the solution will you kindly respond? Thanks!!|

  18. [url=https://atalending.com/]loans bad credit[/url] [url=https://loanspdi.com/]fast easy cash[/url] [url=https://paydaylo.com/]payday loans online[/url] [url=https://kashadvance.com/]online installment loans[/url] [url=https://zailoans.com/]top payday loan companies[/url] [url=https://vedaloans.com/]cash advance inc[/url]

  19. Hi there, just became alert to your blog through Google, and found that it is
    really informative. I’m going to watch out for brussels.

    I will be grateful if you continue this in future. Numerous people will be benefited from your writing.
    Cheers!

  20. [url=http://tadalafilorm.com/]can you buy cialis otc[/url] [url=http://medsptop.com/]generic ceftin[/url] [url=http://eleventabs.com/]coreg 15 mg[/url] [url=http://medcialis.com/]cheap cialis 20mg pills[/url] [url=http://orderivermectin.com/]stromectol ireland[/url] [url=http://hydroxychloroquinev19.com/]hydroxychloroquine sulfate australia[/url] [url=http://pharmotk.com/]lopressor coupon[/url] [url=http://tadalafilfive.com/]cialis tablet online india[/url] [url=http://sildenafilpoi.com/]sildenafil online united states[/url] [url=http://sildenafiltrm.com/]generic female viagra[/url]

  21. [url=http://cashadvi.com/]small loans for bad credit[/url] [url=http://astiloans.com/]loans online instant approval no credit check[/url] [url=http://loanspdi.com/]need money to pay bills[/url] [url=http://conloans.com/]unsecured debt consolidation[/url] [url=http://sniloans.com/]payday loans kansas city[/url] [url=http://jnlending.com/]payday loan no fax[/url] [url=http://paydayover.com/]debt consolidation programs[/url]

  22. [url=http://viagrastab.com/]sildenafil generic australia[/url] [url=http://tadalafilbrm.com/]cialis 20mg cost[/url] [url=http://ivermectinlab.com/]ivermectin 5 mg[/url] [url=http://cifpharm.com/]buy toprol xl 100mg[/url] [url=http://viagracbr.com/]viagra for women 2013[/url]

  23. Does your site have a contact page? I’m having problems locating it but, I’d like to send you an email. I’ve got some ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it develop over time.|

  24. [url=https://astiloans.com/]payday loan online lenders[/url] [url=https://vedaloans.com/]payday loans near me[/url] [url=https://cashadvi.com/]online payday loan lenders[/url] [url=https://lialoans.com/]loans to consolidate debt[/url] [url=https://psiloans.com/]low interest debt consolidation loans[/url] [url=https://usaalend.com/]loans review[/url]

  25. [url=https://viagraneu.com/]purplepill.com[/url] [url=https://viagramru.com/]generic viagra soft tabs uk[/url] [url=https://cialisneo.com/]tadalafil capsules 10mg[/url] [url=https://medsptop.com/]omnicef allergy[/url] [url=https://viagraofp.com/]buy generic viagra professional[/url]

  26. [url=http://vnplending.com/]loan application online[/url] [url=http://usaalend.com/]no credit checks payday loans[/url] [url=http://loanslm.com/]personal loans poor credit[/url] [url=http://ettaloans.com/]loans bad credit[/url] [url=http://vedaloans.com/]debt consolidation program[/url] [url=http://awaloans.com/]unsecured payday loans[/url] [url=http://paydaytwo.com/]alaska loan[/url]

  27. martha stewart cbd line [url=https://bestcbdoiltws.com/ ]charlottes web cbd [/url] buy cbd oil best cbd oil for sale cbd cream for pain relief