question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[lambda] deployment failure on updates to cross-stack layers

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:34
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

14reactions
alexdilleycommented, Aug 15, 2019

@eladb I’ve worked around this by using a SSM parameter store to reference the latest layerVersionArn and then accessing the layer instance via Layer.fromLayerVersionArn(). (Nod to @rhboyd for this idea.)

I wrote a custom construct to act as such a proxy:

const cdk = require('@aws-cdk/core');
const lambda = require('@aws-cdk/aws-lambda');
const ssm = require('@aws-cdk/aws-ssm');

// Proxy to the LATEST version of the Lambda Layer shared between stacks via a
// SSM paramter store.
module.exports = class SharedLayer extends cdk.Construct {
  static parameterName(construct) {
    return `/${cdk.Stack.of(construct).stage}/shared/layerVersionArn`;
  }

  static of(construct) {
    const layerVersionArn = ssm.StringParameter.valueForStringParameter(
      construct,
      this.parameterName(construct)
    );

    return lambda.LayerVersion.fromLayerVersionArn(
      construct,
      'SharedLayer',
      layerVersionArn
    );
  }

  constructor(scope, id, props) {
    super(scope, id, props);

    new ssm.StringParameter(this, 'VersionArn', {
      parameterName: SharedLayer.parameterName(this),
      stringValue: props.layerVersionArn,
    });
  }
};
6reactions
ranguardcommented, Jul 6, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update Cross-Stack AWS Lambda Layers - GnomezGrave
Let's assume we deploy the Lambda layer for the first time. So it's ARN will have :1 at the end denoting the Layer...
Read more >
Troubleshoot deployment issues in Lambda
When you update your function, Lambda deploys the change by launching new instances of the function with the updated code or settings. Deployment...
Read more >
CDK tips, part 3 – how to unblock cross-stack references
I think the correct way is still to do the change in 2 steps ("Update and reference the new layer in all lambdas....
Read more >
b-cfn-lambda-layer 2.4.1 - PyPI
If we do, the next update to the source code in the lambda layer would result in a failed deployment. This is because...
Read more >
amazon cloudformation - Updating dependent stacks
When I try to update stack-layer , I get an error: Export layer-arn cannot be deleted as it is in use by stack-lambda...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found