How to automatically put environment variable to all lambdas created in my CDK project

只谈情不闲聊 提交于 2020-03-04 19:39:50

问题


We implement almost all our lambda's in Chalice and then consume them in CDK project as described here.

I need to add same environment variable to all lambda's in stack (different per developer). I would like to do it automatically, and not count on every developer adding variable to Chalice stage configuration. I can't use AWS Systems Manager Parameter Store, because it's the same for all dev stacks - we share the same AWS account. I can parse final cloudformation template before deploy and add variables to all lambda's, but may be there is a more elegant way ?

Thank you.


回答1:


You should have a look at Aspects.

Aspects are the way to apply an operation to all constructs in a given scope. The functionality could modify the constructs, such as by adding tags, or it could be verifying something about the state of the constructs, such as ensuring that all buckets are encrypted.

@jsii.implements(core.IAspect)
class EnvVarSetter:

  def visit(self, node):
    # See that we're dealing with a Function
    if isinstance(node, lambda.Function):
      # set env var here
      node.add_environment('KEY', 'VALUE')

# Apply to the stack
stack.node.apply_aspect(EnvVarSetter())


来源:https://stackoverflow.com/questions/60269108/how-to-automatically-put-environment-variable-to-all-lambdas-created-in-my-cdk-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!