我是否错误地拆分了这些构造?
Have I split up these Constructs incorrectly?
我是 CDK 的新手,我曾尝试将一些东西从 vanilla JavaScript 迁移到 TypeScript,这样做是为了让它更加模块化。这是我目前的结构:
CdkStack.ts
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// API Gateway
const api = new API(scope, "API");
}
Api.ts
export class API extends Construct {
public restApi: RestApi;
constructor(scope: Construct, id: string) {
super(scope, id);
const restApi = new RestApi(this, "website-api", {
restApiName: "website-api",
description: "This service provides API calls for the website.",
});
const getListLambda = new GetListLambda(
scope,
"GetListLambda"
);
const listApi = restApi.root.addResource("list");
listApi.addMethod("GET", getListLambda.integration); // GET /
}
}
GetListLambda.ts
export class GetListLambda extends Construct {
public lambdaFunction: Function;
public integration: LambdaIntegration;
constructor(scope: Construct, id: string) {
super(scope, id);
this.lambdaFunction = new Function(this, "GetListLambda", {
runtime: Runtime.NODEJS_12_X,
code: new AssetCode("lambdas/getList", {
exclude: ["*.local.js", "*.test.js"],
}),
handler: "index.handler",
environment,
});
this.integration = new LambdaIntegration(this.lambdaFunction, {
proxy: true,
});
}
}
我以前有基本相同的代码,但都在一个文件中。使用当前结构,我在尝试进行部署时得到以下输出:
No stack could be identified for the construct at path API/website-api
/src/api/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:540
if (this.methods.length === 0) {
^ TypeError: Cannot read property 'length' of undefined
at RestApi.validate (/src/api/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:540:22)
at RestApi.onValidate (/src/api/node_modules/@aws-cdk/core/lib/construct-compat.ts:97:17)
at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:445:54)
at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:442:45)
at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:442:45)
at Node.synthesize (/src/api/node_modules/constructs/lib/construct.ts:391:27)
at Function.synth (/src/api/node_modules/@aws-cdk/core/lib/construct-compat.ts:231:22)
at App.synth (/src/api/node_modules/@aws-cdk/core/lib/app.ts:142:36)
at process. (/src/api/node_modules/@aws-cdk/core/lib/app.ts:121:45)
at Object.onceWrapper (events.js:422:26)
错误看起来好像没有任何方法,但是我已经清楚地向 API 添加了一个方法。
使用
const api = new API(this, "API");
而不是
const api = new API(scope, "API");
会将堆栈引用传递给底层模块。
我是 CDK 的新手,我曾尝试将一些东西从 vanilla JavaScript 迁移到 TypeScript,这样做是为了让它更加模块化。这是我目前的结构:
CdkStack.ts
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// API Gateway
const api = new API(scope, "API");
}
Api.ts
export class API extends Construct {
public restApi: RestApi;
constructor(scope: Construct, id: string) {
super(scope, id);
const restApi = new RestApi(this, "website-api", {
restApiName: "website-api",
description: "This service provides API calls for the website.",
});
const getListLambda = new GetListLambda(
scope,
"GetListLambda"
);
const listApi = restApi.root.addResource("list");
listApi.addMethod("GET", getListLambda.integration); // GET /
}
}
GetListLambda.ts
export class GetListLambda extends Construct {
public lambdaFunction: Function;
public integration: LambdaIntegration;
constructor(scope: Construct, id: string) {
super(scope, id);
this.lambdaFunction = new Function(this, "GetListLambda", {
runtime: Runtime.NODEJS_12_X,
code: new AssetCode("lambdas/getList", {
exclude: ["*.local.js", "*.test.js"],
}),
handler: "index.handler",
environment,
});
this.integration = new LambdaIntegration(this.lambdaFunction, {
proxy: true,
});
}
}
我以前有基本相同的代码,但都在一个文件中。使用当前结构,我在尝试进行部署时得到以下输出:
No stack could be identified for the construct at path API/website-api
/src/api/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:540 if (this.methods.length === 0) { ^ TypeError: Cannot read property 'length' of undefined at RestApi.validate (/src/api/node_modules/@aws-cdk/aws-apigateway/lib/restapi.ts:540:22) at RestApi.onValidate (/src/api/node_modules/@aws-cdk/core/lib/construct-compat.ts:97:17) at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:445:54) at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:442:45) at Node.validate (/src/api/node_modules/constructs/lib/construct.ts:442:45) at Node.synthesize (/src/api/node_modules/constructs/lib/construct.ts:391:27) at Function.synth (/src/api/node_modules/@aws-cdk/core/lib/construct-compat.ts:231:22) at App.synth (/src/api/node_modules/@aws-cdk/core/lib/app.ts:142:36) at process. (/src/api/node_modules/@aws-cdk/core/lib/app.ts:121:45) at Object.onceWrapper (events.js:422:26)
错误看起来好像没有任何方法,但是我已经清楚地向 API 添加了一个方法。
使用
const api = new API(this, "API");
而不是
const api = new API(scope, "API");
会将堆栈引用传递给底层模块。