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到现有的使用计划的一些想法?
你的问题已经有一个很好的提示-IUsagePlan
和UsagePlan
是不同的类型。我认为不可能将现有的使用计划添加/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 });