aws-cdk: Can't get port from RDS DatabaseCluster Endpoint

Describe the bug I can’t pass the RDS port from one stack to another.

I open a question on StackOverflow about it, but now I think it’s actually a bug.

To Reproduce

class DbStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: { vpc: ec2.Vpc }) {
    super(scope, id);
    const { vpc } = props;
    const db = new DatabaseCluster(this, "Database", {
      engine: rds.DatabaseClusterEngine.AuroraPostgresql,
      engineVersion: "10.7",
      masterUser: {
        username: "admin",
      },
      defaultDatabaseName: "main",
      instanceProps: {
        instanceType: new ec2.InstanceType("r5.large"),
        vpcSubnets: {
          subnetType: ec2.SubnetType.Private,
        },
        vpc,
      },
      storageEncrypted: true,
      parameterGroup: {
        parameterGroupName: "default.aurora-postgresql10",
      } as any,
    });
    console.log(db.clusterEndpoint);
    console.log(db.clusterEndpoint.hostname);
    console.log(db.clusterEndpoint.port);
    console.log(db.clusterEndpoint.socketAddress);
  }
}

The console output is:

Endpoint {
  hostname: '${Token[Resource.Endpoint.Address.148]}',
  port: -1.8881545897087827e+289,
  socketAddress: '${Token[Resource.Endpoint.Address.148]}:{IndirectPort}' }
${Token[Resource.Endpoint.Address.148]}
-1.8881545897087827e+289
${Token[Resource.Endpoint.Address.148]}:{IndirectPort}

Expected behavior

I need a way to determine the port.

Version:

  • OS: Linux
  • Programming Language: Typescript
  • CDK Version: 0.33.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 17 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@balupraveendatty maybe you’re missing a call to Token.asString() somewhere where you’re passing the port directly as a number, where a string is expected? For example, this works for me:

        const vpc = new ec2.Vpc(this, 'Vpc');
        const db = new rds.DatabaseCluster(this, "Database", {
            engine: rds.DatabaseClusterEngine.auroraPostgres({
                version: rds.AuroraPostgresEngineVersion.VER_10_7,
            }),
            instanceProps: {
                vpc,
            },
        });
        new cdk.CfnOutput(this, 'Output', {
            value: cdk.Token.asString(db.clusterEndpoint.port),
        });

Produces:

Outputs:
  Output:
    Value:
      Fn::GetAtt:
        - DatabaseB269D8BB
        - Endpoint.Port

The problem is still present on 1.45.0 - I get port number -1.8881545897087736e+289 instead of 3306. Looks like overflow.

cdk.Token.asString(

not Token.toString but cdk.Token.asString which you earlier mentioned. 😃

Right. Exactly like I said in https://github.com/aws/aws-cdk/issues/2711#issuecomment-765628398, the code should be:

      port: Token.toString(props.serverlessCluster.clusterEndpoint.port),

, not:

      port: props.serverlessCluster.clusterEndpoint.port.toString(),

Thanks @skinny85 , you saved me 🥰

Nice one, cheers @skinny85