I'm relatively new to CDK and Typescript. I'm attemption to create a custom Vpc. Ive run into an error if i try to deploy it. My error is as follows.
CREATE_FAILED | AWS::EC2::RouteTable | PrivateDatabaseSubnet/RouteTable (PrivateDatabaseSubnetRouteTableE95C2B02)
Resource handler returned message: "The vpc ID 'TemplateVpc' does not exist (Service: Ec2, Status Code: 400, Request ID: 6bd6e0fa-2b60-4834-852b-a24dec883062)" (RequestToken: eb4359e5-948a-3e2a-47d8-c27fc117b29c, HandlerErrorCode: InvalidRequest)
This is my code
import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import {Vpc, Subnet} from 'aws-cdk-lib/aws-ec2';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import {GatewayVpcEndpoint} from "aws-cdk-lib/aws-ec2";
export class CdkVpcStack extends Stack {
readonly vpc : Vpc;
readonly publicSubnet: Subnet;
readonly privateDatabaseSubnet: Subnet;
readonly privateApiSubnet: Subnet;
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpcName= "TempVpc";
this.vpc = new Vpc(this, 'TemplateVpc', {
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
vpcName: vpcName,
maxAzs: 2,
createInternetGateway: true,
natGateways: 1,
});
this.publicSubnet = new ec2.PublicSubnet(this, 'PublicSubnet', {
availabilityZone: 'eu-west-1a',
cidrBlock: '10.0.0.0/24',
vpcId: 'TemplateVpc',
mapPublicIpOnLaunch: true,
});
this.privateDatabaseSubnet = new ec2.PrivateSubnet(this, 'PrivateDatabaseSubnet', {
availabilityZone: 'eu-west-1b',
cidrBlock: '10.0.0.0/28',
vpcId: 'TemplateVpc',
});
this.privateApiSubnet = new ec2.PrivateSubnet(this, 'PrivateApiSubnet', {
availabilityZone: 'eu-west-1a',
cidrBlock: '10.0.0.0/24',
vpcId: 'TemplateVpc',
});
}}
Im hoping to create a Vpc where i have control over the subnets AZs and ID
VPC ID is something like this vpc-1234567890abcdef0
.
It will be generated by AWS automatically, when you create a VPC. Therefore, you shouldn't hardcode VPC ID.
You can use this.vpc.vpcId
to retrieve vpcId attribute.
Please try the following code
import { Stack, StackProps } from 'aws-cdk-lib';
import { Vpc, Subnet } from 'aws-cdk-lib/aws-ec2';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
export class CdkVpcStack extends Stack {
readonly vpc: Vpc;
readonly publicSubnet: Subnet;
readonly privateDatabaseSubnet: Subnet;
readonly privateApiSubnet: Subnet;
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpcName = "TempVpc";
this.vpc = new Vpc(this, 'TemplateVpc', {
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
vpcName: vpcName,
maxAzs: 2,
createInternetGateway: true,
natGateways: 1,
});
this.publicSubnet = new ec2.PublicSubnet(this, 'PublicSubnet', {
availabilityZone: 'eu-west-1a',
cidrBlock: '10.0.0.0/24',
vpcId: this.vpc.vpcId,
mapPublicIpOnLaunch: true,
});
this.privateDatabaseSubnet = new ec2.PrivateSubnet(this, 'PrivateDatabaseSubnet', {
availabilityZone: 'eu-west-1b',
cidrBlock: '10.0.0.0/28',
vpcId: this.vpc.vpcId,
});
this.privateApiSubnet = new ec2.PrivateSubnet(this, 'PrivateApiSubnet', {
availabilityZone: 'eu-west-1a',
cidrBlock: '10.0.0.0/24',
vpcId: this.vpc.vpcId,
});
}
}