Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon CDK: Incorrect Construct Type even though it is extended from cdk.Construct?

I am trying to deploy an API Gateway with a custom domain. (no functions, just a common API Gateway that other stacks can then import and use)

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();

And here is my 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"
            }
        });

The error is at 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)

I am new to TypeScript and I understand there might be a mismatch of properties, but I derived this code from the StaticSite example code on GitHub. I even saw that the ApiAppStack derives from Construct. Am I missing something?

Edit 1: Here is my 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"
  }
}

like image 924
Aekansh Dixit Avatar asked Jan 24 '26 12:01

Aekansh Dixit


1 Answers

CDK doesn't have backwards compatibility in minor version upgrades.

That means that your cdk.Construct from package

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

is not compatible with the apigw.RestApi, which is using the version at least 1.47:

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

to fix this, change all the @aws-cdk packages to the same version in your package.json and don't forget to remove the ^ or at least replace with ~.

then simply reinstall your dependencies: npm install

like image 83
Pedreiro Avatar answered Jan 27 '26 00:01

Pedreiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!