Everyrealm-AWS-CDK / lib /vpc-stack.ts
Jessedbrown's picture
create aws cdk
2d78d32
Raw History Blame Contribute Delete
1.65 kB
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
// TODO: Add a VPC stack that can be used by all other stacks
export class VpcStack extends cdk.Stack {
public readonly vpc: ec2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create VPC
this.vpc = new ec2.Vpc(this, 'BonusAppVpc', {
maxAzs: 2,
natGateways: 2,
subnetConfiguration: [
{
name: 'Public',
subnetType: ec2.SubnetType.PUBLIC,
cidrMask: 24,
},
{
name: 'Private',
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
cidrMask: 24,
},
],
});
// Let CloudFormation use the exact same resource names as before
// No need to override logical ID as we're using the exact same name 'BonusAppVpc'
// that will generate 'BonusAppVpcC5D8AE3E' in CloudFormation
// Add outputs
new cdk.CfnOutput(this, 'VpcId', {
value: this.vpc.vpcId,
description: 'The ID of the VPC',
exportName: `${this.stackName}-VpcId`,
});
new cdk.CfnOutput(this, 'PublicSubnets', {
value: this.vpc.publicSubnets.map(subnet => subnet.subnetId).join(','),
description: 'The IDs of the public subnets',
exportName: `${this.stackName}-PublicSubnets`,
});
new cdk.CfnOutput(this, 'PrivateSubnets', {
value: this.vpc.privateSubnets.map(subnet => subnet.subnetId).join(','),
description: 'The IDs of the private subnets',
exportName: `${this.stackName}-PrivateSubnets`,
});
}
}