skip to Main Content

dI created a custom resource through CDK and return a certain "CommaSeparatedString" as part of the response

  return {
    Status: "SUCCESS",
    RequestId: event.RequestId,
    StackId: event.StackId,
    LogicalResourceId: event.LogicalResourceId,
    PhysicalResourceId: event.ServiceToken,
    Data: { CommaSeparatedString: "ABC,XYZ,TTT"},
  };

I then access this in my CDK stack as below:

myCommaSeparatedString = myCustomResource.getAttString("CommaSeparatedString");

I want to convert this string into a list of strings, but doing this has no effect!

listOfString = myCommaSeparatedString.split(",");

I believe this is because at deployment time, the value of getAttString will be a placeholder in CDK (something like [ ‘${Token[TOKEN.97]}’ ] and hence doesn’t process the actual value.

Since the Data map in the CDK Custom resource response doesn’t support any other complex data types but only String this seems technically impossible.

Does anyone have any ideas or used CDK custom resources to return complex data types?

2

Answers


  1. I am not sure if this would help, but the getAttString(attribute: string) accepts string
    and your response returns data: {key: comma separated list}
    may be if you destruct your response

    const {data} = {your response}
    getAttString(data.CommaSeparatedString)
    
    Login or Signup to reply.
  2. Short answer:

    listOfString = Fn.split(',', myCommaSeparatedString);
    const value = Fn.select(0, listOfString);
    

    Long answer:

    The reason why your split function has no effect is because myCommaSeparatedString value is not available at synthesize time.

    If instead of a dynamic value, you would have myCommaSeparatedString = "ABC,XYZ,TTT" instead, you won’t have that issue.

    Using Cloudformation Intrinsic function reference will handle those cases of dynamic value processing.

    You could also validate CDK behavior by doing cdk synth before doing a cdk deploy.

    Before:

    const myCommaSeparatedString = myCustomResource.getAttString("CommaSeparatedString");
    const listOfString = myCommaSeparatedString.split(",");
    new CfnOutput(this, 'id', {
      value: listOfString[0]
    })
    

    cdk synth

    Outputs:
      id:
        Value:
          Fn::GetAtt:
            - MyCustomResource
            - CommaSeparatedString
    

    After:

    const myCommaSeparatedString = myCustomResource.getAttString("CommaSeparatedString");
    const listOfString = Fn.split(',', myCommaSeparatedString);
    new CfnOutput(this, 'id', {
      value: Fn.select(0, listOfString)
    })
    

    cdk synth

    Outputs:
      id:
        Value:
          Fn::Select:
            - 0
            - Fn::Split:
                - ","
                - Fn::GetAtt:
                    - MyCustomResource
                    - CommaSeparatedString
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search