Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get port from RDS DatabaseCluster Endpoint #2711

Closed
joehillen opened this issue May 31, 2019 · 17 comments · Fixed by #2846 or MechanicalRock/tech-radar#14 · May be fixed by MechanicalRock/cdk-constructs#5, MechanicalRock/cdk-constructs#6 or MechanicalRock/cdk-constructs#7
Labels
bug This issue is a bug.

Comments

@joehillen
Copy link
Contributor

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
@joehillen joehillen added the bug This issue is a bug. label May 31, 2019
@joehillen
Copy link
Contributor Author

joehillen commented Jun 1, 2019

This might be a duplicate of #2679, but not exactly.

@joehillen
Copy link
Contributor Author

Just confirmed. Still happening in v0.34

@cjyclaire
Copy link

+1 for this, seeing the same behavior

jogold added a commit to jogold/aws-cdk that referenced this issue Jun 12, 2019
The port needs to be passed as a string to the `Endpoint` class otherwise the concatenation of
address and port does not resolve correctly (forced string conversion of number token).

Fixes aws#2711
jogold added a commit to jogold/aws-cdk that referenced this issue Jun 12, 2019
The port needs to be passed as a string to the `Endpoint` class otherwise the concatenation of
address and port does not resolve correctly (forced string conversion of number token).

Fixes aws#2711
jogold added a commit to jogold/aws-cdk that referenced this issue Jun 12, 2019
Convert port to a string token before using it in `socketAddress` otherwise the number token is
converted to a string and cannot be resolved.

Fixes aws#2711
rix0rrr pushed a commit that referenced this issue Jun 13, 2019
Convert port to a string token before using it in `socketAddress` otherwise the number token is
converted to a string and cannot be resolved.

Fixes #2711
@jogold
Copy link
Contributor

jogold commented Jun 13, 2019

This should be fixed in the next release, as a temporary workaround in v0.34.0 you can do this to use the socketAddress somewhere

const socketAddress = `${db.clusterEndpoint.hostname}:${new cdk.Token(db.clusterEndpoint.port)}`;

@dekoza
Copy link

dekoza commented Jun 12, 2020

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

@emisbalu
Copy link

This issue is still there in 1.82, Please provide a fix for this

Name: ....
Value:
  Fn::Join:
    - ""
    - - jdbc:postgresql://
      - Fn::GetAtt:
          - RDSDBInstanceD5E32DDA
          - Endpoint.Address
      - :-1.8881545897088128E289/maindb

@skinny85
Copy link
Contributor

@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

@emisbalu
Copy link

Nice one, cheers @skinny85

@v1pz3n
Copy link

v1pz3n commented Sep 11, 2021

Thanks @skinny85 , you saved me 🥰

@moltar
Copy link
Contributor

moltar commented Nov 27, 2021

Looks like this is still an issue.

When I console.log the port:

console.log('port', cluster.clusterEndpoint.port)

I get:

port -1.8881545897088303e+289

@moltar
Copy link
Contributor

moltar commented Nov 27, 2021

Btw, I can fix it like this:

Object.assign(cluster.clusterEndpoint, { port: 5432 })

@skinny85
Copy link
Contributor

@moltar #2711 (comment)

@moltar
Copy link
Contributor

moltar commented Nov 30, 2021

@skinny85 But I don't have the option to wrap in cdk.Token.asString, as I am passing the entire cluster instance to another construct (see linked issue above my first comment). Should the third party construct be handling this then?

@skinny85
Copy link
Contributor

OK... can you show the consuming code then?

@skinny85
Copy link
Contributor

skinny85 commented Dec 7, 2021

Right. Exactly like I said in #2711 (comment), the code should be:

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

, not:

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

@sangheestyle
Copy link

cdk.Token.asString(

not Token.toString but cdk.Token.asString which you earlier mentioned. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment