fromUsagePlanId
的静态函数导入外部使用计划,但这会返回 Interface IUsagePlan
但哪个接口没有方法 addApiStage
来附加我的 Api 及其阶段。
我的代码片段:
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);
我尝试搜索 cloudformation 的 cfn level 1 来执行此操作,但我找不到。如何将构造函数
addApiStage
的方法 UsagePlan
与接口 IUsagePlan
一起使用,或者如何将 mi api 添加到现有的使用计划中?
你的问题已经有一个很好的提示 -
IUsagePlan
和 UsagePlan
是不同的类型。我认为不可能将现有使用计划添加/配对/绑定到新的 CDK RestApi
。
因此,一种解决方法是创建一个新的
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 });