AWS CDK如何将现有的使用计划添加到api阶段



CDK文档定义我可以通过名为fromUsagePlanId的静态函数导入外部使用计划,但这返回Interface IUsagePlan,但该接口没有addApiStage方法来附加我的Api及其阶段。

my snippet code:

import * as apigateway from 'aws-cdk-lib/aws-apigateway';

export class CdkApiGwTemplateStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const api = new apigateway.RestApi(this,`${domain.toLowerCase()}-${subDomain.toLowerCase()}`,
{
restApiName: `${domain.toLowerCase()}-${subDomain.toLowerCase()}`,
description: apiDescription,
binaryMediaTypes: binaryMediaTypes,
deployOptions: {
accessLogDestination: new LogGroupLogDestination(logGroup),
loggingLevel:
cloudwatchLoggingLevel.toUpperCase() as MethodLoggingLevel,
stageName: environment.toLowerCase(),
variables: variables,
},
}
);
const key = api.addApiKey('ApiKey', {
apiKeyName: apikeyName,
description: apiKeyDescription,
});
const plan = apigateway.UsagePlan.fromUsagePlanId(this, 'getExternalUsagePlan', usagePlanId);
plan.addApiKey(key);

我试着搜索cfn一级的云形成来做到这一点,但我找不到。我如何使用UsagePlan的方法addApiStage与接口IUsagePlan或我如何添加mi api到现有的使用计划的一些想法?

你的问题已经有一个很好的提示-IUsagePlanUsagePlan是不同的类型。我认为不可能将现有的使用计划添加/pair/bind到新的CDKRestApi

因此,一个解决方法是创建一个新的UsagePlan,并在新计划中添加一个API Stage:
// There's no obvious way to bind an existing (manually created) usage plan to a RestApi
// as this is of type IUsagePlan, not UsagePlan, so instead of that ...
// const plan = apigateway.UsagePlan.fromUsagePlanId(this, 'getExternalUsagePlan', usagePlanId);
// Create a new UsagePlan
const plan = api.addUsagePlan('HelloUsagePlan', {
name: 'Hello Usage Plan',
});
// If deploy is disabled [enabled by default], you will need to explicitly assign 
// this [deploymentStage] value in order to set up integrations.
plan.addApiStage({ api: api, stage: api.deploymentStage });

相关内容

  • 没有找到相关文章

最新更新