Introduction: CDKTF for AWS VPC Creation

Building a robust and scalable network infrastructure is essential for any enterprise in today’s cloud-driven world. AWS offers powerful tools for network management, but creating and managing Virtual Private Clouds (VPCs) can be complex. This is where the AWS Cloud Development Kit for Terraform (CDKTF) comes into play. CDKTF combines the benefits of Terraform’s infrastructure-as-code (IaC) with the programming capabilities of TypeScript, allowing you to design, manage, and deploy your AWS network infrastructure efficiently.

This hands-on guide will walk you through creating a fully functional AWS VPC using CDKTF. By the end of this guide, you’ll have a solid understanding of leveraging CDKTF to build a scalable and secure network foundation on AWS.

Project Setup: Initializing CDKTF for TypeScript

Prerequisites

Before you begin, ensure you have the following installed on your machine:

  • Node.js (LTS version recommended)
  • Terraform CLI
  • AWS CLI configured with your credentials
  • CDKTF CLI

Step 1: Initialize Your CDKTF Project

To start, create a new directory for your project and navigate into it:

mkdir my-vpc-project

cd my-vpc-project

Initialize a new CDKTF project:

cdktf init –template=typescript –local

This command creates a new CDKTF project with the necessary TypeScript and Terraform configuration files.

Network File: Creating the VPC Foundation

Step 2: Define Your VPC

In the main.ts file, import the necessary CDKTF modules and define your VPC:

import { Construct } from “constructs”;

import { App, TerraformStack } from “cdktf”;

import { AwsProvider, Vpc } from “@cdktf/provider-aws”;

class MyVpcStack extends TerraformStack {

  constructor(scope: Construct, id: string) {

    super(scope, id);

    new AwsProvider(this, “AWS”, {

      region: “us-west-2”,

    });

    new Vpc(this, “MyVPC”, {

      cidrBlock: “10.0.0.0/16”,

    });

  }

}

const app = new App();

new MyVpcStack(app, “my-vpc”);

app.synth();

This code snippet initializes an AWS provider and creates a basic VPC with the CIDR block 10.0.0.0/16.

Subnet Creation: Designing Your Network Layout

Step 3: Add Subnets to Your VPC

Subnets are crucial for segmenting your network. Add the following code to create public and private subnets:

import { Subnet } from “@cdktf/provider-aws/lib/subnet”;

const publicSubnet = new Subnet(this, “PublicSubnet”, {

  vpcId: vpc.id,

  cidrBlock: “10.0.1.0/24”,

  availabilityZone: “us-west-2a”,

  mapPublicIpOnLaunch: true,

});

const privateSubnet = new Subnet(this, “PrivateSubnet”, {

  vpcId: vpc.id,

  cidrBlock: “10.0.2.0/24”,

  availabilityZone: “us-west-2a”,

});

This code creates a public subnet with automatic public IP assignment and a private subnet within the same availability zone.

Routing and Gateways: Enabling Connectivity

Step 4: Configure Routing and Gateways

To enable internet access for your VPC, you need to set up an Internet Gateway and route tables:

import { InternetGateway, RouteTable, Route, RouteTableAssociation } from “@cdktf/provider-aws/lib/vpc”;

const internetGateway = new InternetGateway(this, “InternetGateway”, {

  vpcId: vpc.id,

});

const routeTable = new RouteTable(this, “RouteTable”, {

  vpcId: vpc.id,

});

new Route(this, “Route”, {

  routeTableId: routeTable.id,

  destinationCidrBlock: “0.0.0.0/0”,

  gatewayId: internetGateway.id,

});

new RouteTableAssociation(this, “PublicSubnetAssociation”, {

  subnetId: publicSubnet.id,

  routeTableId: routeTable.id,

});

This code creates an Internet Gateway, associates it with the VPC, and sets up routing for Internet access.

Deployment: Launching Your AWS VPC

Step 5: Deploy Your Infrastructure

With your VPC configuration complete, it’s time to deploy it to AWS. Run the following commands:

cdktf get

cdktf deploy

The cdktf get command installs the necessary providers, and cdktf deploy provisions for your VPC on AWS.

Conclusion: Your AWS VPC – Ready for Resources

Congratulations! You have successfully created a VPC with public and private subnets, an Internet Gateway, and routing configurations using CDKTF. This foundational network infrastructure can deploy additional resources like EC2 instances, RDS databases, and more.

By leveraging CDKTF and TypeScript, you can efficiently manage and scale your AWS network infrastructure with ease, ensuring security, scalability, and reliability.

References

Use AWS CDK to initialize Amazon RDS instances

What is the AWS CDK?