Home > Software design >  AWS CDK Typescript issue: The expected type comes from property 'securityGroups' which is
AWS CDK Typescript issue: The expected type comes from property 'securityGroups' which is

Time:10-28

I am not sure what to do, any help is appreciated!

Getting error:

Type '{ instanceType: ec2.InstanceType; securityGroup: ec2.SecurityGroup; vpc: ec2.IVpc; vpcSubnets: { subnetName: string; }; }' is not assignable to type 'InstanceProps'.
  Object literal may only specify known properties, but 'securityGroup' does not exist in type 'InstanceProps'. Did you mean to write 'securityGroups'?

My Code:

const dbClusterSecurityGroup = new ec2.SecurityGroup(this, "dbClusterSg", {
      allowAllOutbound: true,
      description: `Project ${projectName} Service database cluster`,
      securityGroupName: `${projectName}Database`,
      vpc,
    });
    dbClusterSecurityGroup.node.applyAspect(new cdk.Tag("Name", "serviceDatabaseCluster"));
    dbClusterSecurityGroup.addIngressRule(
      vpnSG,
      ec2.Port.tcp(postgresDatabasePort),
    );
    dbClusterSecurityGroup.addIngressRule(
      codeBuildProjectSG,
      ec2.Port.tcp(postgresDatabasePort),
    );
if (activeRegion === this.region) {
      const postgresCluster = new rds.DatabaseCluster(this, "dbCluster", {
        backup: {
          preferredWindow: "05:00-06:00",
          retention: cdk.Duration.days(30),
        },
        defaultDatabaseName: postgresDatabaseName,
        //engine: "aurora",
        //engine: rds.DatabaseInstanceEngine.AURORA_POSTGRESQL,
        engineVersion: postgresDatabaseEngineVersion,
        instanceProps: {
          instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MEDIUM),
          *securityGroup: dbClusterSecurityGroup,*
          vpc,
          vpcSubnets: { subnetName: "data" },
        },

when tried securityGroups: dbClusterSecurityGroup, getting error :

Type 'SecurityGroup' is missing the following properties from type 'ISecurityGroup[]': length, pop, push, concat, and 26 more.
The expected type comes from property 'securityGroups' which is declared here on type 'InstanceProps'

CodePudding user response:

Start with checking the documentation:

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-rds.InstanceProps.html

As you can see, InstanceProps doesn't have a securityGroup parameter, just securityGroups, which accepts an array of ISecurityGroup objects, not just one ISecurityGroup, which you're passing. This is how it should look like:

securityGroups: [dbClusterSecurityGroup]

CodePudding user response:

If you are using cdk version 1, make sure all your cdk packages in your package.json have the exact same version. Otherwise, strange errors like this may appear.

  • Related