- Explore the Terraform for Terraform CLI >= v1.0+
- Explore the Nodejs for npm CLI >= v14+
- Explore the Yarn for Yarn CLI >= v1.21 (optional - NPM will work as an alternative)
- Explore the CDK for cdktf CLI
Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.
$ export AWS_ACCESS_KEY_ID=AAAAAA
$ export AWS_SECRET_ACCESS_KEY=AAAAA
A CDK for Terraform application in TypeScript for VPC configuraiton.
Install project dependencies
yarn install
Generate CDK for Terraform constructs for Terraform provides and modules used in the project.
cdktf get
You can now edit the main.ts
file if you want to modify any code.
vim main.ts
import { Construct } from "constructs";
import { App, TerraformStack, TerraformOutput } from "cdktf";
import {
AwsProvider,
vpc,
ec2
} from "./.gen/providers/aws/";
class MyStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new AwsProvider(this, "aws", {
region: "us-east-1",
});
// Create VPC with Typescript and CKDTF
const newVpc = new vpc.Vpc(this, "VPC", {
cidrBlock: "10.0.0.0/16",
tags: {
Name: "CDKtf-TypeScript-Demo-VPC",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Two different Private subnets for assigning to the private network
const privateSubnetA = new vpc.Subnet(this, "Private-Subnet-A", {
availabilityZone: "us-east-1a",
vpcId: newVpc.id,
mapPublicIpOnLaunch: false,
cidrBlock: "10.0.1.0/24",
tags: {
Name: "CDKtf-TypeScript-Demo-Private-Subnet-A",
Team: "DevOps",
Company: "Your Comapny",
},
});
const privateSubnetB = new vpc.Subnet(this, "Private-Subnet-B", {
availabilityZone: "us-east-1b",
vpcId: newVpc.id,
mapPublicIpOnLaunch: false,
cidrBlock: "10.0.2.0/24",
tags: {
Name: "CDKtf-TypeScript-Demo-Private-Subnet-B",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Two different Public subnets for assigning to the public network
const publicSubnetA = new vpc.Subnet(this, "Public-Subnet-A", {
availabilityZone: "us-east-1a",
vpcId: newVpc.id,
mapPublicIpOnLaunch: true,
cidrBlock: "10.0.6.0/24",
tags: {
Name: "CDKtf-TypeScript-Demo-Public-Subnet-A",
Team: "DevOps",
Company: "Your Comapny",
},
});
const publicSubnetB = new vpc.Subnet(this, "Public-Subnet-B", {
availabilityZone: "us-east-1b",
vpcId: newVpc.id,
mapPublicIpOnLaunch: true,
cidrBlock: "10.0.7.0/24",
tags: {
Name: "CDKtf-TypeScript-Demo-Public-Subnet-B",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Internet Gateway For communication VPC and Internet
const internetGatway = new vpc.InternetGateway(this, "Internet-Gateway", {
vpcId: newVpc.id,
tags: {
Name: "CDKtf-TypeScript-Demo-IG",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Two different Public IPs for assigning to the public network
const publicipA = new ec2.Eip(this, "eip-A", {
vpc: true,
tags: {
Name: "CDKtf-TypeScript-Demo-Public-eip-A",
Team: "DevOps",
Company: "Your Comapny",
},
});
const publicipB = new ec2.Eip(this, "eip-B", {
vpc: true,
tags: {
Name: "CDKtf-TypeScript-Demo-Public-eip-B",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Nat Gateway For communication Public and Private network
const natgatewayA = new vpc.NatGateway(this, "Nat-Gateway-A", {
allocationId: publicipA.id,
subnetId: publicSubnetA.id,
tags: {
Name: "CDKtf-TypeScript-Demo-Public-NG-A",
Team: "DevOps",
Company: "Your Comapny",
},
});
const natgatewayB = new vpc.NatGateway(this, "Nat-Gateway-B", {
allocationId: publicipB.id,
subnetId: publicSubnetB.id,
tags: {
Name: "CDKtf-TypeScript-Demo-Public-NG-B",
Team: "DevOps",
Company: "Your Comapny",
},
});
// Create Routing Table For communication Public network with Route and Association route
const publicroutetable = new vpc.RouteTable(this, "Public-Route-Table", {
vpcId: newVpc.id,
tags: {
Name: "CDKtf-TypeScript-Demo-Public-RT",
Team: "DevOps",
Company: "Your Comapny",
},
});
new vpc.Route(this, "Route", {
destinationCidrBlock: "0.0.0.0/0",
routeTableId: publicroutetable.id,
gatewayId: internetGatway.id,
});
new vpc.RouteTableAssociation(this, "Route-Table-Association-PUB-SUB-A", {
routeTableId: publicroutetable.id,
subnetId: publicSubnetA.id,
});
new vpc.RouteTableAssociation(this, "Route-Table-Association-PUB-SUB-B", {
routeTableId: publicroutetable.id,
subnetId: publicSubnetB.id,
});
// Create Routing Table For communication Private network with Route and Association route
const privateroutetableA = new vpc.RouteTable(this, "Private-Route-Table-A", {
vpcId: newVpc.id,
tags: {
Name: "CDKtf-TypeScript-Demo-Private-RT-A",
Team: "DevOps",
Company: "Your Comapny",
},
});
new vpc.Route(this, "Private-Route-A", {
destinationCidrBlock: "0.0.0.0/0",
routeTableId: privateroutetableA.id,
natGatewayId: natgatewayA.id,
});
new vpc.RouteTableAssociation(this, "Route-Table-Association-Private-SUB-A", {
routeTableId: privateroutetableA.id,
subnetId: privateSubnetA.id,
});
const privateroutetableB = new vpc.RouteTable(this, "Private-Route-Table-B", {
vpcId: newVpc.id,
tags: {
Name: "CDKtf-TypeScript-Demo-private-RT-B",
Team: "DevOps",
Company: "Your Comapny",
},
});
new vpc.Route(this, "Private-Route-B", {
destinationCidrBlock: "0.0.0.0/0",
routeTableId: privateroutetableB.id,
natGatewayId: natgatewayB.id,
});
new vpc.RouteTableAssociation(this, "Route-Table-Association-Private-SUB-B", {
routeTableId: privateroutetableB.id,
subnetId: privateSubnetB.id,
});
new TerraformOutput(this, "Vpc id", {
value: newVpc.id,
});
}
}
const app = new App();
new MyStack(app, "cdktf-typescript-aws-vpc");
app.synth();
Compile the TypeScript application
tsc
At this step you can run code with two different way:
Generate Terraform configuration
cdktf synth
The above command will create a folder called cdktf.out
that contains all Terraform JSON configuration that was generated.
Run Terraform commands
cd cdktf.out
terraform init
terraform plan
terraform apply
Run cdktf commands
cdktf deploy