Reputation: 519
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
Upvotes: 29
Views: 40512
Reputation: 1408
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
Upvotes: 6
Reputation: 153
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
Upvotes: 1
Reputation: 49
change
import * as cdk from 'aws-cdk-lib';
to
import * as cdk from '@aws-cdk/core';
in bin/something_cdk.ts file
Upvotes: 4
Reputation: 4668
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
}));
}
}
Upvotes: 7
Reputation: 1518
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.
Upvotes: 7
Reputation: 416
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.
Upvotes: 26
Reputation: 519
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.
Upvotes: 1