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

Add Serverless Container Support #723

Merged
merged 7 commits into from
May 6, 2021

Conversation

rogersgt
Copy link
Contributor

@rogersgt rogersgt commented Mar 12, 2021

What did you implement:

Closes #698

How did you implement it:

I added another function to validate.js to get the actual handler name from the function definition, now that serverless rolled out image for custom runtimes.

Review the following documentation: serverless documentation: https://www.serverless.com/blog/container-support-for-lambda. The only gap between feature support defined in the above article and this commit, is the ability to inherit the docker image's default command property. For example, serverless-webpack can't read the CMD property for the following Dockerfile:

# Dockerfile
FROM public.ecr.aws/lambda/nodejs:12
CMD "index.handler"

However you can simply set the function.image.command[0] property in serverless.yml as an override. I assume this would be the desired approach for most use cases, when sharing a docker image for all functions within the service.

# serverless.yml
service:
  ...
provider:
  ...
functions:
  myFunction1:
    image:
      name: public.ecr.aws/lambda/nodejs:12
      command:
        - 'index.handler1'
  myFunction2:
    image:
      name: public.ecr.aws/lambda/nodejs:12
      command:
        - 'index.handler2'

How can we verify it:

  • I added to the existing unit tests to check for a function definition with no handler property, and has image.command instead. Run npm run test to quickly validate test coverage:

Screen Shot 2021-03-12 at 10 31 13 AM

  • On my branch I went into serverless-webpack and did a yalc publish to locally mock the npm package.
  • I created a new serverless docker project with

Screen Shot 2021-03-12 at 10 47 11 AM

  • I opened the new project in my editor; I then ran the following:
npm init
npm install
npm install webpack serverless-webpack bestzip --dev
yalc link serverless-webpack # do a local install of my serverless-webpack branch
  • I added a plugins section with serverless-webpack:

Screen Shot 2021-03-12 at 10 52 08 AM

  • I added a command array to the existing image property for the function called hello:

Screen Shot 2021-03-12 at 10 53 42 AM

Note app.js contains a function called handler.

  • I added the following to a new file called webpack.config.js
const path = require('path');
const slsw = require('serverless-webpack');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  optimization: {
    minimize: false,
  },
  performance: {
    hints: false,
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
    sourceMapFilename: '[file].map',
  },
};
  • I then ran serverless package

Screen Shot 2021-03-12 at 11 03 08 AM

  • I then ran serverless deploy

Screen Shot 2021-03-12 at 11 05 54 AM

Screen Shot 2021-03-12 at 11 06 00 AM

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@j0k3r
Copy link
Member

j0k3r commented Mar 24, 2021

Thanks for that!
I need to find some times to review it.

@j0k3r j0k3r added this to the 5.5.0 milestone Apr 6, 2021
Copy link
Member

@j0k3r j0k3r left a comment

Choose a reason for hiding this comment

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

This looks good to me, thanks for adding tests & updating the README too.
As it's a new feature, I defined the milestone to 5.5.0.

I'll check for this milestone later if we can add more new features before merging it into master.
I also need to release 5.4.1 first.

@rogersgt
Copy link
Contributor Author

rogersgt commented Apr 6, 2021

This looks good to me, thanks for adding tests & updating the README too.
As it's a new feature, I defined the milestone to 5.5.0.

I'll check for this milestone later if we can add more new features before merging it into master.
I also need to release 5.4.1 first.

Thanks @j0k3r !

Copy link
Member

@j0k3r j0k3r left a comment

Choose a reason for hiding this comment

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

Will be shipped with 5.5.0 🚀

@j0k3r
Copy link
Member

j0k3r commented Apr 29, 2021

Can you rebase against the master so I'll be able to merge the PR? Thanks 🙏

@j0k3r j0k3r added the awaiting reply Awaiting for a reply from the OP label May 4, 2021
@rogersgt
Copy link
Contributor Author

rogersgt commented May 6, 2021

Can you rebase against the master so I'll be able to merge the PR? Thanks 🙏

Just updated! Let me know if you need anything else.

@flavianh
Copy link

flavianh commented Jan 5, 2022

I'm sorry @j0k3r I may be missing something, but how does the files resulting from the webpack bundle get into the container? It seems that you are bundling on one side, and writing the original app.js on the other.

@j0k3r
Copy link
Member

j0k3r commented May 8, 2022

@flavianh I don't know I've never used it.
Maybe you should check the Serverless doc or open a different issue with more information about what you expect, what you got, some code and config to check, etc.

@primosoft-dev-2
Copy link

primosoft-dev-2 commented Sep 15, 2023

I'm sorry @j0k3r I may be missing something, but how does the files resulting from the webpack bundle get into the container? It seems that you are bundling on one side, and writing the original app.js on the other.

@flavianh I have found a fix for this issue.
Instead of copying the whole code into the docker container while building an image, we can copy the zip file in the .serverless folder.
In the Dockerfile we can write something like below:

RUN yum install unzip -y

WORKDIR /var/task

COPY ./.serverless/{service-name}.zip ./

RUN unzip {service-name}.zip

RUN rm {service-name}.zip

@yuto-moriizumi
Copy link

I'm sorry @j0k3r I may be missing something, but how does the files resulting from the webpack bundle get into the container? It seems that you are bundling on one side, and writing the original app.js on the other.

@flavianh I have found a fix for this issue. Instead of copying the whole code into the docker container while building an image, we can copy the zip file in the .serverless folder. In the Dockerfile we can write something like below:

RUN yum install unzip -y

WORKDIR /var/task

COPY ./.serverless/{service-name}.zip ./

RUN unzip {service-name}.zip

RUN rm {service-name}.zip

This worked very well! I spent hours for finding this information... Shouldn't this be included in the documentation?

@j0k3r
Copy link
Member

j0k3r commented Oct 3, 2023

@yuto-moriizumi feel free to submit a PR to add it

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.

serverless-webpack support for new container image features in AWS Lambda and serverless
6 participants