Skip to content

Commit

Permalink
fix(logs): respect region when importing log group (#18215)
Browse files Browse the repository at this point in the history
When log group is imported it is crucial to preserve an information
about region where log group exists. Without that information it is not possible
to implement cross region logging.

fixes #18214

----

*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 committed Jan 4, 2022
1 parent 26ee826 commit be909bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-logs/lib/log-group.ts
Expand Up @@ -355,7 +355,9 @@ export class LogGroup extends LogGroupBase {
public readonly logGroupName = Stack.of(scope).splitArn(baseLogGroupArn, ArnFormat.COLON_RESOURCE_NAME).resourceName!;
}

return new Import(scope, id);
return new Import(scope, id, {
environmentFromArn: baseLogGroupArn,
});
}

/**
Expand Down
24 changes: 23 additions & 1 deletion packages/@aws-cdk/aws-logs/test/loggroup.test.ts
Expand Up @@ -149,7 +149,7 @@ describe('log group', () => {

});

test('import from arn', () => {
test('import from ARN, same region', () => {
// GIVEN
const stack2 = new Stack();

Expand All @@ -166,6 +166,28 @@ describe('log group', () => {

});

test('import from ARN, different region', () => {
// GIVEN
const stack = new Stack();
const importRegion = 'asgard-1';

// WHEN
const imported = LogGroup.fromLogGroupArn(stack, 'lg',
`arn:aws:logs:${importRegion}:123456789012:log-group:my-log-group`);
imported.addStream('MakeMeAStream');

// THEN
expect(imported.logGroupName).toEqual('my-log-group');
expect(imported.logGroupArn).toEqual(`arn:aws:logs:${importRegion}:123456789012:log-group:my-log-group:*`);
expect(imported.env.region).not.toEqual(stack.region);
expect(imported.env.region).toEqual(importRegion);

expect(stack).toHaveResource('AWS::Logs::LogStream', {
LogGroupName: 'my-log-group',
});
expect(stack).toCountResources('AWS::Logs::LogGroup', 0);
});

test('import from name', () => {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit be909bc

Please sign in to comment.