Skip to content

Commit

Permalink
fix(ecs): respect LogGroup's region for aws-log-driver (#18212)
Browse files Browse the repository at this point in the history
Respect passed log group `region` when constructing an instance of `AwsLogDriver`.
That change allows to implement cross region logging pattern for ECS containers.

fixes #17747

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kornicameister authored Jan 13, 2022
1 parent 06666f4 commit b6e3e51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as logs from '@aws-cdk/aws-logs';
import { Stack } from '@aws-cdk/core';
import { ContainerDefinition } from '../container-definition';
import { LogDriver, LogDriverConfig } from './log-driver';
import { removeEmpty } from './utils';
Expand Down Expand Up @@ -127,7 +126,7 @@ export class AwsLogDriver extends LogDriver {
options: removeEmpty({
'awslogs-group': this.logGroup.logGroupName,
'awslogs-stream-prefix': this.props.streamPrefix,
'awslogs-region': Stack.of(containerDefinition).region,
'awslogs-region': this.logGroup.env.region,
'awslogs-datetime-format': this.props.datetimeFormat,
'awslogs-multiline-pattern': this.props.multilinePattern,
'mode': this.props.mode,
Expand Down
34 changes: 34 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/aws-log-driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,38 @@ describe('aws log driver', () => {


});

test('allows cross-region log group', () => {
// GIVEN
const logGroupRegion = 'asghard';
const logGroup = logs.LogGroup.fromLogGroupArn(stack, 'LogGroup',
`arn:aws:logs:${logGroupRegion}:1234:log-group:my_log_group`);

// WHEN
td.addContainer('Container', {
image,
logging: new ecs.AwsLogDriver({
logGroup,
streamPrefix: 'hello',
}),
});

// THEN
expect(stack).toCountResources('AWS::Logs::LogGroup', 0);
expect(stack).toHaveResourceLike('AWS::ECS::TaskDefinition', {
ContainerDefinitions: [
{
LogConfiguration: {
LogDriver: 'awslogs',
Options: {
'awslogs-group': logGroup.logGroupName,
'awslogs-stream-prefix': 'hello',
'awslogs-region': logGroupRegion,
},
},
},
],
});
});

});

0 comments on commit b6e3e51

Please sign in to comment.