ค้นหาเว็บไซต์

node.js - Amazon CDK: ประเภท Construct ไม่ถูกต้องแม้ว่าจะถูกขยายมาจาก cdk.Construct?


ฉันกำลังพยายามที่จะปรับใช้ API Gateway พร้อมโดเมนที่กำหนดเอง (ไม่มีฟังก์ชันใด ๆ แค่ API Gateway ธรรมดาที่สแต็กอื่นสามารถนำไปใช้และใช้งานต่อได้)

bin/api-app.ts:

import { APIAppStack } from '../lib/api-app-stack';

export class MyAPIAppStack extends cdk.Stack {
    constructor(parent: cdk.App, name: string, props: cdk.StackProps) {
        super(parent, name, props);

        new APIAppStack(this, 'APIApp', {
            domainName: this.node.tryGetContext('domain'),
            siteSubDomain: this.node.tryGetContext('subdomain'),
        });
   }
}

const app = new cdk.App();

new MyAPIAppStack(app, 'APIAppStack'+(((process.env.subdomain || "").length > 0)?"-"+process.env.subdomain:""), { env: {
    region: process.env.AWS_DEFAULT_REGION,
    account: process.env.CDK_DEFAULT_ACCOUNT
}});

app.synth();

และนี่คือ Construct ของฉัน: lib/api-app-stack:

export interface APIAppProps {
    domainName: string;
    siteSubDomain: string;
}

export class APIAppStack extends Construct {
    constructor(parent: Construct, name: string, props: APIAppProps) {
        super(parent, name);
        const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
        const siteDomain = "api." + props.domainName;
        new cdk.CfnOutput(this, 'API', { value: 'https://' + siteDomain });

        // ... removed code for brevity. All works fine.
        // My error is below: (I am not able to create the ApiGateway itself)
        const api = new apigw.RestApi(this, 'APIApp', {
            restApiName: 'API App',
            description: "The API App exposes a common endpoint for the Apps to consume.",
            deployOptions: {
                accessLogDestination: accessLogDestination,
                accessLogFormat: apigw.AccessLogFormat.jsonWithStandardFields(),
                loggingLevel: apigw.MethodLoggingLevel.INFO,
                dataTraceEnabled: true,
                tracingEnabled: true,
                metricsEnabled: true,
                description: "Prod Stage"
            }
        });

ข้อผิดพลาดอยู่ที่ apigw.RestApi(this:

Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'APIAppStack' is not assignable to type 'Construct'.
    Property 'onValidate' is protected but type 'Construct' is not a class derived from 'Construct'.ts(2345)

ฉันเพิ่งเริ่มใช้ TypeScript และฉันเข้าใจว่าอาจมีความไม่ตรงกันของคุณสมบัติ แต่ฉันได้สร้างโค้ดนี้จากตัวอย่างโค้ด StaticSite บน GitHub ฉันยังเห็นว่า ApiAppStack สืบทอดมาจาก Construct ฉันพลาดอะไรไปหรือเปล่า?

แก้ไข 1: นี่คือ package.json ของฉัน:

{
  "name": "api-app",
  "version": "0.1.0",
  "bin": {
    "frontend-app": "bin/api-app.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "cdk": "cdk"
  },
  "devDependencies": {
    "aws-cdk": "1.38.0",
    "@aws-cdk/assert": "1.38.0",
    "@types/node": "10.17.5",
    "jest": "^25.5.0",
    "ts-node": "^8.1.0",
    "typescript": "~3.7.2",
    "@aws-cdk/aws-certificatemanager": "*",
    "@aws-cdk/aws-cloudfront": "*",
    "@aws-cdk/aws-iam": "*",
    "@aws-cdk/aws-route53": "*",
    "@aws-cdk/aws-route53-targets": "*",
    "@aws-cdk/aws-s3": "*",
    "@aws-cdk/aws-s3-deployment": "*",
    "@aws-cdk/core": "*"
  },
  "dependencies": {
    "@aws-cdk/aws-apigateway": "^1.47.1",
    "@aws-cdk/aws-sns": "1.38.0",
    "@aws-cdk/aws-sns-subscriptions": "1.38.0",
    "@aws-cdk/aws-sqs": "1.38.0",
    "@aws-cdk/core": "1.38.0"
  }
}

คำตอบ #1

CDK ไม่มีความเข้ากันได้ย้อนหลังในการอัปเกรดเวอร์ชันย่อย

นั่นหมายความว่า cdk.Construct ของคุณจากแพ็กเกจ

"@aws-cdk/core": "1.38.0"

ไม่เข้ากันกับ apigw.RestApi ซึ่งกำลังใช้เวอร์ชันอย่างน้อย 1.47:

"@aws-cdk/aws-apigateway": "^1.47.1"

เพื่อแก้ไขปัญหานี้ ให้เปลี่ยนแพ็กเกจ @aws-cdk ทั้งหมดเป็นเวอร์ชันเดียวกันใน package.json ของคุณ และ อย่าลืมลบ ^ หรืออย่างน้อยก็แทนที่ด้วย ~.

จากนั้นเพียงติดตั้ง dependencies ของคุณใหม่: npm install

บทความที่เกี่ยวข้อง