Skip to content

Commit a0bb8e5

Browse files
authored
chore(ec2): support new vpc flow log fields in v7 (#30202)
### Reason for this change VPC Flow log added several fields regarding ECS in v7. https://aws.amazon.com/about-aws/whats-new/2024/05/amazon-vpc-flow-logs-extends-support-ecs/ https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html#flow-log-records This change supports these fields in L2 construct. ### Description of changes Added new log fields to `LogFormat` class. ### Description of how you validated changes Unit test and integ test are both updated. Changes of logFormat configuration requires resource replacement, which is necessary. It seems like that in order to enable these ecs related log fields, at least one ECS cluster is required in the VPC. So a new ECS cluster is also created in the integ test. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8b4685e commit a0bb8e5

File tree

7 files changed

+120
-7
lines changed

7 files changed

+120
-7
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-flow-logs-customformat.js.snapshot/FlowLogsTestStack.assets.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-flow-logs-customformat.js.snapshot/FlowLogsTestStack.template.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@
531531
"TrafficType": "ALL"
532532
}
533533
},
534+
"ECSCluster7D463CD4": {
535+
"Type": "AWS::ECS::Cluster"
536+
},
534537
"FlowLogsAllFormatCWIAMRoleAF92546B": {
535538
"Type": "AWS::IAM::Role",
536539
"Properties": {
@@ -618,7 +621,7 @@
618621
]
619622
},
620623
"LogDestinationType": "cloud-watch-logs",
621-
"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}",
624+
"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}",
622625
"LogGroupName": {
623626
"Ref": "FlowLogsAllFormatCWLogGroup3DAB6837"
624627
},

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-flow-logs-customformat.js.snapshot/manifest.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-flow-logs-customformat.js.snapshot/tree.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.vpc-flow-logs-customformat.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Bucket } from 'aws-cdk-lib/aws-s3';
2+
import { Cluster } from 'aws-cdk-lib/aws-ecs';
23
import { App, Stack, StackProps, RemovalPolicy } from 'aws-cdk-lib';
34
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
45
import { FlowLog, FlowLogDestination, FlowLogResourceType, Vpc, LogFormat } from 'aws-cdk-lib/aws-ec2';
@@ -19,6 +20,9 @@ class TestStack extends Stack {
1920
LogFormat.SRC_PORT,
2021
],
2122
});
23+
24+
new Cluster(this, 'ECSCluster', { vpc });
25+
2226
new FlowLog(this, 'FlowLogsAllFormatCW', {
2327
resourceType: FlowLogResourceType.fromVpc(vpc),
2428
logFormat: [
@@ -51,6 +55,16 @@ class TestStack extends Stack {
5155
LogFormat.PKT_DST_AWS_SERVICE,
5256
LogFormat.FLOW_DIRECTION,
5357
LogFormat.TRAFFIC_PATH,
58+
LogFormat.ECS_CLUSTER_ARN,
59+
LogFormat.ECS_CLUSTER_NAME,
60+
LogFormat.ECS_CONTAINER_INSTANCE_ARN,
61+
LogFormat.ECS_CONTAINER_INSTANCE_ID,
62+
LogFormat.ECS_CONTAINER_ID,
63+
LogFormat.ECS_SECOND_CONTAINER_ID,
64+
LogFormat.ECS_SERVICE_NAME,
65+
LogFormat.ECS_TASK_DEFINITION_ARN,
66+
LogFormat.ECS_TASK_ARN,
67+
LogFormat.ECS_TASK_ID,
5468
],
5569
});
5670

packages/aws-cdk-lib/aws-ec2/lib/vpc-flow-logs.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,58 @@ export class LogFormat {
646646
*/
647647
public static readonly TRAFFIC_PATH = LogFormat.field('traffic-path');
648648

649+
/**
650+
* AWS Resource Name (ARN) of the ECS cluster if the traffic is from a running ECS task.
651+
*/
652+
public static readonly ECS_CLUSTER_ARN = LogFormat.field('ecs-cluster-arn');
653+
654+
/**
655+
* Name of the ECS cluster if the traffic is from a running ECS task.
656+
*/
657+
public static readonly ECS_CLUSTER_NAME = LogFormat.field('ecs-cluster-name');
658+
659+
/**
660+
* ARN of the ECS container instance if the traffic is from a running ECS task on an EC2 instance.
661+
*/
662+
public static readonly ECS_CONTAINER_INSTANCE_ARN = LogFormat.field('ecs-container-instance-arn');
663+
664+
/**
665+
* ID of the ECS container instance if the traffic is from a running ECS task on an EC2 instance.
666+
*/
667+
public static readonly ECS_CONTAINER_INSTANCE_ID = LogFormat.field('ecs-container-instance-id');
668+
669+
/**
670+
* Docker runtime ID of the container if the traffic is from a running ECS task.
671+
* If there is one container or more in the ECS task, this will be the docker runtime ID of the first container.
672+
*/
673+
public static readonly ECS_CONTAINER_ID = LogFormat.field('ecs-container-id');
674+
675+
/**
676+
* Docker runtime ID of the container if the traffic is from a running ECS task.
677+
* If there is more than one container in the ECS task, this will be the Docker runtime ID of the second container.
678+
*/
679+
public static readonly ECS_SECOND_CONTAINER_ID = LogFormat.field('ecs-second-container-id');
680+
681+
/**
682+
* Name of the ECS service if the traffic is from a running ECS task and the ECS task is started by an ECS service.
683+
*/
684+
public static readonly ECS_SERVICE_NAME = LogFormat.field('ecs-service-name');
685+
686+
/**
687+
* ARN of the ECS task definition if the traffic is from a running ECS task.
688+
*/
689+
public static readonly ECS_TASK_DEFINITION_ARN = LogFormat.field('ecs-task-definition-arn');
690+
691+
/**
692+
* ARN of the ECS task if the traffic is from a running ECS task.
693+
*/
694+
public static readonly ECS_TASK_ARN = LogFormat.field('ecs-task-arn');
695+
696+
/**
697+
* ID of the ECS task if the traffic is from a running ECS task.
698+
*/
699+
public static readonly ECS_TASK_ID = LogFormat.field('ecs-task-id');
700+
649701
/**
650702
* The default format.
651703
*/

packages/aws-cdk-lib/aws-ec2/test/vpc-flow-logs.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,16 @@ test('log format for built-in types is correct', () => {
712712
LogFormat.PKT_DST_AWS_SERVICE,
713713
LogFormat.FLOW_DIRECTION,
714714
LogFormat.TRAFFIC_PATH,
715+
LogFormat.ECS_CLUSTER_ARN,
716+
LogFormat.ECS_CLUSTER_NAME,
717+
LogFormat.ECS_CONTAINER_INSTANCE_ARN,
718+
LogFormat.ECS_CONTAINER_INSTANCE_ID,
719+
LogFormat.ECS_CONTAINER_ID,
720+
LogFormat.ECS_SECOND_CONTAINER_ID,
721+
LogFormat.ECS_SERVICE_NAME,
722+
LogFormat.ECS_TASK_DEFINITION_ARN,
723+
LogFormat.ECS_TASK_ARN,
724+
LogFormat.ECS_TASK_ID,
715725
],
716726
});
717727

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

0 commit comments

Comments
 (0)