Argument of type 'this' is not assignable to parameter of type 'Construct' in AWS CDK

Issue

I am having an issue while using the CDK in that the this property is erroring and saying that I can’t assign ‘this’ to parameter of type construct. This is happens start on the const s3ListLambdaRole part and makes every new variable declaration after that also error for the same thing.

import * as sns from '@aws-cdk/aws-sns';
import * as subs from '@aws-cdk/aws-sns-subscriptions';
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
import * as lambda from '@aws-cdk/aws-lambda';
import * as path from 'path';
import { Bucket } from '@aws-cdk/aws-s3';
import * as iam from'@aws-cdk/aws-iam';


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

    const testSecurityqueue = new sqs.Queue(this, 'testSecurityqueue', {
      visibilityTimeout: cdk.Duration.seconds(300)
    });

    const testSecuritytopic = new sns.Topic(this, 'testSecuritytopic');

      testSecuritytopic.addSubscription(new subs.SqsSubscription(testSecurityqueue));
    //Creating lambda role below
    const s3ListLambdaRole = new iam.Role(this, 's3ListLambdaRole', {
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
    });

    s3ListLambdaRole.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AWSLambdaFullAccess')) //creates LambdaFullAccess Role

    //Adding specific permissions to role now

    s3ListLambdaRole.addToPolicy(new iam.PolicyStatement({
      resources: ['*'], //adds full access to lamda
      actions: ['s3']
    }));

    const s3ListLambda = new lambda.Function (this, 's3ListLambda', {
      runtime: lambda.Runtime.PYTHON_3_6,
      handler: 'listS3.handler',
      role:s3ListLambdaRole,
      code: lambda.Code.fromAsset(path.join(__dirname, '../lambda'))
    });

    const testSecurityBucket = new s3.Bucket(this, 'testSecurityBucket');






    }

  }

Thank you in advance!

Solution

This happens when version of CDK dependencies are at different versions.Make sure CDK dependencies have same version.

  • Delete node_modules folder
  • Delete package-lock.json
  • Ensure all dependencies in package.json are using same version.
  • Remove carrot ^ symbol before dependencies
  • npm install

Answered By – Yogeshwar Tanwar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published