In the previous post, we learned about how to create subnets for a VPC. Now, let's learn about how to create VPC by making use of Terraform Cloud Development Kit.
What is CDKTF ?
Before proceeding further, let us learn what is CDKTF?
Cloud Development Kit for Terraform (CDKTF) allows us to use familiar programming languages to define and provision infrastructure. This gives us access to the entire Terraform ecosystem without learning HashiCorp Configuration Language also known as HCL and lets you leverage the power of the existing toolchain for testing, dependency management, etc.
CDKTF currently supports Typescript, Python, Java , C#.
We can find more information about CDKTF i.e. how to setup CDKTF and create and initialise a project refer the docs here.
Now, assuming that we have a fair bit of knowledge about CDKTF and how it works, let's proceed further to create a VPC by making use of CDKTF. We'll be using the typescript language.
Now let us learn what is going on in the above main.ts snippet.
Constructs are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component. Constructs are part of the Construct Programming Model (CPM) and are also used by other tools such as CDK for Terraform (CDKTF).
Next, we are importing the modules provided by cdktf/provider-aws . Since the provider is AWS, it depicts that are going to create our resources in AWS.
We set the AWS Credentials in the function _setAwsProvider so that CDKTF can create the resources in the particular region for that account.
We now assign a valid CIDR value in order to create a VPC. In the above example, we are gonna use 10.0.0.0/16 as the CIDR for our VPC. We give a name to the vpc and then we define the objects needed to created our VPC such as setting Availability Zones for our VPC, Creating Public/Private subnets and configuring a few settings such as:-
Here is our utils.ts where we define the public and the private subnets functions.
The above snippet, defines the subnet CIDR ranges as we had seen in the previous article. We are creating two public subnets and two private subnets based on the above snippet.