aws-cdk: TS - Argument of type 'this' is not assignable to parameter of type 'Construct'
I have a created a simple Stack that creates a VPC with three simple subnets. though I receive the error Argument of type ‘this’ is not assignable to parameter of type ‘Construct’ . When I use any other resource like S3 or SQS I do not receive the error. As of now, it happens only with VpcNetwork. The typescript code is as follows
cdk version: 0.8.0 (build bb95676) node version: 10.3.0
import * as cdk from '@aws-cdk/cdk'
import { VpcNetwork, SubnetType } from '@aws-cdk/aws-ec2'
class MyStack extends cdk.Stack {
constructor(parent: cdk.App, name: string, props?: cdk.StackProps) {
super(parent, name, props);
new VpcNetwork(this, 'MyVPC', {
cidr: '10.0.0.0/21',
subnetConfiguration: [
{
cidrMask: 24,
name: 'Ingress',
subnetType: SubnetType.Public,
natGateway: true,
},
{
cidrMask: 24,
name: 'Application',
subnetType: SubnetType.Private,
},
{
cidrMask: 28,
name: 'Database',
subnetType: SubnetType.Isolated,
}
],
});
}
}
const app = new cdk.App(process.argv);
new MyStack(app, 'MyStack');
process.stdout.write(app.run());
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 39 (10 by maintainers)
Hi @benswinburne, I believe this is caused by the fact that your
package.jsonfile has mixed versions of the CDK (@aws-cdk/cdkis at 0.20.0 and@aws-cdk/aws-*are at 0.21.0):Try to upgrade everything to 0.21.0 and let us know if the issue still persists.
As mentioned above, this is usually caused by having more than a single version of the CDK in your node modules.
npm installagainYou should absolutely re-open this issue as this clearly has not been adequately solved. We’ve incurred a lot of cost trying to wrangle between version mismatches and I’m not even sure that fully diagnoses the issue of something as trivial as two separate versions of the Construct class.
This amounts to “configuration hell” and the responsible thing is to own that these issues exist or clearly state your assumptions in your documentation about what developers need to do to circumvent such issues in reliable ways. As of now, my conclusion is that building CDK applications in TypeScript is a waste of time (prefer JavaScript).
Since I stumpled over this one, even in latest cdk:
In working state, get the output of code that is a result of synth
Aws CDK has a mismatch (1.32.0) to the other modules (1.32.2).
you might not only want to nuke your node_modules folder, but as well the package-lock.json
This is a little side effect of the way typings work and how they evolve. An argument could be made, that a minor update is not the right thing as soon as a typing changed.
I hope this helps if you stumble over this problem.
I was having the same issue following a tutorial from AWS and the solution from @omar-dulaimi worked!
Basically change this:
import * as ec2 from "@aws-cdk/aws-ec2"; import * as ecs from "@aws-cdk/aws-ecs"; import * as ecs_patterns from "@aws-cdk/aws-ecs-patterns";To this:
import { Stack, StackProps, aws_ec2 as ec2, aws_ecs as ecs, aws_ecs_patterns as ecs_patterns } from 'aws-cdk-lib';@jordiyapz Maybe there’s a better way, but this is how I did it:
In my case it was just a problem with cached types. If you are working in typescript VSCode, try restarting it.
Please try to use
1.56.0without the^whenever using CDK.Also your devDependency links to
1.45.0@omar-dulaimi you’re mixing CDK v1 (
"@aws-cdk/aws-elasticbeanstalk"and"@aws-cdk/aws-iam") and CDK v2 ("aws-cdk-lib").Get rid of the V1 dependencies, re-install your modules, and you should be good to go 🙂.
Could somebody tell me what’s the issue here?
A fresh new project just minutes ago, and I keep getting these errors:
I have similar issue. I did
cdk initthen after few hours didnpm install @aws-cdk/aws-ec2. I realized that the problem is caused by different versions:Just after few hours there is a minor update on the module. Problem solved by changing
aws-cdk/coreversion to1.32.2It’s a good idea but might be a bit tricky for us to handle at the CLI level because it is language agnostic. We are actually changing the way peer dependencies are defined (see #3783) so I hope that this will improve the experience a little.
At any rate, you are right. This is an annoying friction we are aware of and would look into ways to improve this experience.
Yeah good point! I’m not used to being the one to post the answer haha! I decided to only use the V2 library, which keeps all the old V1 stuff at bay. So I blew away all of the V1 stuff from my stack definition file as well as the package.json files. But I had to find where the function calls were buried in the library so I could include them appropriately… The documentation page for the Vpc constructor is here As you can see, the Vpc function is can be found at: aws-cdk-lib » aws_ec2 » Vpc So I decided to include all of aws_ec2 with this line:
import { aws_ec2 as ec2 } from 'aws-cdk-lib;Then I called the Vpc function like this:const vpc = ec2.Vpc.fromLookup(this, "VPC", { isDefault: true });I followed this process for all of the other functions I needed: Find the documentation page, figure out where the function lives in the aws-cdk-lib, and include that location with an appropriate name. I hope this helps!Just to add, for anyone still struggling even after the helpful comments above:
After you nuke node_modules and package-lock.json, you then have to change all the versions for cdk things in package.json UP TO THE NEW VERSION, not down to old one. I tried changing it down (since I’d prefer not to be forced to use the latest minor release within minutes of it being released) and that didn’t work. Upwards and onwards.
I’m just going through the AWS getting started tutorials: https://aws.amazon.com/getting-started/guides/setup-cdk/module-three/ and I’m running into the same issue. In my case the it seems the tutorial is written for the V1 libraries, while V2 is now the default install. So following the tutorial results in trying to use V1 and V2 libraries at the same time. I was able to fix it with some knowledge of JavaScript and some googling, and I figured how to use V2, but it’s less than ideal. I think the tutorial documents need updating to use V2 so that new people don’t have to get so confused.
I had exactly the same issue with Python Function, I will share my workaround on this.
Write
Instead of
And then declare your object
Ohh waw, they combine multiple modules into one 😯. Thanks @omar-dulaimi , I’ll try it. Anw, currently the aws documentation site (
docs.aws.amazon.com) is unreachable so I have no way to lookup on how to use the V2 version of elasticbeanstalk module. Thanks again for the quick reply.UPDATE: Yes, it worked! Thanks
I’m experiencing this problem too using
0.20.0. I was following the example found herehttps://awslabs.github.io/aws-cdk/examples.html#step-3-create-a-fargate-service
and received a similar error to the one described above
I’ve pushed the example to a repo exactly as I have it
https://github.com/benswinburne/cdk-example
This doesn’t repro in 0.8.1, do you mind upgrading to the latest version and trying again? Let us know if you still encounter issues.
P.S. the
natGatewayproperty is not needed anymore…