After defining VpcStack in the lib directory, the next step is to declare and organize this Stack in the main entry point at bin/cdk.ts, compile and check the source code, and deploy the network infrastructure to AWS.
bin/cdk.tsNavigate to the bin directory and open the cdk.ts file in VS Code. Add the initialization declaration for VpcStack:
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkStack } from '../lib/cdk-stack';
import { VpcStack } from '../lib/vpc-stack';
const app = new cdk.App();
const env: cdk.Environment = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION ?? 'ap-southeast-1',
};
// Initialize ECR Stack
const ecrStack = new CdkStack(app, 'ProductsServiceEcrStack', { env });
// Initialize VPC Stack
const vpcStack = new VpcStack(app, 'ProductsServiceVpcStack', {
env,
});

Note: The vpcStack object will later be used to pass network attributes vpc: vpcStack.vpc into the ECS Fargate cluster and Internal Load Balancers in subsequent steps.
tsc) to check all syntax errors:npm run build

npx cdk list
Or:
cdk list

Output displayed:
ProductsServiceEcrStack
ProductsServiceVpcStack
npx cdk synth ProductsServiceVpcStack

cdk deploy)Open Terminal in VS Code at the IaC-ECS-CDK/cdk directory.
Run the command to deploy only the VPC Stack:
npx cdk deploy ProductsServiceVpcStack --require-approval never

The process of creating the VPC, allocating Elastic IP, and configuring the NAT Gateway usually takes about 2 to 3 minutes. The CDK CLI will update resource creation progress in real time on the terminal screen.
ap-southeast-1 is selected).
Go to the VPC service > select Your VPCs. You will see the VPC named ecs-cdk-vpc with default IPv4 CIDR range 10.0.0.0/16.


Go to the Subnets section in the left menu. The system has automatically created 4 subnets across 2 AZs:
ProductsServiceVpcStack/VpcLab/PublicSubnetSubnet1 and ...Subnet2.ProductsServiceVpcStack/VpcLab/PrivateSubnetSubnet1 and ...Subnet2.
Go to the NAT gateways section in the left menu. You will see 1 NAT Gateway in the Available state, allocated an Elastic IP, and placed in the Public Subnet.

Go to the Route tables section in the left menu to inspect routing configuration:
0.0.0.0/0 pointing to the Internet Gateway (igw-xxxxxx).0.0.0.0/0 pointing to the NAT Gateway (nat-xxxxxx), ensuring containers running in the Private Subnet have one-way outbound Internet access.
Congratulations! You have completed building and deploying a secure standard VPC network infrastructure with AWS CDK. This network system is ready to host the Amazon ECS Fargate cluster and database in the following lessons.