aws-cdk: [lambda] deployment failure on updates to cross-stack layers
Here is the application structure:
- Stack A:
- lambda layer
- Stack B: Depends on A:
- Lambda which is using the layer
new lambda.Function(this, 'some-lambda'
{
...
layers: [stackA.lambdaLayer]
}
)
where stackA.lambdaLayer is an instance of lambda.LayerVersion.
The initial deployment works well. However, when the layer code is changed and the second deployment takes place, Stack A deployment fails with error message:
lambdas-layer (…) Requested update requires the creation of a new physical resource; hence creating one. Resource creation Initiated dev-StackA Export dev-CoreStack:corelambdaslayerLayerVersionArn261FB3DB cannot be updated as it is in use by dev-StackB
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 37
- Comments: 20 (10 by maintainers)
Commits related to this issue
- feat: remove lambda_layers stack and bundle layers directly in stacks This is to avoid open bug: https://github.com/aws/aws-cdk/issues/1972 — committed to shaftoe/api-l3x-in by shaftoe 4 years ago
- fix: implemented workaround for https://github.com/aws/aws-cdk/issues/1972 — committed to ryparker/aws-cdk-sample-lambda-layers by ryparker 3 years ago
@eladb I’ve worked around this by using a SSM parameter store to reference the latest
layerVersionArnand then accessing the layer instance viaLayer.fromLayerVersionArn(). (Nod to @rhboyd for this idea.)I wrote a custom construct to act as such a proxy:
I split the layers into a stack of their own, and share the ARN via SSM.Params, to then be used by a lambda in another stack. So no hard dependency…
You can see both the layer creation and then usage (which would usually be in a separate stack)
https://github.com/ranguard/cdk-talk-examples/tree/master/code/lambda_layer_example/lib
We have a ‘layers’ stack
AWS keeps layer versions around, whilst any lambda is pointing at them - so new layer deploys won’t break existing functions - but you do need to re-deploy any stack that is using the layers
@fabio-vitali
The code @alexdilley wrote breaks down into:
Creating layer
Using layer:
@fabio-vitali That solutions works by breaking the coupling between one stack’s export value and another stack’s import value (which is enforced by cloudformation). Instead they are using one stack to put a value into SSM Parameter store then another stack is reading values from the same SSM Parameter store.
@nija-at yeah, personally I’ve worked around that not sharing layers between stacks, in the end it’s not that bad with the little helper function utility and folder structure, so I define the layer once for the project and I create a new one per stack whenever needed. Easier showed than said:
Thanks to you @lapair I try that solution, and it works! This adjusts perfectly with my idea of separate layers in their stack