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

CDK aws_apigateway: cannot use imported RestApi with ApiKey because ApiKey references prop only populated on non-imported RestApi #22102

Closed
sempfa opened this issue Sep 18, 2022 · 5 comments · Fixed by #22368
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@sempfa
Copy link

sempfa commented Sep 18, 2022

Describe the bug

When using RestApi.fromRestApiId, deploymentStage property is not populated.

this effects other areas, such as creating an API Key to add to the RestApi, ie:

 const key = new apigw.ApiKey(this, 'tenant-key', {
      apiKeyName: `Tenant-Key`,
      resources: [api]
    });

(where api is the object created from .fromRestApiId)
returns error "Cannot read property 'stageName' of undefined"

Expected Behavior

deploymentStage property populated from RestApi.fromRestApiId

Current Behavior

deploymentStage undefined

Reproduction Steps

  1. use .fromRestApiId to reference existing API:
    const api = apigw.RestApi.fromRestApiId(this, "clientApi", 'xxxx123');

  2. create APIKey

 const key = new apigw.ApiKey(this, 'tenant-key', {
      apiKeyName: `Tenant-Key`,
      resources: [api]
    });

Possible Solution

  1. populate deploymentStage property

  2. implement .addApiKey method on object created from .fromRestApiId

Additional Information/Context

Api is created in another stack and is used for a much larger system... the app being created only needs to generate an API Key and a bucket and add them to param store.

CDK CLI Version

2.41.0 (build 6ad48a3)

Framework Version

No response

Node.js Version

v16.16.0

OS

Mac

Language

Typescript

Language Version

3.9.7

Other information

No response

@sempfa sempfa added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 18, 2022
@github-actions github-actions bot added the @aws-cdk/aws-s3 Related to Amazon S3 label Sep 18, 2022
@sempfa sempfa changed the title (module name): (short issue description) CDK aws_apigateway: fromRestApiId not populating deploymentStage Sep 19, 2022
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Sep 19, 2022
@peterwoodworth
Copy link
Contributor

CDK isn't making any calls to your AWS account unless an import method is specifically configured to do that (usually denoted with the name lookup being present in the method). See our docs here for more info

Usually, if a property on the interface (or proxy) returned by the import method is undefined, then there is a way to define that property through a parameter in fromXxxAttributes. However, that's not the case for RestApi.fromRestApiAttributes because the deploymentStage prop is of type Stage, meaning that if you were to define the deploymentStage when importing the RestApi, you would need to end up creating a new Stage which doesn't make sense. To support defining the stage on the imported RestApi, we would need to adjust the typing to IStage.

Alternatively, in ApiKey we could potentially handle this differently to not require the restApi passed in have the deploymentStage defined, though I'm not sure how we would accomplish this exactly

const stageName = restApi.deploymentStage!.stageName.toString();

@peterwoodworth peterwoodworth changed the title CDK aws_apigateway: fromRestApiId not populating deploymentStage CDK aws_apigateway: cannot use imported RestApi with ApiKey because ApiKey references prop only populated on non-imported RestApi Sep 19, 2022
@peterwoodworth peterwoodworth added p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Sep 19, 2022
@peterwoodworth
Copy link
Contributor

I am marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

Check out our contributing guide if you're interested in contributing yourself - there's a low chance the team will be able to address this soon but we'd be happy to review a PR 🙂

@sempfa
Copy link
Author

sempfa commented Sep 21, 2022

@peterwoodworth If this can't be solved soon, then what is your recommended approach for creating an api key for an existing api gw?

@peterwoodworth
Copy link
Contributor

@sempfa you can use the CfnApiKey resource, it's very similar to the ApiKey construct and will create the same resource.

Something like this:

    new CfnApiKey(this, 'ApiKey', {
      enabled: true,
      name: 'Tenant-key',
      stageKeys: [
        {
          restApiId: 'restApiId',
          stageName: 'StageName'
        }
      ]
    });

Alternatively you can stay on the L2 level. If you do this you would want to not pass in anything to the resources parameter, and instead use an escape hatch to modify the generated resource in the template. Here's what you'd want to do:

    const key = new ApiKey(this, 'ApiKey', {
      apiKeyName: 'Tenant-key'
    });
    const cfnKey = key.node.defaultChild as CfnApiKey;
    cfnKey.addPropertyOverride('StageKeys', [
      {
        RestApiId: 'restApiId',
        StageName: 'StageName'
      }
    ]);

@corymhall corymhall assigned corymhall and unassigned otaviomacedo Oct 5, 2022
@mergify mergify bot closed this as completed in #22368 Oct 11, 2022
mergify bot pushed a commit that referenced this issue Oct 11, 2022
…2368)

The current implementation of `ApiKey` is a little confusing. It takes a `resources` property which expects a list of `IRestApi`s. An ApiKey is not associated with a RestApi by itself though, it needs to be associated with a specific `Stage` on the RestApi. When you import a RestApi you cannot import it with a `Stage` and even if you could provide an `IStage` I don't think that makes the most sense (you can have multiple stages).

Instead, this PR deprecates the `resources` prop and adds a new `stages` prop which expects a list of `IStage`s. To facilitate this I have also done a couple of other things

- Added the ability to import a Stage using `Stage.fromStageAttributes()`.
- Added an `addApiKey` method to `IStage`.
- Added additional error handling if the user does try to create an ApiKey with an imported RestApi.

fixes #22102


----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants