Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-ec2): add support for custom logformat in vpc flow logs #16279

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc-flow-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ export interface FlowLogOptions {
* @default FlowLogDestinationType.toCloudWatchLogs()
*/
readonly destination?: FlowLogDestination;

/**
* The fields to include in the flow log record, in the order in which they should appear.
* See https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html#flow-log-records
*
* @default - No custom log format options provided.
*/
readonly logFormat?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the log format expects specific values, I think it makes sense to turn this into something that is better typed and can provide useful information to the user. Maybe something like

readonly customLogFormatFields?: LogFormatField[];

And then LogFormat could be something like

export class LogFormatField {
  /**
    * The source port of the traffic.
    */
  public static readonly SRC_PORT = new LogFormatField('${srcport'});

  public static custom(field: string): LogFormatField {
    return new LogFormatField(`\${${field}}`);
  }

  constructor(public readonly value: string) {}

}

new FlowLog(stack, 'FlowLogs', {
  customLogFormatFields: [
    LogFormatField.SRC_PORT,
    LogFormatField.custom('dstport'),
    ...
  ]
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good feedback, will work on it

}

/**
Expand Down Expand Up @@ -396,6 +404,9 @@ export class FlowLog extends FlowLogBase {
? props.trafficType
: FlowLogTrafficType.ALL,
logDestination,
logFormat: props.logFormat
? props.logFormat
: undefined,
});

this.flowLogId = flowLog.ref;
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc-flow-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ describe('vpc flow logs', () => {
);

});
test('with custom log format set, it successfully creates with cloudwatch log destination', () => {
const stack = getTestStack();

new FlowLog(stack, 'FlowLogs', {
resourceType: FlowLogResourceType.fromNetworkInterfaceId('eni-123455'),
logFormat: '${srcport} ${dstport}',
});

expect(stack).toHaveResource('AWS::EC2::FlowLog', {
ResourceType: 'NetworkInterface',
TrafficType: 'ALL',
ResourceId: 'eni-123455',
DeliverLogsPermissionArn: {
'Fn::GetAtt': ['FlowLogsIAMRoleF18F4209', 'Arn'],
},
LogFormat: '${srcport} ${dstport}',
LogGroupName: {
Ref: 'FlowLogsLogGroup9853A85F',
},
});

expect(stack).toCountResources('AWS::Logs::LogGroup', 1);
expect(stack).toCountResources('AWS::IAM::Role', 1);
expect(stack).not.toHaveResource('AWS::S3::Bucket');
});
});

function getTestStack(): Stack {
Expand Down