Skip to content

Add AWS Lambda Layer Support #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 7, 2019

Conversation

david-mkl
Copy link
Contributor

This provides the option to place the requirements into a lambda layer.

2 additional options have been added:

  • inject: boolean. Controls whether the requirements should be injected into the build. Defaults to true
  • layer: boolean | object: Indicates that the requirements should not be injected into the build (will set inject = false when this option is present), and instead build separately to be used a lambda layer. Defaults false

When the layer option is provided as a boolean true, then some defaults will be chosen for the layer, and the layer will be added to the global layers object in serverless.yml.

When layer is provided as an object, then the values will be merged onto the defaults.

The layer will be created in the Cloudformation stack as PythonRequirementsLambdaLayer. In order to use the layer in a function, add the reference to the layer in the function's layers section. e.g.

functions:
  hello:
    handler: handler.hello
    layers:
      - {Ref: PythonRequirementsLambdaLayer}

Future Improvements/ Limitations of this implementation:

  • This will build a "global" lambda layer, i.e. there is no support for layers-per-lambda.
  • There is no way to control which requirements are placed in the layer. This will place all requirements into a single layer.

@rbu
Copy link

rbu commented Feb 5, 2019

@david-mk-lawrence Thanks for the PR, I was looking exactly for this feature myself.

What's the reason for creating two options though (inject and layers)? It looks to me there are only two setting that make sense:

  • layers:false, inject:true (the default)
  • layers:true, inject:false (use layers)

The two other "possible" settings (possible in terms of the serverless.yaml):

  • layers:true, inject:true -- inject will be overwritten to false by code
  • layers:false, inject:false -- apparently disables the plugin

I'd suggest you reduce it down to one option, "layers", which is false by default and when true(ish), it disables injection and enables layers.

Copy link
Contributor

@dschep dschep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the simplicity of this implementation, great job!!

The main changes I want made before I merge this:

  • remove the inject option, base all behavior on the layer option
  • add yourself to the contributors list!
  • respond to the 2 code comments (both are 100% open for discussion)

@david-mkl
Copy link
Contributor Author

@rbu Thanks for the feedback! Yes, that is a good point and I've removed that. Now the layer option will solely control the behavior.

The reason I put it there initially is because I found it very useful to prevent the injection from occurring while developing. But now that the layer is working, it shouldn't be needed.

@dschep dschep merged commit c7e4e9e into serverless:master Feb 7, 2019
@squaresurf
Copy link
Contributor

🎉

@dschep any idea when we can expect you to cut a new release with this and #308?

@dschep
Copy link
Contributor

dschep commented Feb 7, 2019

probably on monday 🙂

@rbu
Copy link

rbu commented Feb 11, 2019

@dschep That would make a great start of the week for me. Appreciated. Thanks for merging this so fast 🍻

@dschep
Copy link
Contributor

dschep commented Feb 11, 2019

🚀 Shipped v4.3.0!!!!

@SimonScholl
Copy link

I really like this feature that i can create layers for my lambda function that easily. The only thing i am missing is that i can just include one layers. Would it be possible to create more then one layer so i can distribute my dependencies over several functions in a modular way. The plugin already allows to define function specific requirements for multi function projects. :)

@felschr
Copy link

felschr commented Jun 12, 2019

Any plans on adding support for defining multiple layers and including only certain packages in a layer?
With all non-specified packages still being bundled with the lambda functions directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants