Search code examples
typescriptamazon-rdsaws-cdkamazon-auroraaws-security-group

AWS CDK Typescript issue: The expected type comes from property 'securityGroups' which is declared here on type 'InstanceProps'


What can I do here?

I am getting this 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: [ rds.DatabaseClusterEngine.auroraPostgres ],
         engineVersion: [ rds.AuroraPostgresEngineVersion.VER_13_3 ],
        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'

Updating my question:

securityGroups: [dbClusterSecurityGroup] this worked but now getting errors

 engine: [ rds.DatabaseClusterEngine.auroraPostgres ],
 engineVersion: [ rds.AuroraPostgresEngineVersion.VER_13_3 ],

Error is:

TSError: ⨯ Unable to compile TypeScript:
lib/database-stack.ts:120:9 - error TS2739: Type '((props: AuroraPostgresClusterEngineProps) => IClusterEngine)[]' is missing the following properties from type 'IClusterEngine': singleUserRotationApplication, multiUserRotationApplication, supportedLogTypes, bindToCluster, engineType

120         engine: [ rds.DatabaseClusterEngine.auroraPostgres ],
            ~~~~~~

  node_modules/@aws-cdk/aws-rds/lib/cluster.d.ts:26:14
    26     readonly engine: IClusterEngine;
                    ~~~~~~
    The expected type comes from property 'engine' which is declared here on type 'DatabaseClusterProps'

Solution

  • 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]