3

I am trying to add a MongoDB cluster as part of a Serverless deployment, but I can't set the environment variable.

Here is part of the serverless.yml file:

service: serverless-test

plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs4.3
  environment:
    MONGO_URI: "mongodb://mongo-6:27000,mongo-7:27000,mongo-8:27000/db-dev?replicaSet=mongo"

How do I pass the MONGO_URI to contain the cluster as a comma separated value?

Any advise is much appreciated.

Zanon
  • 29,231
  • 20
  • 113
  • 126
khinester
  • 3,398
  • 9
  • 45
  • 88

1 Answers1

4

Unfortunately, you can't use commas in Lambda environment variables. This is an AWS limitation and not a Serverless issue.

For example, browse the AWS console and try to add a environment variable that contains a comma:

environment variable foo,bar

When you save, you will get the following error:

1 validation error detected: Value at 'environment.variables' failed to satisfy constraint: Map value must satisfy constraint: [Member must satisfy regular expression pattern: [^,]*]

The error message says that the regex [^,]* must be satisfied and what this small regex explicitly says is to not (^) accept the comma (,). Any other char is acceptable.

I don't know why they don't accept the comma and this is not explained in their documentation, but at least their error message shows that it is intentional.

As a workaround, you can replace your commas by another symbol (like #) to create the env var and replace it back to comma after reading the variable, or you will need to create multiple env vars to store the endpoints.

Zanon
  • 29,231
  • 20
  • 113
  • 126