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

Lambda Custom Runtime via Docker Support #1246

Closed
wants to merge 11 commits into from

Conversation

rogersgt
Copy link

@rogersgt rogersgt commented Jun 18, 2021

Description

AWS Lambda introduced container support for custom runtimes (see https://www.serverless.com/blog/container-support-for-lambda). The serverless configuration prevents you from using a handler property in your function definitions when you use the image property. Currently serverless-offline will result in the following error using the aws-nodejs-docker serverless template with the latest serverless-offline@7.0.0 and serverless-webpack@5.5.1.
Screen Shot 2021-06-18 at 1 46 43 PM

Motivation and Context

See my related contribution to serverless-webpack: serverless-heaven/serverless-webpack#723
Screen Shot 2021-06-18 at 1 44 48 PM

How Has This Been Tested?

  • I started by locally leveraging yalc to mock published changes on my branch.
    Screen Shot 2021-06-18 at 1 49 48 PM

  • I then created a new serverless project via a template.
    Screen Shot 2021-06-18 at 1 51 04 PM

  • From the test project directory, I then added dev dependencies.
    Screen Shot 2021-06-18 at 1 59 25 PM

  • Next I linked my local serverless-offline package to my test project with yalc
    Screen Shot 2021-06-18 at 2 03 11 PM

  • I added a new file called webpack.config.js

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  optimization: {
    // We no not want to minimize our code.
    minimize: false,
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false,
  },
  devtool: 'source-map',
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          },
        ],
      },
    ],
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
    sourceMapFilename: '[file].map',
  },
};
  • Next I added a .babelrc file for babel
{
  "comments": false,
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "14"
        }
      }
    ]
  ]
}
  • I then registered the serverless plugins by modifying the serverless.yml to include a plugins section
    Screen Shot 2021-06-18 at 1 57 16 PM

  • I also modified the functions section of serverless.yml to include the image.command property (required by serverless-webpack) and an HTTP event.
    Screen Shot 2021-06-18 at 2 06 59 PM

  • In the app.js file I converted the module.exports syntax to export const.
    Screen Shot 2021-06-18 at 2 01 17 PM

  • Finally I ran yarn sls offline start and then ran a curl command against the example HTTP route.
    Screen Shot 2021-06-18 at 1 58 10 PM
    Screen Shot 2021-06-18 at 2 07 19 PM
    Screen Shot 2021-06-18 at 2 07 34 PM
    Screen Shot 2021-06-18 at 2 07 45 PM

  • Unit Test Results:
    Screen Shot 2021-06-18 at 2 10 07 PM

  • E2E Test Results
    Screen Shot 2021-06-18 at 2 16 28 PM

@timborden
Copy link

timborden commented Mar 18, 2022

Hey @rogersgt, thanks for putting up this PR. I'm trying to use your fork.

package.json:

  "devDependencies": {
    "serverless": "^3.7.9",
    "serverless-offline": "rogersgt/serverless-offline.git#grady/container-support"
  }

...but I'm getting a Serverless plugin "serverless-offline" not found. Make sure it's installed and listed in the "plugins" section of your serverless config file. Run "serverless plugin install -n serverless-offline" to install it. error.

How do I set up your version of serverless-offilne in my serverless.yml file?

UPDATE
serverless.yml:

plugins:
    - ./node_modules/serverless-offline/src/ServerlessOffline.js

Currently getting the following NPM error:

npm ERR! Found: serverless@3.7.9
npm ERR! node_modules/serverless
npm ERR!   dev serverless@"^3.7.9" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer serverless@"^1.60.0 || 2" from serverless-offline@8.2.0
npm ERR! node_modules/serverless-offline
npm ERR!   dev serverless-offline@"rogersgt/serverless-offline.git#grady/container-support" from the root project

....thinking about forking the source repo and cherry-picking your commits 🤔

eek added a commit to eek/serverless-offline that referenced this pull request Mar 23, 2022
@dherault
Copy link
Owner

Thanks @rogersgt , could you rebase please?

@rogersgt
Copy link
Author

@dherault is there anything else needed from me on this?

@dnalborczyk
Copy link
Collaborator

thank you for your PR @rogersgt please hold on for a little bit, I'm trying to get v9 out of the door which should hopefully happen very soon. then we can pull in contributions.

@rogersgt rogersgt closed this Sep 10, 2022
@rogersgt
Copy link
Author

I'm struggling to keep these changes up to date. Pulling latest broke all the tests, so I think it would make more sense to re-branch and open a new PR.

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.

4 participants