Skip to content

Commit f5bc59b

Browse files
authored
fix(aws-ecs): set permissions for 'awslogs' log driver (#1291)
Make sure that tasks using the 'awslogs' Log Driver have the correct IAM permissions to actually write logs. Add grant() methods to IAM LogGroups to make this nicer to write. Fixes #1279.
1 parent 0919bf4 commit f5bc59b

File tree

10 files changed

+791
-2
lines changed

10 files changed

+791
-2
lines changed

packages/@aws-cdk/aws-ecs/lib/container-definition.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export class ContainerDefinition extends cdk.Construct {
229229
this.memoryLimitSpecified = props.memoryLimitMiB !== undefined || props.memoryReservationMiB !== undefined;
230230

231231
props.image.bind(this);
232+
if (props.logging) { props.logging.bind(this); }
232233
}
233234

234235
/**

packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logs = require('@aws-cdk/aws-logs');
22
import cdk = require('@aws-cdk/cdk');
3+
import { ContainerDefinition } from '../container-definition';
34
import { cloudformation } from '../ecs.generated';
45
import { LogDriver } from "./log-driver";
56

@@ -61,6 +62,13 @@ export class AwsLogDriver extends LogDriver {
6162
});
6263
}
6364

65+
/**
66+
* Called when the log driver is configured on a container
67+
*/
68+
public bind(containerDefinition: ContainerDefinition): void {
69+
this.logGroup.grantWrite(containerDefinition.taskDefinition.obtainExecutionRole());
70+
}
71+
6472
/**
6573
* Return the log driver CloudFormation JSON
6674
*/

packages/@aws-cdk/aws-ecs/lib/log-drivers/log-driver.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cdk = require('@aws-cdk/cdk');
2+
import { ContainerDefinition } from '../container-definition';
23
import { cloudformation } from '../ecs.generated';
34

45
/**
@@ -9,4 +10,9 @@ export abstract class LogDriver extends cdk.Construct {
910
* Return the log driver CloudFormation JSON
1011
*/
1112
public abstract renderLogDriver(): cloudformation.TaskDefinitionResource.LogConfigurationProperty;
13+
14+
/**
15+
* Called when the log driver is configured on a container
16+
*/
17+
public abstract bind(containerDefinition: ContainerDefinition): void;
1218
}

packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.expected.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,19 @@
759759
],
760760
"Effect": "Allow",
761761
"Resource": "*"
762+
},
763+
{
764+
"Action": [
765+
"logs:CreateLogStream",
766+
"logs:PutLogEvents"
767+
],
768+
"Effect": "Allow",
769+
"Resource": {
770+
"Fn::GetAtt": [
771+
"FargateServiceLoggingLogGroup9B16742A",
772+
"Arn"
773+
]
774+
}
762775
}
763776
],
764777
"Version": "2012-10-17"

0 commit comments

Comments
 (0)