localstack / serverless-localstack

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

Fine-grained hot reloading configuration for functions and layers to replace global `mountCode` option #223

Open joe4dev opened 1 year ago

joe4dev commented 1 year ago

The current hot reloading configuration mountCode has many limitations (e.g., global config, no layer support). An improved configuration API and documentation could make hot reloading more powerful and easier to use with the Serverless Framework.

Related Issues/PRs

Challenges

Background

User Stories

Enable hot reloading for:

  1. Single function code
  2. Single function code + single layer
  3. Single layer
  4. Two functions sharing the same hot-reloaded layer

These cases are supported by LocalStack but not by the serverless-localstack plugin.

Possible API

service: myService

plugins:
  - serverless-localstack

custom:
  localstack:
# DEPRECATED global config
#    lambda:
#      mountCode: true
    stages:
      - local
    host: http://127.0.0.1

provider:
  name: aws
  region: us-east-1
  runtime: python3.9
  # OPTIONAL provider-scoped config here (would need to think about function vs. layers scope)

# https://www.serverless.com/framework/docs/providers/aws/guide/layers
layers:
  layerOne:
    path: layerOne
    hotReload: true

# https://www.serverless.com/framework/docs/providers/aws/guide/functions
functions:
  hello:
    handler: handler.handler
    hotReload: true
    layers:
      - !Ref LayerOneLambdaLayer

Considerations

  1. Hot reloading for layers is configured per layer (rather than per layer usage) and applies to all functions using the layer
  2. LocalStack (currently) support at most one layer in total when using hot reloading for a layer. This makes it difficult to apply hot reloading globally for layers.
  3. Should we consider supporting ZIP files for hot reloading such that we can re-use build steps from tools such as Serverless, Gradle, etc rather than adding extra steps to unzip these archives (Java Gradle example).

Alternatives to create layers with Serverless

Based on https://www.tutorialspoint.com/serverless/serverless_layer_creation.htm

  1. Creating python-requirements layer
  2. Using an existing layer from another function in the same region
  3. Non-requirements/ generic layers (example: https://www.serverless.com/blog/publish-aws-lambda-layers-serverless-framework)
dominikschubert commented 3 weeks ago

Another user story for this would be having both image-based and zip-based lambda functions and we don't want the image-based functions to have the S3Uri etc. fields