Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fn::split in Serverless yaml configuration not working

I'm using serverless framework to deploy an API on AWS. I have the following in my serverless.yml file:

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          fn::split:
            delimiter: ','
            value: ${env:VPC_SG_ID}

VPC_SG_ID contains the following string: sg-1111111111,sg-222222222,sg-3333333333

However, when deploying the application, I get the following error: An error occurred: MyLambdaFunction - Value of property SecurityGroupIds must be of type List of String.

If I hardcode the SGs list, it's working without any issue:

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          - "sg-1111111111"
          - "sg-2222222222"
          - "sg-3333333333"

Why the fn::split function is not returning a list of strings?

Edit:

The following configuration results in the same error

custom:
  vpcSettings:
    private:
      securityGroupIds:
        private:
          Fn::Split:
            - ','
            - ${env:VPC_SG_ID}
like image 281
Cramix Avatar asked Sep 12 '25 00:09

Cramix


2 Answers

In order to use the split function, first install serverless-plugin-utils

npm install -D serverless-plugin-utils

add it to your plugins section in serverless.yml

plugins:
 - serverless-plugin-utils

The split function is now available. If I have a following custom variable section

custom:
  my_var: 'value1,value2,value3,value4'

and I want to split it into an array like below

values:
  - value1
  - value2
  - value3
  - value4

then split function can be used from serverless utils plugin

values: 
   ${split(${self:custom.my_var}, ',')}
like image 167
M. Tovbin Avatar answered Sep 13 '25 17:09

M. Tovbin


I had an exact same problem, where I was doing split as below, but it was not working

   Fn::Split:
        delimiter: ','
        value: ${cf:mystack.ClusterSubnets}

It was giving split errors like

Split object requires two parameters, (1) a string delimiter and (2) a string to be split

I changed it to the following and it worked

    Fn::Split: 
        - ','
        - ${cf:mystack.ClusterSubnets}
like image 35
A.B Avatar answered Sep 13 '25 18:09

A.B