Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk-lib): Pass lookupRoleArn to NestedStackSynthesizer #26116

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ export class DefaultStackSynthesizer extends StackSynthesizer implements IReusab
return this.qualifier;
}

/**
* The role used to lookup for this stack
*/
public get lookupRole(): string | undefined {
return this.lookupRoleArn;
}

public bind(stack: Stack): void {
super.bind(stack);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class NestedStackSynthesizer extends StackSynthesizer {
return this.parentDeployment.bootstrapQualifier;
}

public get lookupRole(): string | undefined {
return this.parentDeployment.lookupRole;
}

public addFileAsset(asset: FileAssetSource): FileAssetLocation {
// Forward to parent deployment. By the magic of cross-stack references any parameter
// returned and used will magically be forwarded to the nested stack.
Expand All @@ -34,6 +38,6 @@ export class NestedStackSynthesizer extends StackSynthesizer {
public synthesize(session: ISynthesisSession): void {
// Synthesize the template, but don't emit as a cloud assembly artifact.
// It will be registered as an S3 asset of its parent instead.
this.synthesizeTemplate(session);
this.synthesizeTemplate(session, this.lookupRole);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export abstract class StackSynthesizer implements IStackSynthesizer {
return undefined;
}

/**
* The role used to lookup for this stack
*/
public get lookupRole(): string | undefined {
return undefined;
}

private _boundStack?: Stack;

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/core/lib/stack-synthesizers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export interface IStackSynthesizer {
*/
readonly bootstrapQualifier?: string;

/**
* The role used to lookup for this stack
*
* @default - no role
*/
readonly lookupRole?: string;

/**
* Bind to the stack this environment is going to be used on
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as cxschema from '../../../cloud-assembly-schema';
import { ArtifactType } from '../../../cloud-assembly-schema';
import * as cxapi from '../../../cx-api';
import { App, Aws, CfnResource, ContextProvider, DefaultStackSynthesizer, FileAssetPackaging, Stack } from '../../lib';
import { App, Aws, CfnResource, ContextProvider, DefaultStackSynthesizer, FileAssetPackaging, Stack, NestedStack } from '../../lib';
import { ISynthesisSession } from '../../lib/stack-synthesizers/types';
import { evaluateCFN } from '../evaluate-cfn';

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

beforeEach(() => {
app = new App({
Expand Down Expand Up @@ -187,6 +188,24 @@ describe('new style synthesis', () => {

});

test('nested Stack uses the lookup role ARN of the parent stack', () => {
// GIVEN
const myapp = new App();
const mystack = new Stack(myapp, 'mystack', {
synthesizer: new DefaultStackSynthesizer({
generateBootstrapVersionRule: false,
}),
env: {
account: '111111111111', region: 'us-east-1',
},
});
nestedStack = new NestedStack(mystack, 'nestedStack');

// THEN
expect(nestedStack.synthesizer.lookupRole).toEqual('arn:${AWS::Partition}:iam::111111111111:role/cdk-hnb659fds-lookup-role-111111111111-us-east-1');

});

test('add file asset', () => {
// WHEN
const location = stack.synthesizer.addFileAsset({
Expand Down