You are on page 1of 10
G Sign in to Medium with Google x Luciano Viana luciano.viana@b: This is your last free member-only story t To create your account, Google will share your name, email address, and profile picture with Medium. See Medium's privacy policy and terms of CDK or Terraform? © snic B@ Pang Bian ( IO SS sep 21,2020-9 min read * CDK vs Terraform know, I know it is late. Picking an infrastructure provider in 2020, what had I been doing all that time... That’s true, but hey at least we are having the final say — who do ‘we want to use for delivering serverless products to the cloud? Decisions, decisions Recently, I had to work on a streak of serverless products. It was a blast, a lot of decisions were made, I learned a lot, I taught a lot. All good, all that Arnold taught us (better yourself, give back, and so on). However, one decision bugged me for some time — what is the best tool to deploy resources to the cloud? This is no small matter — in serverless provisioning is important. Sometimes it is even harder than making the resources (the code) themselves. Let’s do it right. What do we have, what do we need So, the Company is “Serverless first” and we like AWS. That’s the first constraint — we only need to be able to deploy AWS T¢ signin to Medium with Google x infrastructure-as-a-code approach mu separate team of people who would bt J. Luciano Viana person develops code for a lambda fur "uciano.viana@balresdev.com lambda. Then, the deployment frame nota startup, we want stability and fe To create your account, Google will share your name, email address, and profile picture with Medium. See Medium's privacy policy and terms of So, this is it — AWS, comfortable to w_ service fashioned way. research, but here are the options that | considered: © Terraform * AWS CDK © Pulumi I discarded Serverless, Cloud Formation, AWS SAM as either not mature enough (Serverless, like honestly, if you can find something more than a Lambda+API Gateway it would be insane), or not convenient for developers (nobody wants to write miles of Yam in Cloud Formation). Among the three, Pulumi failed the hello-world test. I tried to run a simple Golang lambda example from their docs and it failed with an error. Please hear me out —I did not do any effort on fixing the error or even understanding it. For me, it was enough to see that in the conditions I have the simplest example did not work. The example from the docs. If we fail on step one — let’s not even bother. Please, take my Pulumi evaluation with a fine deal of skepticism — it could totally work for you and by many is believed as the best tool for the task. I just felt discouraged in the first 15 minutes I used it and just discontinued it. So then we are left with Terraform and AWS CDK. Let’s take a deeper look. AWS CDK Created by Amazon themselves, a new product. It employs the infrastructure-as-a-code to the fullest — you can write your infrastructure code in the language of your choice. Typically, we use TypeScript. Underneath, AWS CDK converts the code into a Cloud Formation template and the provision then delivered by it. Terraform Created by HashiCorp, ithas been aro @_ sign in to Medium with Google x providers including AWS. Terraform t are described using HCL (HashiCorp (4. Luciano Viena luciano.viana@bairesdev.com Janguage that is supposed to be both kind of like YAML. It is not YAML, but with. To create your account, Google will share your , . name, email address, and profile picture with Let’s try to put the popularityand the Medium. See Medium's privacy policy and terms of service. Note that J included Terraform and Terraform AWS Module separately, as it is more representative of my case. I only care about the AWS part, so even though Terraform was released in 2014 and CDK only in 2018 it doesn’t mean that Terraform’s AWS support is three times more mature th Module and AWS CDK the difference i year, you know. I think it is still a fairly Luciano Viana The same goes for popularity in the cc "uciano.viana@balresdev.com stars, the Terraform AWS Module and said that, I believe that there are quite G Sign in to Medium with Google x To create your account, Google will share your name, email address, and profile picture with Medium. See Medium's privacy policy and terms of service, Terraform AWS Modules repo and sin github”. This means that Terraform this must be considered. To put it together kudos to Terraform for long life and much love. Terraform wins the maturity award in my eyes. You might say — “Hey, but what about the fact that AWS CDK is written by Amazon, does it not make it more stable by definition? These people, they know their stuff”. I agree with that, however, it is impossible to measure, so as soon as there is no strong evidence for that, I will discard this. I believe time is one of the most critical factors when it comes to maturity. Now, to the best part — how fun is it to write code in both? The actual fun, seriously For the poor souls coming from the Java world the best explanation I can give when comparing CDK and Terraform — it is exactly like Gradle and Maven. The first one is code, the second one is a declaration. CDK gives you the freedom to do whatever, ‘Terraform is more conservative — you need to write what is within the scope. I am not going to go through all the Declarative vs Imperative again. If you are from the Java world, you most likely know already. CDK will give you flexibility and convenience in tough situations, but reading unfamiliar CDK code will surprise you. Terraform is more organized, but this is limiting. Where CDK is winning is that for your project you can choose from a list of programming languages it supports. This is immensely cool, when everything is written (code + infra) in asingle language — it is big. It is not as big as some people might think of, as in practice your infra code even if written in TypeScript will never be using any complex language features. You will never integrate your infra code with source code. Esthetically you will be pleased though. For those who don't understand what is it all about — just ignore it, we will just go through some practical example. Ibel__@_ signin to Medium with Google x Into the Real world iano Vi ., Luciano Viana From now on I will be referring to a pi luciano viana@bairesdev.com Thave written in both Terraform and ¢ https://github.com/Otanikotani/tf-vs deploys. Its just some AWS Glue jobs “teate your account, Google will share your AWS Glue. name, email address, and profile picture with assets), and AWS Neptune cluster. Tht Medium. See Medium's privacy policy and terms of declared in the code) for networking S°'vic® different resources. This is a solid example, not a “hello world”. Let’s go through some concepts and try to evaluate both frameworks accordingly. Structure For the CDK we used TypeScript so that modular structure is inherited. There are certain best practices in CDK. Have a bin/-ts file and lib/ folder contains all the stacks. A stack is a single module of your app. You can always choose to deploy a single stack if you like (not sure why though, but yeah). There are some hard limits on the size of the stack — it can’t have more than 100 resources, but it is unlikely to be hit in a well-designed infra code. Not a factor. In the sample repository, we have three stacks: Neptune, Network, and Orchestration. Stacks can be dependent on each other — since Neptune requires Network resources, you can see that the Network stack is passed as an argument to Neptune. This is very handy and comfortable. Just like in normal programming. I once ran into an issue of circular dependencies in CDK and apparently, it is quite easy to run into it. CDK couldn’t resolve it properly even though I think I did not have any cycles in the dependency graph of the stacks. Oh well, just simplify things then, not too much of a trouble. In Terraform there are Modules. Each module can contain multiple .tf files. So in tf/ you will find a main.tf and three subfolders named after the modules. The modules are identical to what we have in CDK. The difference here is building the dependencies. While in CDK it was easy, Terraform requires you to jump through. For example, to pass a Neptune endpoint URL from the AWS Neptune module to Orchestration I had to: * Declare a variable variable “neptune_endpoint” { } in the Orchestration module.

You might also like