54
Introduction to terraform CDK
- It supports TypeScript, Python, C#, Go(Beta), and Java Currently.
Before we get started install cdktf-cli by running npm i -g cdktf-cli@latest
and then run cdktf init
to initialize a broiler code for you. Choose the language and name as your choice. For this instance, I will be using typescript.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Kn83NtE6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jnjh4qtixbszf15g4xd.png)
Let's add AWS providers and try to make an EC2 Instance. There are some prebuilt providers like the following for typescript. So in this case we install the AWS provider by running npm i @cdktf/provider-aws
![](https://res.cloudinary.com/practicaldev/image/fetch/s--vUeA5sY1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/273c5szre8pfaq9rk2eg.png)
If in case you are using any other language then you can add your providers to the terraformProviders field in the cdktf.json file and run cdktf get
to install the constructs and convert them to your native language
Here's my Typescript Code
Running cdktf synth
will create a directory called cdktf.out where you will have your stack named folder which contains the terraform files.
Now you can run terraform init
& terraform plan
to initialize terraform and review your infrastructure resources and if you want to go ahead and apply the changes run terraform apply
- You must have to terraform installed in order for cdktf to work. You can Download the latest version by visiting this link
- If you guys want me write the same for other language do let me know in the comment section below
54