简体   繁体   English

aws cli lambda `update-function-configuration` 删除现有环境变量

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

The documentation on the AWS cil lambda states that AWS cil lambda上的文档指出

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

Which I assume means that the rest of the settings would still remain the same.我认为这意味着设置的 rest 仍将保持不变。 However, say my lambda function has environment variables:但是,假设我的 lambda function 有环境变量:

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 !目的是更新变量: var1var2分别使用新值new_val1new_val2 ,虽然这两个变量确实得到了更新,但是第三个变量var3被删除了!

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?我绝对可以使用一种解决方法来处理它,其中我可以获取当前配置,然后在本地更新 env 变量,然后推送整个更新的配置,所有这些都通过 python 代码等。但是,这是唯一的方法吗? Or can there be a simpler way of doing it?或者可以有更简单的方法吗?

It seems not easy to partially update the environment variables for a lambda with awscli .使用awscli部分更新 lambda 的环境变量似乎并不容易。

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.但是通过使用内置的基于 JSON 的客户端过滤(使用JMESPath语法),我找到了一种方法来实现我需要做的事情。

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.当然,您可以使用该技巧更新多个环境变量。

You are misinterpreting the intention of the documentation. 您误解了文档的用意。

You provide only the parameters you want to change. 您只提供要更改的参数。

--environment is the (singular) "parameter" that you are specifying should be changed -- not the individual variables. --environment是您指定的(单数)“参数”应该更改 - 而不是单个变量。

The environment variables are configured en bloc so there is no concept of specifying only certain environment variables that you want to be different. 环境变量是整体配置的,因此没有概念只指定您想要的不同的某些环境变量。

aws lambda update-function-configuration --function-name my-function-name --environment Variables="{VAR1=variable_value, VAR2=variable_value}" 

描述:Above Command将更新aws中lambda函数的环境变量。

I had the same problem where I wanted to update only one env variable of a function and not touch the rest. 我有同样的问题,我想只更新一个函数的一个env变量而不是触及其余的。

I ended up writing a script in node and publishing it: 我最终在节点中编写脚本并发布它:

https://www.npmjs.com/package/aws-lambda-update-env https://www.npmjs.com/package/aws-lambda-update-env

It is pretty simple to use: 使用起来非常简单:

update-lambda-env KEY "My New Test Value" --stack-name myApplicationStack

This will only change the variable KEY in the functions located in the stack myApplicationStack 这只会更改堆栈myApplicationStack中函数的变量KEY

A better solution might be to use AWS Parameter Store if your variable is going to change often. 如果您的变量经常更改,则更好的解决方案可能是使用AWS参数存储。

https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM