From 0c5836f1495846f9ed243bb00eb5ef7117494775 Mon Sep 17 00:00:00 2001 From: Yutaro Sakamoto Date: Fri, 4 Oct 2024 21:35:01 +0900 Subject: [PATCH] [Add]: create a VPC --- .../lib/constructs/Network/index.ts | 50 +++++++++++++++++++ infrastructure/lib/main.ts | 9 +--- 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 infrastructure/lib/constructs/Network/index.ts diff --git a/infrastructure/lib/constructs/Network/index.ts b/infrastructure/lib/constructs/Network/index.ts new file mode 100644 index 0000000..f1eb27e --- /dev/null +++ b/infrastructure/lib/constructs/Network/index.ts @@ -0,0 +1,50 @@ +import { Construct } from "constructs"; +import * as ec2 from "aws-cdk-lib/aws-ec2"; +import * as logs from "aws-cdk-lib/aws-logs"; +import * as iam from "aws-cdk-lib/aws-iam"; + +/** + * VPCとVPCエンドポイントに関するリソースを定義する + */ +export class Network extends Construct { + /** + * VPC + */ + public readonly vpc: ec2.Vpc; + + constructor(scope: Construct, id: string) { + super(scope, id); + + // VPCを作成 + this.vpc = new ec2.Vpc(this, "Vpc", { + natGateways: 0, + createInternetGateway: false, + maxAzs: 2, + subnetConfiguration: [ + { + cidrMask: 24, + name: "Private", + subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS, + }, + ], + }); + + // VPC Flow Logsを作成 + const vpcFlowLogGroup = new logs.LogGroup(this, "VpcFlowLogGroup", { + retention: logs.RetentionDays.THREE_DAYS, + }); + + const vpcFlowLogRole = new iam.Role(this, "VpcFlowLogGroupRole", { + assumedBy: new iam.ServicePrincipal("vpc-flow-logs.amazonaws.com"), + }); + + new ec2.FlowLog(this, "FlowLog", { + resourceType: ec2.FlowLogResourceType.fromVpc(this.vpc), + trafficType: ec2.FlowLogTrafficType.ALL, + destination: ec2.FlowLogDestination.toCloudWatchLogs( + vpcFlowLogGroup, + vpcFlowLogRole, + ), + }); + } +} diff --git a/infrastructure/lib/main.ts b/infrastructure/lib/main.ts index 28d2204..d58fd10 100644 --- a/infrastructure/lib/main.ts +++ b/infrastructure/lib/main.ts @@ -1,6 +1,6 @@ import * as cdk from "aws-cdk-lib"; import { Construct } from "constructs"; -// import * as sqs from 'aws-cdk-lib/aws-sqs'; +import { Network } from "./constructs/Network"; /** * スタック @@ -9,12 +9,7 @@ export class Cobol4JAwsWebStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); - // The code that defines your stack goes here - - // example resource - // const queue = new sqs.Queue(this, 'ImageBuilder4JQueue', { - // visibilityTimeout: cdk.Duration.seconds(300) - // }); + new Network(this, "Network"); } /**