@@ -10,6 +10,9 @@ import {
1010import {
1111 CfnSubnet ,
1212 FlowLogDestination ,
13+ FlowLogMaxAggregationInterval ,
14+ FlowLogOptions ,
15+ FlowLogTrafficType ,
1316 GatewayVpcEndpoint ,
1417 InterfaceVpcEndpoint ,
1518 InterfaceVpcEndpointAwsService ,
@@ -19,6 +22,7 @@ import {
1922 Vpc
2023} from "aws-cdk-lib/aws-ec2"
2124import { Role , ServicePrincipal } from "aws-cdk-lib/aws-iam"
25+ import { Bucket } from "aws-cdk-lib/aws-s3"
2226import { Key } from "aws-cdk-lib/aws-kms"
2327import { LogGroup } from "aws-cdk-lib/aws-logs"
2428import { AwsCustomResource , AwsCustomResourcePolicy , PhysicalResourceId } from "aws-cdk-lib/custom-resources"
@@ -43,6 +47,7 @@ export class VpcResourcesStack extends Stack {
4347 // Context
4448 /* context values passed as --context cli arguments are passed as strings so coerce them to expected types*/
4549 const logRetentionInDays : number = Number ( this . node . tryGetContext ( "logRetentionInDays" ) )
50+ const forwardCsocLogs : boolean = Boolean ( this . node . tryGetContext ( "forwardCsocLogs" ) )
4651
4752 // Imports
4853 const cloudwatchKmsKey = Key . fromKeyArn (
@@ -60,16 +65,34 @@ export class VpcResourcesStack extends Stack {
6065 removalPolicy : RemovalPolicy . DESTROY
6166 } )
6267
68+ // Build flow logs configuration
69+ const flowLogsConfig : Record < string , FlowLogOptions > = {
70+ "FlowLogCloudwatch" : {
71+ destination : FlowLogDestination . toCloudWatchLogs ( flowLogsLogGroup , flowLogsRole )
72+ }
73+ }
74+
75+ // Conditionally add S3 flow logs if forwardCsocLogs is true
76+ if ( forwardCsocLogs ) {
77+ const vpcFlowLogsBucket = Bucket . fromBucketArn (
78+ this ,
79+ "VpcFlowLogsBucket" ,
80+ "arn:aws:s3:::nhsd-audit-vpcflowlogs"
81+ )
82+
83+ flowLogsConfig [ "FlowLogS3" ] = {
84+ destination : FlowLogDestination . toS3 ( vpcFlowLogsBucket ) ,
85+ trafficType : FlowLogTrafficType . ALL ,
86+ maxAggregationInterval : FlowLogMaxAggregationInterval . TEN_MINUTES
87+ }
88+ }
89+
6390 const vpc = new Vpc ( this , "vpc" , {
6491 ipAddresses : IpAddresses . cidr ( "10.190.0.0/16" ) ,
6592 enableDnsSupport : true ,
6693 enableDnsHostnames : true ,
6794 availabilityZones : props . availabilityZones ,
68- flowLogs : {
69- "FlowLogCloudwatch" : {
70- destination : FlowLogDestination . toCloudWatchLogs ( flowLogsLogGroup , flowLogsRole )
71- }
72- }
95+ flowLogs : flowLogsConfig
7396 } )
7497
7598 // Add cfn-guard suppressions
0 commit comments