Skip to content
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

Allow packaging of static files alongside binary #41

Open
dancoates opened this issue May 14, 2019 · 12 comments
Open

Allow packaging of static files alongside binary #41

dancoates opened this issue May 14, 2019 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@dancoates
Copy link

💡 Feature description

It would be nice if it was possible to use the include and exclude package configuration in serverless.yml to choose other files to bundle with the bootstrap binary. For example to include a .env file that is then read by the dotenv crate at runtime.

I am sure that there are other use cases for including files other than the binary but that is the one that sprang to mind.

@softprops
Copy link
Owner

softprops commented May 16, 2019

That's a good point. I've deferred in adding features until they are needed or asked for. I'll look into this

@nosideeffects
Copy link

I was under the impression that this was already supported. How would I add this capability myself for my current serverless project?

@nickspell
Copy link

nickspell commented May 29, 2019

An alternative solution to the static file packaging problem is to use AWS Lambda Layers.
Just put your static files into some directory at the root of your project and create a layer from that.

Example:

myservice
├── static
│   ├── foo.json
├── src
├── serverless.yml

In your serverless.yml insert:

layers:
  mydata:
    path: static
    description: My static json data

In your function you can put the dependency on your layer as a title cased reference:

functions:
  myfunction:
    handler: #...
    layers: 
       - {Ref: MydataLambdaLayer}
     # .....

You can now access your files as if they are located in the "/opt" folder of your lambda, e.g.:

let file = File::open("/opt/foo.json")?;

This solution is quite useful especially when you want to share your static data between different lambdas.

@cecton
Copy link

cecton commented Aug 6, 2019

I think we could use the "include" attribute of a function and "add" the files to the zip after the build in the container succeeded.

@vim-zz
Copy link

vim-zz commented Nov 12, 2019

Adding another use case, which requires having a certificate file to communicate with a certain server.

@sachaarbonel
Copy link

sachaarbonel commented Apr 6, 2020

I just wanted to point out that the workaround of @nickspell doesnt work for my use case which is opening a sqlite db. For some reason I'm getting a SqliteError "attempt to write a readonly database". I guess the /opt folder is read only. EDIT : nevermind I copied the file to the /tmp folder (which is the only writable folder in aws lambda) and it did the trick.

@softprops softprops added the help wanted Extra attention is needed label Jun 4, 2020
@softprops
Copy link
Owner

I think the most appropriate way to approach this is the suggestion original mentioned. Use the built in include / exclude packaging options.

@kahlil29
Copy link

kahlil29 commented Jun 4, 2020

@softprops Could you tell me if it would be possible to use a rust program that has an OpenCV dependency?
I'm currently using this crate
Any thoughts about this? I'm guessing I need to introduce the opencv files (statically linked?) into the Lambda somehow?

@softprops
Copy link
Owner

This looks a bit involved but possibly doable. A lambda layer might be a useful approach here. Heres an example from the python world of setting up a layer https://www.bigendiandata.com/2019-04-15-OpenCV_AWS_Lambda/

I'm not familiar with the opencv project enough myself to know why there might not be an off the shelf layer available https://github.com/mthenw/awesome-layers

@kahlil29
Copy link

kahlil29 commented Jun 4, 2020

Thanks for the informative and helpful reply @softprops
I had tried earlier (not using Layers) but could not get it working.
I guess it's time to give Lambda Layers a go.
So in your experience, it's common/acceptable to use Lambda Layers to package in dependencies for Lambdas? It's known to work without (many) issues?

@softprops
Copy link
Owner

Without more expertise in opencv I cant gaurantee this will work but to answer the question about using layers to package host dependencies, that's almost the sole use of layers https://aws.amazon.com/blogs/aws/new-for-aws-lambda-use-any-programming-language-and-share-common-components/

That said this is likely an easier case for interpreted languages im not sure if you can package and dynamically link via env var with rust. I glanced through the opencv docs and there was some suggestions that you could

@kahlil29
Copy link

kahlil29 commented Jun 4, 2020

Thanks a lot @softprops
Appreciate the answer and info 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

8 participants