AWS Lambda Layers - Node Modules not working

layers:
  AnalyticsShared:
    path: node_modules
    name: analytics-shared-${opt:stage, self:provider.stage}
    description: "Shared node modules for analytics"

functions:
  track:
    handler: analytics.queue
    timeout: 30
    layers:
      - {Ref: AnalyticsSharedLambdaLayer}

This way of writing is correct.“AnalyticsShared:
This part doesn’t always work correctly though Serverless Framework says as following in AWS - Layers:Using your layers.

To use a layer with a function in the same service, use a CloudFormation Ref. The name of your layer in the CloudFormation template will be your layer name TitleCased (without spaces) and have LambdaLayer appended to the end. EG:

  • wrong
layers:
  test:
    path: layer
functions:
  hello:
    handler: handler.hello
    layers:
      - { Ref: TestLambdaLayer }
  • right
layers:
  Test:
    path: layer
functions:
  hello:
    handler: handler.hello
    layers:
      - { Ref: TestLambdaLayer }

This phenomenon may happen when layers name is equal to service name. That’s because layers name create zip file (e.g.Test.zip) as well as service name create zip file (e.g.test.zip) when sls deploy with cloudformation info.