Home > Software design >  Fetching AWS SecretManager Secret as Integer Value per CDK
Fetching AWS SecretManager Secret as Integer Value per CDK

Time:01-21

I'm trying to fetch the Secret as integer Value (port number) per CDK in order to create another resource. Basically when I need a String value for String parameters everything is working fine, but when I try to parse the String to int in order to provide the number parameter it does not work anymore. The problem is that CDK generates a reference for these values and it cannot be casted to a number value. The question is: is there any way to retrive the Secret Value as number?

Here some code snippets:

SecretManager Object:

const secret = secretsmanager.Secret.fromSecretAttributes(this, "SecretId", {
      secretCompleteArn: someValidSecretArn
    });

Working fine:

host: secret.secretValueFromJson('host').toString()

Not working because the parameter needs to be a number value:

port: secret.secretValueFromJson('port').toString()

Not working because port is null (it is not!), basically the reference cannot be parsed:

port: parseInt(secret.secretValueFromJson('port').toString())

Not working, same as above:

port:  secret.secretValueFromJson('port').toString()

CodePudding user response:

You can use the CDK's Token class to cast the token to a number:

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_core.Token.html#static-aswbrnumbervalue

port: cdk.Token.asNumber(secret.secretValueFromJson('port'));

It will be resolved to the proper value at deploy time.

  •  Tags:  
  • Related