0

I'm creating a QLDB Ledger as part of my stack. I would like to not have to supply a Ledger Name (otherwise I can't create multiple envs because of name clashes).

I also need to know the Ledger Name as an output / returned value from my stack because later some lambda functions need to know it to be able to use the QLDB SDK.

Using the following code, ledger.name is always undefined

import * as qldb from '@aws-cdk/aws-qldb';

const ledger = new qldb.CfnLedger(this, 'Ledger', {
  deletionProtection,
  permissionsMode: 'STANDARD',
});

console.log(ledger.name)

This kind of makes sense because it doesn't seem that the ledger gets its generated name until the Ledger is created in AWS (though it does seem to be derived from the stack).

FYI, once it is created and I know its name, searching for the name in the cdk.out folder finds nothing, so I don't believe it is included in the CFN template.

For now, I am generating a unique name with CDK using the following code but I just wonder if there's a cleaner way.

const name = cdk.Names.uniqueId(ledger);
ledger.name = name;

EDIT

This code often produced a name that was longer than the 32 char limit for the ledger name so I also ran a regex on it to get the last part of the unique name.

const name = cdk.Names.uniqueId(ledger);
ledger.name = name;
Chris Sargent
  • 321
  • 2
  • 13

1 Answers1

0

Considering your requirement is not to hard code the ledger name for the cfnLedger creation and you want to refer it's name in other cfn resources, you have following two options

  1. Refer the ledger name as a reference
    const ledger = new qldb.CfnLedger(this, 'Ledger', {
        deletionProtection,
        permissionsMode: 'STANDARD',
    });
    const ledgerName = ledger.ref
    
    // Create CloudFormation output. 
    new cdk.CfnOutput(this, 'QldbLedgerName', {
        value: ledger.ref,
        description: 'Qldb Ledger Name',
    });

Note : console.log(ledger.ref) will not produce a name but a token reference. e.g. ${Token[TOKEN.#]}.

  1. Generate the ledger name as unique id and pass it to the ledger creation. The approach you have taken.

We would recommend you go with option #1.

  • Hi Junaid, thanks for the answer much appreciated. Is the `ref` always going to be shorter than 32 chars? (I think I might have tried it but ran in to this issue). I also saw that `PhysicalName.GENERATE_IF_NEEDED` could be an option? – Chris Sargent Jul 11 '21 at 08:24
  • Generated ledger name is of the format : - We confirm that CFN respects ledgerName limitation of max 32 charecter. Ledger name will have two parts, 1/ At max 19 charecter's of the "id" of the QLDB CFN Ledger construct is taken 2/ Unique id generated by CFN of 12 charecters 3/ Component #1 and #2 are separated by '-' E.g.if I pass id as "LedgerNameTestjhbasjhkci", the ledger name generated is like "LedgerNameTestjhbas-ec4j9IDIHgbR" – Junaid Mohammed Jul 12 '21 at 23:21
  • Instead of PhysicalName.GENERATE_IF_NEEDED I would suggest you to go with recommended approach, as the ledger name here is readable and CFN construct generates id which satisfies the constraint of 32 charecter on ledger name. – Junaid Mohammed Jul 12 '21 at 23:24