问题
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