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

chore(ec2): support new vpc flow log fields in v7 #30202

Merged
merged 8 commits into from
Jul 30, 2024
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@
"TrafficType": "ALL"
}
},
"ECSCluster7D463CD4": {
"Type": "AWS::ECS::Cluster"
},
"FlowLogsAllFormatCWIAMRoleAF92546B": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -618,7 +621,7 @@
]
},
"LogDestinationType": "cloud-watch-logs",
"LogFormat": "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status} ${vpc-id} ${subnet-id} ${instance-id} ${tcp-flags} ${type} ${pkt-srcaddr} ${pkt-dstaddr} ${region} ${az-id} ${sublocation-type} ${sublocation-id} ${pkt-src-aws-service} ${pkt-dst-aws-service} ${flow-direction} ${traffic-path}",
"LogFormat": "${version} ${account-id} ${interface-id} ${srcaddr} ${dstaddr} ${srcport} ${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status} ${vpc-id} ${subnet-id} ${instance-id} ${tcp-flags} ${type} ${pkt-srcaddr} ${pkt-dstaddr} ${region} ${az-id} ${sublocation-type} ${sublocation-id} ${pkt-src-aws-service} ${pkt-dst-aws-service} ${flow-direction} ${traffic-path} ${ecs-cluster-arn} ${ecs-cluster-name} ${ecs-container-instance-arn} ${ecs-container-instance-id} ${ecs-container-id} ${ecs-second-container-id} ${ecs-service-name} ${ecs-task-definition-arn} ${ecs-task-arn} ${ecs-task-id}",
"LogGroupName": {
"Ref": "FlowLogsAllFormatCWLogGroup3DAB6837"
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Bucket } from 'aws-cdk-lib/aws-s3';
import { Cluster } from 'aws-cdk-lib/aws-ecs';
import { App, Stack, StackProps, RemovalPolicy } from 'aws-cdk-lib';
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { FlowLog, FlowLogDestination, FlowLogResourceType, Vpc, LogFormat } from 'aws-cdk-lib/aws-ec2';
Expand All @@ -19,6 +20,9 @@ class TestStack extends Stack {
LogFormat.SRC_PORT,
],
});

new Cluster(this, 'ECSCluster', { vpc });

new FlowLog(this, 'FlowLogsAllFormatCW', {
resourceType: FlowLogResourceType.fromVpc(vpc),
logFormat: [
Expand Down Expand Up @@ -51,6 +55,16 @@ class TestStack extends Stack {
LogFormat.PKT_DST_AWS_SERVICE,
LogFormat.FLOW_DIRECTION,
LogFormat.TRAFFIC_PATH,
LogFormat.ECS_CLUSTER_ARN,
LogFormat.ECS_CLUSTER_NAME,
LogFormat.ECS_CONTAINER_INSTANCE_ARN,
LogFormat.ECS_CONTAINER_INSTANCE_ID,
LogFormat.ECS_CONTAINER_ID,
LogFormat.ECS_SECOND_CONTAINER_ID,
LogFormat.ECS_SERVICE_NAME,
LogFormat.ECS_TASK_DEFINITION_ARN,
LogFormat.ECS_TASK_ARN,
LogFormat.ECS_TASK_ID,
],
});

Expand Down
52 changes: 52 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/lib/vpc-flow-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,58 @@ export class LogFormat {
*/
public static readonly TRAFFIC_PATH = LogFormat.field('traffic-path');

/**
* AWS Resource Name (ARN) of the ECS cluster if the traffic is from a running ECS task.
*/
public static readonly ECS_CLUSTER_ARN = LogFormat.field('ecs-cluster-arn');

/**
* Name of the ECS cluster if the traffic is from a running ECS task.
*/
public static readonly ECS_CLUSTER_NAME = LogFormat.field('ecs-cluster-name');

/**
* ARN of the ECS container instance if the traffic is from a running ECS task on an EC2 instance.
*/
public static readonly ECS_CONTAINER_INSTANCE_ARN = LogFormat.field('ecs-container-instance-arn');

/**
* ID of the ECS container instance if the traffic is from a running ECS task on an EC2 instance.
*/
public static readonly ECS_CONTAINER_INSTANCE_ID = LogFormat.field('ecs-container-instance-id');

/**
* Docker runtime ID of the container if the traffic is from a running ECS task.
* If there is one container or more in the ECS task, this will be the docker runtime ID of the first container.
*/
public static readonly ECS_CONTAINER_ID = LogFormat.field('ecs-container-id');

/**
* Docker runtime ID of the container if the traffic is from a running ECS task.
* If there is more than one container in the ECS task, this will be the Docker runtime ID of the second container.
*/
public static readonly ECS_SECOND_CONTAINER_ID = LogFormat.field('ecs-second-container-id');

/**
* Name of the ECS service if the traffic is from a running ECS task and the ECS task is started by an ECS service.
*/
public static readonly ECS_SERVICE_NAME = LogFormat.field('ecs-service-name');

/**
* ARN of the ECS task definition if the traffic is from a running ECS task.
*/
public static readonly ECS_TASK_DEFINITION_ARN = LogFormat.field('ecs-task-definition-arn');

/**
* ARN of the ECS task if the traffic is from a running ECS task.
*/
public static readonly ECS_TASK_ARN = LogFormat.field('ecs-task-arn');

/**
* ID of the ECS task if the traffic is from a running ECS task.
*/
public static readonly ECS_TASK_ID = LogFormat.field('ecs-task-id');

/**
* The default format.
*/
Expand Down
15 changes: 14 additions & 1 deletion packages/aws-cdk-lib/aws-ec2/test/vpc-flow-logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,16 @@ test('log format for built-in types is correct', () => {
LogFormat.PKT_DST_AWS_SERVICE,
LogFormat.FLOW_DIRECTION,
LogFormat.TRAFFIC_PATH,
LogFormat.ECS_CLUSTER_ARN,
LogFormat.ECS_CLUSTER_NAME,
LogFormat.ECS_CONTAINER_INSTANCE_ARN,
LogFormat.ECS_CONTAINER_INSTANCE_ID,
LogFormat.ECS_CONTAINER_ID,
LogFormat.ECS_SECOND_CONTAINER_ID,
LogFormat.ECS_SERVICE_NAME,
LogFormat.ECS_TASK_DEFINITION_ARN,
LogFormat.ECS_TASK_ARN,
LogFormat.ECS_TASK_ID,
],
});

Expand All @@ -722,7 +732,10 @@ test('log format for built-in types is correct', () => {
+ '${dstport} ${protocol} ${packets} ${bytes} ${start} ${end} ${action} ${log-status} '
+ '${vpc-id} ${subnet-id} ${instance-id} ${tcp-flags} ${type} ${pkt-srcaddr} '
+ '${pkt-dstaddr} ${region} ${az-id} ${sublocation-type} ${sublocation-id} '
+ '${pkt-src-aws-service} ${pkt-dst-aws-service} ${flow-direction} ${traffic-path}'),
+ '${pkt-src-aws-service} ${pkt-dst-aws-service} ${flow-direction} ${traffic-path} '
+ '${ecs-cluster-arn} ${ecs-cluster-name} ${ecs-container-instance-arn} ${ecs-container-instance-id} '
+ '${ecs-container-id} ${ecs-second-container-id} ${ecs-service-name} ${ecs-task-definition-arn} '
+ '${ecs-task-arn} ${ecs-task-id}'),
});
});

Expand Down