import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { CfnOutput } from 'aws-cdk-lib';
import { IpAddresses } from 'aws-cdk-lib/aws-ec2';
export class TestCdkStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'test-vpc', {
maxAzs: 2,
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
subnetConfiguration: [],
});
const publicSubnet1 = new ec2.Subnet(this, 'MyPublicSubnet1', {
vpcId: vpc.vpcId,
cidrBlock: '10.0.0.0/24',
availabilityZone: 'ap-northeast-1a',
mapPublicIpOnLaunch: true,
});
const publicSubnet2 = new ec2.Subnet(this, 'MyPublicSubnet2', {
vpcId: vpc.vpcId,
cidrBlock: '10.0.1.0/24',
mapPublicIpOnLaunch: true,
availabilityZone: 'ap-northeast-1c',
});
const privateSubnet1 = new ec2.Subnet(this, 'MyPrivateSubnet1' , {
vpcId: vpc.vpcId,
cidrBlock: '10.0.10.0/24',
availabilityZone: 'ap-northeast-1a',
mapPublicIpOnLaunch: false,
});
const privateSubnet2 = new ec2.Subnet(this, 'MyPrivateSubnet2', {
vpcId: vpc.vpcId,
cidrBlock: '10.0.11.0/24',
availabilityZone: 'ap-northeast-1c',
mapPublicIpOnLaunch: false,
});
const igw = new ec2.CfnInternetGateway(this, 'DefaultInternetGateway');
new ec2.CfnVPCGatewayAttachment(this, 'MyVPCGatewayAttachment', {
vpcId: vpc.vpcId,
internetGatewayId: igw.ref,
});
const routeTable = new ec2.CfnRouteTable(this, 'MyRouteTable', {
vpcId: vpc.vpcId,
});
new ec2.CfnRoute(this, 'MyRoute', {
routeTableId: routeTable.ref,
gatewayId: igw.ref,
destinationCidrBlock: '0.0.0.0/0',
});
[publicSubnet1, publicSubnet2].forEach((subnet, index) => {
new ec2.CfnSubnetRouteTableAssociation(this, `MySubnetRouteTableAssociation${index}`, {
subnetId: subnet.subnetId,
routeTableId: routeTable.ref,
});
});
}
}