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

chore(core): use key instead of logicalId for key when provided #28075

Merged
merged 2 commits into from
Nov 20, 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
9 changes: 8 additions & 1 deletion packages/aws-cdk-lib/core/lib/cfn-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export interface CfnOutputProps {
*/
readonly description?: string;

/**
* The key of the property returned by aws cloudformation describe-stacks command.
*/
readonly key?: string;

/**
* The value of the property returned by the aws cloudformation describe-stacks command.
* The value of an output can include literals, parameter references, pseudo-parameters,
Expand Down Expand Up @@ -38,6 +43,7 @@ export interface CfnOutputProps {
export class CfnOutput extends CfnElement {
private _description?: string;
private _condition?: CfnCondition;
private _key?: string;
private _value?: any;
private _exportName?: string;

Expand All @@ -58,6 +64,7 @@ export class CfnOutput extends CfnElement {
}

this._description = props.description;
this._key = props.key;
this._value = props.value;
this._condition = props.condition;
this._exportName = props.exportName;
Expand Down Expand Up @@ -160,7 +167,7 @@ export class CfnOutput extends CfnElement {
public _toCloudFormation(): object {
return {
Outputs: {
[this.logicalId]: {
[this._key ?? this.logicalId]: {
Description: this._description,
Value: this._value,
Export: this._exportName != null ? { Name: this._exportName } : undefined,
Expand Down
26 changes: 25 additions & 1 deletion packages/aws-cdk-lib/core/test/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeEach(() => {
});

describe('output', () => {
test('outputs can be added to the stack', () => {
test('outputs can be added to the stack with no key provided', () => {
const res = new CfnResource(stack, 'MyResource', { type: 'R' });
const ref = res.ref;

Expand All @@ -32,6 +32,30 @@ describe('output', () => {
});
});

test('outputs can be added to the stack with a key provided', () => {
const res = new CfnResource(stack, 'MyResource', { type: 'R' });
const ref = res.ref;

new CfnOutput(stack, 'MyOutput', {
exportName: 'ExportName',
key: 'MyOutputWithKey',
value: ref,
description: 'CfnOutput properties',
});
expect(toCloudFormation(stack)).toEqual({
Resources: { MyResource: { Type: 'R' } },
Outputs:
{
MyOutputWithKey:
{
Description: 'CfnOutput properties',
Export: { Name: 'ExportName' },
Value: { Ref: 'MyResource' },
},
},
});
});

test('No export is created by default', () => {
// WHEN
new CfnOutput(stack, 'SomeOutput', { value: 'x' });
Expand Down
Loading