Home > Blockchain >  How to extract parameters of type stringList in cdk?
How to extract parameters of type stringList in cdk?

Time:07-05

I am trying to extract existing SSM parameters of stringList in my cdk app.

I can store a single value, but to avoid duplication of code I am trying to store 3 values in single variable and access them as array in the form value[0]

        const stringValue = ssm.StringParameter.fromStringParameterAttributes(this, 'MyValue', 
    
        const stringValue2 = ssm.StringListParameter.fromStringListParameterName(this, 'myValue', '/dev/name')
    
        const ssmstringlistoutput = new cdk.CfnOutput(this, 'ssmoutput', {
          value: cdk.Fn.select(0, stringValue2.stringListValue),
          description: 'name of ssmlist',
          exportName: 'avatarsBucket',
        });
    
    
        // Chatbot Slack Notification Integration
        const bot = new chat.SlackChannelConfiguration(
          this,
          "sample-slack-notification",
          {
            slackChannelConfigurationName: 'my channel name', 
            slackWorkspaceId: 'stringValue2[0]',
            slackChannelId: 'stringValue[1]',
            notificationTopics: [topic],
          }
        );
      }

The output is for verification. It outputs the entire list as one string value, not just the extracted first element as expected.

CodePudding user response:

Instead of StringListParameter.fromStringListParameterName, create a CfnParameter construct. Pass the parameter name as the default value:

const listParam = new cdk.CfnParameter(this, 'ListParam', {
  type: 'AWS::SSM::Parameter::Value<List<String>>',
  default: '/dev/name',
});

Then select an element when passing the value as prop inputs:

slackWorkspaceId: cdk.Fn.select(0, listParam.valueAsList),
slackChannelId: cdk.Fn.select(1, listParam.valueAsList),

What's going on? fromStringListParameterName synthesises to a CloudFormation dynamic reference like {{resolve:ssm:/dev/name}}. And as you discovered, CloudFormation apparently cannot apply a split to dynamic references.

  • Related