Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cli lambda `update-function-configuration` deletes existing environment variables

The documentation on the AWS cil lambda states that

...You provide only the parameters you want to change...

Which I assume means that the rest of the settings would still remain the same. However, say my lambda function has environment variables :

var1=old_val1
var2=old_val2
var3=old_val3

and then when I try doing something like this :

aws lambda update-function-configuration --function-name dummy_fun --environment '{"Variables":{"var1":"new_val1","var2":"new_val2"}}'

with the intention of updating the variables : var1 and var2 with the new values new_val1 and new_val2 respectively, although these 2 variables DO get updated, but the third one, var3, gets deleted !

Am I doing something wrong ? Or is there a way to make sure this doesn't happen?

I can definitely handle it using a workaround wherein I can fetch the current config and then update the env variables locally and then push the entire updated config, all of this through a python code etc. But, is that the only way to do it ? Or can there be a simpler way of doing it?

like image 447
qre0ct Avatar asked Sep 28 '18 05:09

qre0ct


People also ask

Can Lambda update environment variables?

You can't change the configuration (which includes environment variables) or function code in a published Lambda function version. To change the environment variables of a published Lambda function version, you must first change the current, unpublished function version ($LATEST). Then, publish a new function version.

Do lambda functions save memory?

Starting today, you can allocate up to 10 GB of memory to a Lambda function. This is more than a 3x increase compared to previous limits. Lambda allocates CPU and other resources linearly in proportion to the amount of memory configured. That means you can now have access to up to 6 vCPUs in each execution environment.


1 Answers

It seems not easy to partially update the environment variables for a lambda with awscli.

But with the usage of the built-in JSON-based client-side filtering that uses the JMESPath syntax, I found a way to achieve what I needed to do.

NEW_ENVVARS=$(aws lambda get-function-configuration --function-name your-func-name --query "Environment.Variables | merge(@, \`{\"ENV_VAR_TO_UPDATE\":\"value_here\"}\`)")

aws lambda update-function-configuration --function-name your-func-name --environment "{ \"Variables\": $NEW_ENVVARS }"

Of course, you can update more than one environment variable with that trick.

like image 190
Pierre Maoui Avatar answered Sep 24 '22 18:09

Pierre Maoui