localstack / serverless-localstack

⚡ Serverless plugin for running against LocalStack
511 stars 82 forks source link

Fix: deploy layers when hot reload enabled #222

Closed joe4dev closed 1 year ago

joe4dev commented 1 year ago

Layers are not deployed if hot reloading is enabled through mountCode: true. In the Serverless AWS provider, Lambda functions and layers are deployed together (uploadFunctionsAndLayers) but serverless-localstack has currently many limitations with hot reloading (e.g., global config, no layer support). This PR provides a workaround to unblock customers who want to hot-reload functions and regularly deploy layers.

Root cause

Hot reloading (i.e., mountCode) in serverless-localstack breaks any Lambda layers with the new Lambda provider:

custom:
  localstack:
    lambda:
      mountCode: true

When trying to deploy a Lambda function with a layer, it fails with an OperationalError:

OperationalError: ENOENT: no such file or directory, open '/Users/joe/Projects/LocalStack/issues/support_aaporomu_u04ba3n4bam/serverless-lambda-layers-minimal/.serverless/layerOne.zip'

When mountCode is enabled, layers do not get packaged and deployed: Skip plugin function Package.packageService (lambda.mountCode flag is enabled)

Therefore, the deployment fails.

Challenges

Background

Workaround

Removing the skipIfMountLambda circumvents the issue at the expense of slower initial deployment (with potential other deployment-related side-effects):

Testing

Side-effects

To fix this issue, we need to think about a new fine-grained API for hot reloading and handle selective deployments of all layers and functions that are not hot reloading.

Questions

  1. Is there a dependency of AwsCompileFunctions.downloadPackageArtifacts ⇒ package (i.e., can we safely skip packaging without downloadPackageArtifacts?
  2. Any other dependencies or implicit compatibility issues (e.g., related to other serverless plugins)?