Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of type 'this' not assignable to parameter 'Construct'

I am trying to call a lambda function into a 'sample app' stack and it is giving me an error because I am trying to pass it a parameter of 'this'.

Here is my lambda function

export async function handler(event) {
    console.log("request:", JSON.stringify(event, undefined, 2));
    return {
        statusCode: 200,
        headers: { "Content-Type": "text/plain" },
        body: `Hello, CDK! You've hit ${event.path}\n`
    };
};

Here is the 'app' calling that function

//import sns = require('@aws-cdk/aws-sns');
//import subs = require('@aws-cdk/aws-sns-subscriptions');
//import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');

//Exports class from other file much like a function

export class CdkWorkshopStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { 
    super(scope, id, props);

    // Describes an AWSLambda Resource
    const hello = new lambda.Function (this, 'HelloHandler', {
      runtime: lambda.Runtime.NODEJS_8_10,    //execution environment
      code: lambda.Code.asset('lambda'),   // code loaded from the "lambda" directory
      handler: 'hello.handler'                // file is "hello", function is "handler"
    });
  }
}

The error I'm getting is:


lib/cdk-workshop-stack.ts:31:39 - error TS2345: Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'CdkWorkshopStack' is not assignable to type 'Construct'.
    Types of property 'node' are incompatible.
      Type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/core/lib/construct").ConstructNode' is not assignable to type 'import("/Users/aroe/cdk-workshop/node_modules/@aws-cdk/aws-lambda/node_modules/@aws-cdk/core/lib/construct").ConstructNode'.
        Types have separate declarations of a private property 'host'.

31     const hello = new lambda.Function(this, 'HelloHandler', {
                                         ~~~~

[1:24:08 PM] Found 1 error. Watching for file changes.

And finally I am using Node version v13.3.0

like image 284
aroe Avatar asked Dec 17 '19 20:12

aroe


1 Answers

The construct definition looks right to me. That error can occur if the various cdk modules aren't all at the same version; see Argument of type 'this' is not assignable to parameter of type 'Construct' for one example of that. Try running npm update; see if that resolves the issue.

like image 160
Charles Fulton Avatar answered Sep 26 '22 07:09

Charles Fulton