diff --git a/packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts b/packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts index 8d56646852d10..7a1238797e193 100644 --- a/packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts +++ b/packages/aws-cdk/lib/api/evaluate-cloudformation-template.ts @@ -35,7 +35,7 @@ export class LazyListStackResources implements ListStackResources { } export interface LookupExport { - lookupExport(name: string): Promise; + lookupExport(name: string): Promise; } export class LookupExportError extends Error { } @@ -45,7 +45,7 @@ export class LazyLookupExport implements LookupExport { constructor(private readonly sdk: ISDK) { } - async lookupExport(name: string): Promise { + async lookupExport(name: string): Promise { if (this.exports[name]) { return this.exports[name]; } @@ -63,7 +63,7 @@ export class LazyLookupExport implements LookupExport { } } - throw new LookupExportError(`CloudFormation Export cannot be resolved as it does not exist: ${name}`); + return undefined; // export not found } private async * listExports() { @@ -253,6 +253,9 @@ export class EvaluateCloudFormationTemplate { async 'Fn::ImportValue'(name: string): Promise { const exported = await self.lookupExport.lookupExport(name); + if (!exported) { + throw new CfnEvaluationException(`Export '${name}' could not be found for evaluation`); + } if (!exported.Value) { throw new CfnEvaluationException(`Export '${name}' exists without a value`); } diff --git a/packages/aws-cdk/test/api/evaluate-cloudformation-template.test.ts b/packages/aws-cdk/test/api/evaluate-cloudformation-template.test.ts index eeb022c219bac..576bab1853ca1 100644 --- a/packages/aws-cdk/test/api/evaluate-cloudformation-template.test.ts +++ b/packages/aws-cdk/test/api/evaluate-cloudformation-template.test.ts @@ -1,4 +1,5 @@ import { + CfnEvaluationException, EvaluateCloudFormationTemplate, LazyListStackResources, LazyLookupExport, @@ -106,8 +107,7 @@ describe('evaluateCfnExpression', () => { const evaluate = () => evaluateCfnTemplate.evaluateCfnExpression({ 'Fn::ImportValue': 'blah', }); - - await expect(evaluate).rejects.toBeInstanceOf(LookupExportError); + await expect(evaluate).rejects.toBeInstanceOf(CfnEvaluationException); }); }); }); diff --git a/packages/aws-cdk/test/api/lazy-lookup-export.test.ts b/packages/aws-cdk/test/api/lazy-lookup-export.test.ts index d24cbcc81d965..9c00a2f855599 100644 --- a/packages/aws-cdk/test/api/lazy-lookup-export.test.ts +++ b/packages/aws-cdk/test/api/lazy-lookup-export.test.ts @@ -49,8 +49,8 @@ describe('LazyLookupExport', () => { it('returns the matching export', async () => { const name = 'test-name-3'; const result = await lookup.lookupExport(name); - expect(result.Name).toEqual(name); - expect(result.Value).toEqual('test-value-3'); + expect(result?.Name).toEqual(name); + expect(result?.Value).toEqual('test-value-3'); }); it('stops fetching once export is found', async () => { @@ -76,8 +76,9 @@ describe('LazyLookupExport', () => { expect(listExports).toHaveBeenCalledTimes(1); }); - it('throws if the export does not exist', async () => { - await expect(() => lookup.lookupExport('test-name-unknown')).rejects.toBeInstanceOf(LookupExportError); + it('returns undefined if the export does not exist', async () => { + const result = await lookup.lookupExport('test-name-unknown'); + expect(result).toBeUndefined(); }); }); });