27

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

aroe
  • 499
  • 1
  • 6
  • 15

7 Answers7

25

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.

Charles Fulton
  • 406
  • 4
  • 5
  • 3
    I got the error after installing @aws-cdk/aws-lambda. My CDK version was 1.22.0 while the recently installed lambda package was 1.23.0 After installing the corresponding lambda 1.22.0 the issue was fixed – CCarlos Feb 10 '20 at 14:23
7

I got the same error because the installed version for @aws-cdk/aws-lambda and @aws-cdk/aws-sns-subscriptions was different,

while aws-sns-subscriptions has a dependency on aws-lambda of the same version.

enter image description here

when I downgraded @aws-cdk/aws-lambda version the error is gone!

you can also update all aws-cdk packages to have the same version.

Aya Salama
  • 1,458
  • 13
  • 17
6

In my case I even though I had used the cdk init app --language=typescript the project bootstrapped with the old legacy aws-cdk library.

So when I started using Typescript libraries from the @aws-cdk namespaced packages, I received this error.

So I had to replace all references to Stack, Construct, etc. from the @aws-cdk/core:

import * as cdk from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';
import * as lambda from '@aws-cdk/aws-lambda'
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources';

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

    // example resource
    const queue = new sqs.Queue(this, 'WorkerQueue', {
      queueName: 'WorkerQueue',
    });

    // processor function
    const processor = new lambda.Function(this, 'QueueProcessor', {
      code: lambda.Code.fromAsset('lambda'),
      handler: 'processor.handler',
      functionName: 'QueueProcessor',
      runtime: lambda.Runtime.NODEJS_12_X,
    });

    // map processor to queue
    processor.addEventSource(new SqsEventSource(queue, {
      batchSize: 10, // default
      reportBatchItemFailures: true, // default to false
    }));
  }
}

Dylan Pierce
  • 4,313
  • 3
  • 35
  • 45
3

change import * as cdk from 'aws-cdk-lib';

to import * as cdk from '@aws-cdk/core';

in bin/something_cdk.ts file

  • 2
    AWS's own CDK Primer tutorial is screwed up. The tutorial video and isntructions assume v1 dependencies, but the test files Amazon provides contain a mix of v1 and v2 dependencies. Your answer got me on the right track to fix Amazon's tutorial. Thank you! – kookaburra Jul 15 '22 at 16:30
  • Feels great to be of help !! @kookaburra – Rishabh Jain Dec 12 '22 at 10:51
2

This worked for me:

import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';

instead of using:

import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';

Getting all the modules from aws-cdk-lib, thus from the same version

vikramaditya234
  • 1,278
  • 2
  • 18
  • 37
1

This was solved by simply ignoring the 'this' error and running the lambda anyway. I do not believe it would have worked if it was an actual node/JS program. However, when using CDK native TypeScript it babies you a lot.

aroe
  • 499
  • 1
  • 6
  • 15
  • can you plz share what you did put after removing this coz I am also getting same issue but after removing "this" getting lib/cdk-stack.ts:183:52 - error TS2345: Argument of type '"playerlogsBucket"' is not assignable to parameter of type 'Construct'. 183 const playerlogsBucket = new s3.CfnBucket( "playerlogsBucket", { ~~~~~~~~~~~~~~~~~~ – Ashish Karpe Sep 08 '21 at 07:05
1

Just changing

import * as cdk from "@aws-cdk/core";

import * as apigw from "@aws-cdk/aws-apigateway";

import lambda from "@aws-cdk/aws-lambda"

import dynamodb from "@aws-cdk/aws-dynamodb"

To

const lambda = require ('@aws-cdk/aws-lambda');

const dynamodb= require('@aws-cdk/aws-dynamodb');

const cdk=require('@aws-cdk/core');

const apigw=require('@aws-cdk/aws-apigateway');

worked for me

J.dev
  • 844
  • 3
  • 12
  • 22