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

Viewed 3137

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!

4 Answers

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

If anyone comes across this issue currently, like I did which is what led me here, the thing I did to resolve this was use the cdk lib and import the things I needed from that.

import {Stack, StackProps, App, aws_s3 as s3, aws_iam as iam } from 'aws-cdk-lib';
import { BucketEncryption } from 'aws-cdk-lib/aws-s3';

update your CDK library. This is normally caused when your CDK library has different versions. npm update -g aws-cdk

The issue was that the @aws-cdk/lambda dependency was not the same version as the @aws-cdk/sqs and @aws-cdk/sns dependencies.

Related