Skip to content

Commit

Permalink
Add logging to LoadBalancedFargateService (aws#1190)
Browse files Browse the repository at this point in the history
Create AWS log group and log drivers by default for L3 Fargate Service
  • Loading branch information
SoManyHs authored and rix0rrr committed Nov 19, 2018
1 parent f3a5f12 commit 52b7554
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 19 deletions.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ICluster } from './cluster';
import { IContainerImage } from './container-image';
import { FargateService } from './fargate/fargate-service';
import { FargateTaskDefinition } from './fargate/fargate-task-definition';
import { AwsLogDriver } from './log-drivers/aws-log-driver';

/**
* Properties for a LoadBalancedEcsService
Expand Down Expand Up @@ -101,6 +102,12 @@ export interface LoadBalancedFargateServiceProps {
* Setting this option will set the load balancer port to 443.
*/
certificate?: CertificateRef;
/**
* Whether to create an AWS log driver
*
* @default true
*/
createLogs?: boolean;
}

/**
Expand All @@ -121,8 +128,11 @@ export class LoadBalancedFargateService extends cdk.Construct {
cpu: props.cpu
});

const optIn = props.createLogs !== undefined ? props.createLogs : true;

const container = taskDefinition.addContainer('web', {
image: props.image,
logging: optIn ? this.createAWSLogDriver(this.id) : undefined
});

container.addPortMappings({
Expand Down Expand Up @@ -175,4 +185,8 @@ export class LoadBalancedFargateService extends cdk.Construct {
});
}
}

private createAWSLogDriver(prefix: string): AwsLogDriver {
return new AwsLogDriver(this, 'Logging', { streamPrefix: prefix });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand All @@ -78,7 +75,10 @@
"GatewayId": {
"Ref": "VpcIGWD7BA715C"
}
}
},
"DependsOn": [
"VpcVPCGWBF912B6E"
]
},
"VpcPublicSubnet1EIPD7E02669": {
"Type": "AWS::EC2::EIP",
Expand Down Expand Up @@ -158,9 +158,6 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand All @@ -169,7 +166,10 @@
"GatewayId": {
"Ref": "VpcIGWD7BA715C"
}
}
},
"DependsOn": [
"VpcVPCGWBF912B6E"
]
},
"VpcPublicSubnet2EIP3C605A87": {
"Type": "AWS::EC2::EIP",
Expand Down Expand Up @@ -760,4 +760,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
},
"VpcPublicSubnet1DefaultRoute3DA9E72A": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet1RouteTable6C95E38E"
Expand All @@ -78,7 +75,10 @@
"GatewayId": {
"Ref": "VpcIGWD7BA715C"
}
}
},
"DependsOn": [
"VpcVPCGWBF912B6E"
]
},
"VpcPublicSubnet1EIPD7E02669": {
"Type": "AWS::EC2::EIP",
Expand Down Expand Up @@ -158,9 +158,6 @@
},
"VpcPublicSubnet2DefaultRoute97F91067": {
"Type": "AWS::EC2::Route",
"DependsOn": [
"VpcVPCGWBF912B6E"
],
"Properties": {
"RouteTableId": {
"Ref": "VpcPublicSubnet2RouteTable94F7E489"
Expand All @@ -169,7 +166,10 @@
"GatewayId": {
"Ref": "VpcIGWD7BA715C"
}
}
},
"DependsOn": [
"VpcVPCGWBF912B6E"
]
},
"VpcPublicSubnet2EIP3C605A87": {
"Type": "AWS::EC2::EIP",
Expand Down Expand Up @@ -723,4 +723,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@
"Devices": [],
"Tmpfs": []
},
"LogConfiguration":{
"LogDriver":"awslogs",
"Options": {
"awslogs-group": {
"Ref": "FargateServiceLoggingLogGroup9B16742A"
},
"awslogs-stream-prefix": "FargateService",
"awslogs-region": {
"Ref": "AWS::Region"
}
}
},
"MountPoints": [],
"Name": "web",
"PortMappings": [
Expand Down Expand Up @@ -612,6 +624,13 @@
]
}
},
"FargateServiceLoggingLogGroup9B16742A": {
"Type": "AWS::Logs::LogGroup",
"Properties": {
"RetentionInDays": 365
},
"DeletionPolicy": "Retain"
},
"FargateServiceECC8084D": {
"Type": "AWS::ECS::Service",
"Properties": {
Expand Down
48 changes: 47 additions & 1 deletion packages/@aws-cdk/aws-ecs/test/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export = {
test.done();
},

'test Fargateloadbalanced construct'(test: Test) {
'test Fargate loadbalanced construct'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'VPC');
Expand All @@ -48,6 +48,20 @@ export = {

// THEN - stack contains a load balancer and a service
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));
expect(stack).to(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" },
"awslogs-stream-prefix": "Service",
"awslogs-region": { Ref: "AWS::Region" }
}
},
}
]
}));

expect(stack).to(haveResource("AWS::ECS::Service", {
DesiredCount: 2,
Expand All @@ -61,6 +75,38 @@ export = {
test.done();
},

'test Fargate loadbalanced construct opting out of log driver creation'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

// WHEN
new ecs.LoadBalancedFargateService(stack, 'Service', {
cluster,
image: ecs.ContainerImage.fromDockerHub('test'),
desiredCount: 2,
createLogs: false
});

// THEN - stack contains a load balancer and a service
expect(stack).notTo(haveResource('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
LogConfiguration: {
LogDriver: "awslogs",
Options: {
"awslogs-group": { Ref: "ServiceLoggingLogGroupC3D6A581" },
"awslogs-stream-prefix": "Service",
"awslogs-region": { Ref: "AWS::Region" }
}
},
}
]
}));

test.done();
},
'test Fargateloadbalanced construct with TLS'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 52b7554

Please sign in to comment.