Skip to content

contextprovider: CC API context provider should have the way to ensure the result has exact one value #257

@Tietew

Description

@Tietew

Describe the bug

While conversations in aws/aws-cdk#33619, implementing PrefixList.fromLookup(), I encountered issues how to handle unexpected result from CC API context provider with propertyMatch.

Other context provider returns one of the expected query result or a dummy value with provider error.

But CC API context provider returns an empty array [] when no resources match or an array with multiple elements when multiple resources match. It's not expected result for lookup method.

For example, when the result is [], cdk.context.json has the fixed result [] and never be removed. Vpc.fromLookup() removes the result from cdk.context.json when no VPC are found.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

CC API context provider should have the way to report ContextProviderError when the result does not have exact one resource.

Current Behavior

CC API context provider can return an empty or more than one entry.

Reproduction Steps

After synth the following stack,

export class CCApiTest extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const value = ContextProvider.getValue(this, {
      provider: cxschema.ContextProvider.CC_API_PROVIDER,
      props: {
        typeName: 'AWS::EC2::PrefixList',
        propertyMatch: {
          PrefixListName: 'not-exist-name',
        },
        propertiesToReturn: ['PrefixListId'],
      },
      dummyValue: [{ PrefixListId: 'pl-xxxxxxxx' }],
    });

    new CfnOutput(this, 'PrefixListQueryResult', {
      value: value.PrefixListId,
    });
  }
}

cdk.context.json contains

{
  "cc-api-provider:account=ACCOUNTID:propertiesToReturn.0=PrefixListId:propertyMatch.PrefixListName=not-exist-name:region=REGION:typeName=AWS$:$:EC2$:$:PrefixList": []
}

Possible Solution

In CcApiContextProviderPlugin.listResources(), throws ContextProviderError when the result does not have exact one resource, instead of returning as-is.

Additional Information/Context

No response

CDK CLI Version

2.1004.0

Framework Version

No response

Node.js Version

v22.14.0

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions