Skip to content

Commit 3c29223

Browse files
fix(cdk-lib): Pass lookupRoleArn to NestedStackSynthesizer (#26116)
NestedStack's synthesizer doesn't receive the lookupRoleArn from the parent stack synthesizer, so the NestedStack tries with local credentials (of the deployment account) instead of assuming a cross-account role (on the target account) as regular non-nested Stack would do. This PR aims to add lookupRoleArn reference to the StackSynthesizer class and IStackSynthesizer, so it can be use on the NestedStack to explicitly set an IAM role in case of parent stack having one already defined, so CDK uses the role instead of local credentials. Closes #25171. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f9d4573 commit 3c29223

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

packages/aws-cdk-lib/core/lib/stack-synthesizers/default-synthesizer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ export class DefaultStackSynthesizer extends StackSynthesizer implements IReusab
344344
return this.qualifier;
345345
}
346346

347+
/**
348+
* The role used to lookup for this stack
349+
*/
350+
public get lookupRole(): string | undefined {
351+
return this.lookupRoleArn;
352+
}
353+
347354
public bind(stack: Stack): void {
348355
super.bind(stack);
349356

packages/aws-cdk-lib/core/lib/stack-synthesizers/nested.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class NestedStackSynthesizer extends StackSynthesizer {
1919
return this.parentDeployment.bootstrapQualifier;
2020
}
2121

22+
public get lookupRole(): string | undefined {
23+
return this.parentDeployment.lookupRole;
24+
}
25+
2226
public addFileAsset(asset: FileAssetSource): FileAssetLocation {
2327
// Forward to parent deployment. By the magic of cross-stack references any parameter
2428
// returned and used will magically be forwarded to the nested stack.
@@ -34,6 +38,6 @@ export class NestedStackSynthesizer extends StackSynthesizer {
3438
public synthesize(session: ISynthesisSession): void {
3539
// Synthesize the template, but don't emit as a cloud assembly artifact.
3640
// It will be registered as an S3 asset of its parent instead.
37-
this.synthesizeTemplate(session);
41+
this.synthesizeTemplate(session, this.lookupRole);
3842
}
3943
}

packages/aws-cdk-lib/core/lib/stack-synthesizers/stack-synthesizer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export abstract class StackSynthesizer implements IStackSynthesizer {
2828
return undefined;
2929
}
3030

31+
/**
32+
* The role used to lookup for this stack
33+
*/
34+
public get lookupRole(): string | undefined {
35+
return undefined;
36+
}
37+
3138
private _boundStack?: Stack;
3239

3340
/**

packages/aws-cdk-lib/core/lib/stack-synthesizers/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export interface IStackSynthesizer {
1313
*/
1414
readonly bootstrapQualifier?: string;
1515

16+
/**
17+
* The role used to lookup for this stack
18+
*
19+
* @default - no role
20+
*/
21+
readonly lookupRole?: string;
22+
1623
/**
1724
* Bind to the stack this environment is going to be used on
1825
*

packages/aws-cdk-lib/core/test/stack-synthesis/new-style-synthesis.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs';
22
import * as cxschema from '../../../cloud-assembly-schema';
33
import { ArtifactType } from '../../../cloud-assembly-schema';
44
import * as cxapi from '../../../cx-api';
5-
import { App, Aws, CfnResource, ContextProvider, DefaultStackSynthesizer, FileAssetPackaging, Stack } from '../../lib';
5+
import { App, Aws, CfnResource, ContextProvider, DefaultStackSynthesizer, FileAssetPackaging, Stack, NestedStack } from '../../lib';
66
import { ISynthesisSession } from '../../lib/stack-synthesizers/types';
77
import { evaluateCFN } from '../evaluate-cfn';
88

@@ -15,6 +15,7 @@ const CFN_CONTEXT = {
1515
describe('new style synthesis', () => {
1616
let app: App;
1717
let stack: Stack;
18+
let nestedStack: NestedStack;
1819

1920
beforeEach(() => {
2021
app = new App({
@@ -187,6 +188,24 @@ describe('new style synthesis', () => {
187188

188189
});
189190

191+
test('nested Stack uses the lookup role ARN of the parent stack', () => {
192+
// GIVEN
193+
const myapp = new App();
194+
const mystack = new Stack(myapp, 'mystack', {
195+
synthesizer: new DefaultStackSynthesizer({
196+
generateBootstrapVersionRule: false,
197+
}),
198+
env: {
199+
account: '111111111111', region: 'us-east-1',
200+
},
201+
});
202+
nestedStack = new NestedStack(mystack, 'nestedStack');
203+
204+
// THEN
205+
expect(nestedStack.synthesizer.lookupRole).toEqual('arn:${AWS::Partition}:iam::111111111111:role/cdk-hnb659fds-lookup-role-111111111111-us-east-1');
206+
207+
});
208+
190209
test('add file asset', () => {
191210
// WHEN
192211
const location = stack.synthesizer.addFileAsset({

0 commit comments

Comments
 (0)