'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
Solution 1:[1]
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.
Solution 2:[2]
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.
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.
Solution 3:[3]
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
}));
}
}
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Dylan Pierce |
Solution 4 | aroe |