2

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!

aroe
  • 147
  • 2
  • 13
  • 1
    Does this answer your question? [Argument of type 'this' not assignable to parameter 'Construct'](https://stackoverflow.com/questions/59381686/argument-of-type-this-not-assignable-to-parameter-construct) – MyStackRunnethOver Sep 09 '20 at 14:54
  • This question is a duplicate of an older and much more complete one. Please consider saving the mods time and just deleting it in favor of the existing question :) – MyStackRunnethOver Sep 09 '20 at 14:55

5 Answers5

7

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
Yogeshwar Tanwar
  • 417
  • 4
  • 15
6

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';
Dharman
  • 30,962
  • 25
  • 85
  • 135
Paul Allsopp
  • 622
  • 1
  • 9
  • 23
  • 1
    Thanks man, this works in 2022! – Omar Dulaimi Feb 12 '22 at 13:56
  • In my case, I had imports from different versions. `import { Stack, App, StackProps,} from 'aws-cdk-lib';` and `import {Function, Runtime, Code} from '@aws-cdk-lib/ /aws-lambda';` Changed to ` import {Stack, App, StackProps,} from 'aws-cdk-lib'; import {Function, Runtime, Code,} from 'aws-cdk-lib/aws-lambda'; ` – Aman May 18 '22 at 11:01
  • Yep this works changed my bucket initialisation to -> new cdk.aws_s3.Bucket(this, ...) – Diana Aug 31 '22 at 09:43
4

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

Kordestan
  • 100
  • 1
  • 9
1

Just to pile on to Yogeshwar's answer (which was incredibly helpful)...

While making sure the versions being used are consistent was a good first step, understanding where the specific versions were coming from and why more deeply was key for me to fix this issue for me.

I'm using CDK v2, which only requires you to pull in a single module (aws-cdk-lib), but I was still seeing the issue related to the construct library. Two additional pieces of information that helped were to run npm why constructs to see where the different versions of the packages were being pulled in from. This led me to another module that I had created that was asking for "constructs": "^10.0.0", but was getting 10.1.x...

This pushed me towards getting a greater understanding of semver, and finding out that ^, will pull in anything in the 10.x series of versions, thus bringing in the incompatible package (the "Remove ^" step from the other answer).

This caused me to update how I'm specifying dependencies, and I was eventually able to fix the issue.

0

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

aroe
  • 147
  • 2
  • 13