I’m using serverless 1.15.3 to deploy a lambda function into a VPC and need to set different securityGroupIds and subnetIds for each deployment environment (dev, test, prod). For other configuration that depends on the deployment environment I’ve used environment variables that are set by the pipeline, however, the securityGroupIds and subnetIds are lists, and I haven’t been able to figure out how to specify values in the environment so they can be used within the serverless.yml configuration.
My preferred solution would define the vpc as follows in serverless.yml:
custom:
defaultSubnetIds: [subnet-12345678, subnet-9abcde01, subnet-23456789]
defaultSecurityGroupIds: [sg-12345678]
vpc:
subnetIds: ${env:SUBNET_IDS, self:custom.defaultSubnetIds}
securityGroupIds: ${env:SECURITY_GROUP_IDS, self:custom.defaultSecurityGroupIds}
Serverless is happy with this, as long as there’s SECURITY_GROUP_IDS and SUBNET_IDS are not defined in the environment.
However, I haven’t been able to find a way to set the value of either of the above environment variables so that serverless interprets them as a list of strings. Serverless gives the error Value of property SubnetIds must be of type List of String.
when I try to set the environment variables following any of the following patterns:
SUBNET_IDS="[sg-1,sg2,sg3]"
SUBNET_IDS="['sg1','sg2','sg3']"
SUBNET_IDS="['sg1', 'sg2', 'sg3']"
SUBNET_IDS="[ sg1 sg2 sg3 ]"
SUBNET_IDS="[ sg1, sg2, sg3 ]"
SUBNET_IDS=(sg1 sg2 sg3)
Generally speaking, the shell requires the double quotes.
For the time being, I’m using a separate variable for each SUBNET_ID, but I find this dissatisfying because it is inflexible about the number of security groups that are defined.
Has anyone had any luck setting a serverless attribute list with an environment variable?