CDK from the Command Line
Create a VPC with the CDK
Our goal is to create a vpc using the AWS CDK, make changes to the vpc, and provision the vpc from a client device.
Instructions
Create a directory named create-vpc and change to the directory.
mkdir create-vpc
cd create-vpc
Initialize the directory with a CDK project
cdk init --language typescript
Read the User Guide to learn more about the directory structure.
In a text editor open the file lib/create-vpc.ts.
At the top of the file below the import cdk statement add the following line:
import ec2 = require('@aws-cdk/aws-ec2');
Below the comment // // The code that defines your stack goes here add the following line:
new ec2.VpcNetwork(this, 'TheNewVPC');
To deploy the vpc the Typscript must transpile to javascript, then the javascript transpiles to CloudFormation Templates.
Save the file and open a console window and run the commands to transpile the typescript, show the CloudFormation Template to the screen, then deploy the template to the aws account.
npm run build
cdk synth
cdk deploy
Open the AWS console and look at the VPC resources created. Does everything look as you would expect?
Find the props for ec2.VpcNetwork so it will create one NAT Gateway,
AWS CDK Documentation. Update the VPC after making the changes.
new ec2.VpcNetwork(this, 'TheNewVpc', {
natGateways: 1
});
npm run build
cdk synth
cdk diff
cdk deploy