Skip to content

(aws-lambda): Layer updates not creating new function version #19098

@varju

Description

@varju

What is the problem?

If my stack contains a Lambda LayerVersion and a Lambda Function with an alias pointing to the newest version, content changes within the layer do not trigger deployment of a new Function version.

Reproduction Steps

Example stack demonstrating the problem:

export class CdkLayerExampleStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const layer = new lambda.LayerVersion(this, 'MyLayer', {
      code: lambda.Code.fromAsset('layer-code'),
      compatibleRuntimes: [lambda.Runtime.NODEJS_14_X],
      description: 'A layer to test the L2 construct',
    });

    const fn = new lambda.Function(this, 'MyLayeredLambda', {
      code: lambda.Code.fromAsset('function-code'),
      handler: 'index.handler',
      runtime: lambda.Runtime.NODEJS_14_X,
      layers: [layer],
    });

    fn.currentVersion.addAlias('live');
  }
}

The layer contents, defined in layer-code/nodejs/node_modules/layer.js:

exports.getLayerVersion = function() {
  return 'V1';
}

The Lambda function, defined in function-code/index.js:

const layer = require('layer');

exports.handler = async function(event, context) {
  const functionVersion = 'V1';
  const layerVersion = layer.getLayerVersion();

  return {
    functionVersion,
    layerVersion,
  }
};

In cdk.json:

{
  "app": "npx ts-node --prefer-ts-exts bin/cdk-layer-example.ts",
  "context": {
    "@aws-cdk/core:newStyleStackSynthesis": "true",
    "@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
    "@aws-cdk/core:enableStackNameDuplicates": "true",
    "aws-cdk:enableDiffNoFail": "true",
    "@aws-cdk/core:stackRelativeExports": "true",
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
    "@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
    "@aws-cdk/aws-kms:defaultKeyPolicies": true,
    "@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
    "@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true,
    "@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
    "@aws-cdk/aws-efs:defaultEncryptionAtRest": true,
    "@aws-cdk/aws-lambda:recognizeVersionProps": true,
    "@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true
  }
}

What did you expect to happen?

Making content changes within layer-code/nodejs/node_modules/layer.js should trigger deployment of a new layer version and new function version, updating the live alias to point to the new function version.

What actually happened?

A new layer version was created, as expected, but a new function version was not created. Invoking the function directly shows that the new layer version is being used, but invoking the function alias shows the old layer version is still in use.

CDK CLI Version

1.143.0 (build bebd295)

Framework Version

No response

Node.js Version

v16.13.2

OS

macOS 12.2

Language

Typescript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-lambdaRelated to AWS LambdabugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions