How to make serverless not changing the value of environment variable?


#1

I’m trying to add environment variable to my function in Serverless config but set value for it using Lambda console to not store sensitive data in repository. The problem is: on every deployment of the function Serverless overrides value for the environment variable I set in console.
I have no value for the variable in Serverless config, only the name.
Is this possible to make it ignore value of variable?


#2

Have you tried removing the environment variable from the serverless.yml and just setting it in the console? Putting the name into the serverless config is like saying it’s value is an empty string.


#3

I’m sure it will work but was hoping it’s possible to add variables using Serverless. Seems like it’s not.


#4

In order to get around this type of issue, I use tools like Bitbucket Pipelines or Gitlab’s CI/CD runners to store data like this. You can add an environment variable to Bitbucket or Gitlab manually and reference that in your serverless.yml so that you end up using the environment variable you inserted into Bitbucket/Gitlab instead and never need to log into AWS to set it


#5

Great suggestion. Can you please show how can I reference that value in yml file?


#6

What you do is set the environment variable in Bitbucket or Gitlab depending on which you use. Then in your serverless.yml you are referencing an environment variable so:

  environment:
    AN_ENV_VARIABLE_HERE: ${env:ENV_VAR_IN_BITBUCKET_OR_GITLAB}

Then when you deploy AN_ENV_VARIABLE will be available to your lambda with the value you used in your CI/CD build process.

Hope that’s clear enough.