Skip to content

Commit be909bc

Browse files
fix(logs): respect region when importing log group (#18215)
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*
1 parent 26ee826 commit be909bc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/@aws-cdk/aws-logs/lib/log-group.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ export class LogGroup extends LogGroupBase {
355355
public readonly logGroupName = Stack.of(scope).splitArn(baseLogGroupArn, ArnFormat.COLON_RESOURCE_NAME).resourceName!;
356356
}
357357

358-
return new Import(scope, id);
358+
return new Import(scope, id, {
359+
environmentFromArn: baseLogGroupArn,
360+
});
359361
}
360362

361363
/**

packages/@aws-cdk/aws-logs/test/loggroup.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('log group', () => {
149149

150150
});
151151

152-
test('import from arn', () => {
152+
test('import from ARN, same region', () => {
153153
// GIVEN
154154
const stack2 = new Stack();
155155

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

167167
});
168168

169+
test('import from ARN, different region', () => {
170+
// GIVEN
171+
const stack = new Stack();
172+
const importRegion = 'asgard-1';
173+
174+
// WHEN
175+
const imported = LogGroup.fromLogGroupArn(stack, 'lg',
176+
`arn:aws:logs:${importRegion}:123456789012:log-group:my-log-group`);
177+
imported.addStream('MakeMeAStream');
178+
179+
// THEN
180+
expect(imported.logGroupName).toEqual('my-log-group');
181+
expect(imported.logGroupArn).toEqual(`arn:aws:logs:${importRegion}:123456789012:log-group:my-log-group:*`);
182+
expect(imported.env.region).not.toEqual(stack.region);
183+
expect(imported.env.region).toEqual(importRegion);
184+
185+
expect(stack).toHaveResource('AWS::Logs::LogStream', {
186+
LogGroupName: 'my-log-group',
187+
});
188+
expect(stack).toCountResources('AWS::Logs::LogGroup', 0);
189+
});
190+
169191
test('import from name', () => {
170192
// GIVEN
171193
const stack = new Stack();

0 commit comments

Comments
 (0)