Home > OS >  Proper way to use AWS CDK imports
Proper way to use AWS CDK imports

Time:09-03

I viewed and read a few tutorials about using the AWS-CDK with typescript, and was getting a constant error stating "Argument of type 'this' is not assignable to parameter of type 'Construct'" Most answers said that it was because of a CDK mismatch, but I found that not to be my case as this was my package.json dependencies

  "dependencies": {
    "@aws-cdk/aws-lambda": "1.171.0",
    "@aws-cdk/aws-s3": "1.171.0",
    "aws-cdk-lib": "2.40.0",
    "constructs": "^10.1.94",
    "source-map-support": "^0.5.21"
  }

I see that importing the CDK dependencies as

import * as AWS_lambda from '@aws-cdk/aws-lambda'

does not seem to work, but importing them like

import * as subs from 'aws-cdk-lib/aws-sns-subscriptions'

does seem to work. Is the latter the expected way to perform the imports now?

CodePudding user response:

You are mixing CDK v1 and CDK v2 dependencies here. "aws-cdk-lib": "2.40.0" is CDK V2, which consolidates all the libraries into a single dependency. There for you do not need to individually add Lambda and S3 as well, as they will already exist in aws-cdk-lib.

Try just this:

"dependencies": {
    "aws-cdk-lib": "2.40.0",
    "constructs": "^10.1.94",
    "source-map-support": "^0.5.21"
}
  • Related