dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 796 forks source link

Saved code change is not hot reloaded #1654

Closed Michael-Xie closed 1 year ago

Michael-Xie commented 1 year ago

Bug Report

Current Behavior

I have a lambda that returns a response by providing a POST request with specific parameters in the body. When I make a small code change, like renaming a variable or calculation within one line, the change won't be in effect when I invoke the lambda.

Sample Code

const serverlessConfiguration: AWS = {
    service: 'portal-lambda',
    frameworkVersion: '3',

    custom: {
        webpack: {
            webpackConfig: './webpack.config.js',
            includeModules: true,
        },
    },
    plugins: ['serverless-webpack', 'serverless-offline'],
    provider: {
        name: 'aws',
        runtime: 'nodejs16.x',
        timeout: 30,

        vpc: {
            securityGroupIds: ['${file(./secrets.json):SECURITY_GROUP}'],
            subnetIds: [
                '${file(./secrets.json):SUBNET1}',
                '${file(./secrets.json):SUBNET2}',
            ],
        },
        apiGateway: {
            minimumCompressionSize: 1024,
            shouldStartNameWithService: true,
        },
        environment: {
                    // key value pairs of env variables
        },
    },
    // import the function via paths
    functions: {
        // functions
    },
    resources: {
        // resources
    },
};
{
    "extends": "./tsconfig.paths.json",
    "compilerOptions": {
        "lib": ["ESNext"],
        "moduleResolution": "node",
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "removeComments": true,
        "sourceMap": true,
        "target": "ES2020",
        "outDir": "lib",
        "esModuleInterop": true
    },
    "include": ["src/**/*.ts", "serverless.ts"],
    "exclude": [
        "node_modules/**/*",
        ".serverless/**/*",
        ".webpack/**/*",
        "_warmup/**/*",
        ".vscode/**/*"
    ],
    "ts-node": {
        "require": ["tsconfig-paths/register"]
    }
}
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@functions/*": ["src/functions/*"],
      "@libs/*": ["src/libs/*"]
    }
  }
}

Expected behavior/code

I expect the saved code changes can be observed the next time I invoke the function containing them. Now, I have to restart sls offline to see the changes.

Environment

terozio commented 1 year ago

Noticed the same behaviour when I upgraded one older function from serverless-offline 8.8.x to 12.0.x

Michael-Xie commented 1 year ago

Noticed the same behaviour when I upgraded one older function from serverless-offline 8.8.x to 12.0.x

This issue occurred when I finished updating my packages too.

NickParks commented 1 year ago

Same issue here after updating

IsaaX commented 1 year ago

Use the reloadHandler flag.

I personally not sure what the considerations are around the flag but I'd prefer this to be enabled by default?

Just an FYI i'm also using serverless-webpack so that might why there's additional config reloadHandler

robin-glimp commented 1 year ago

Use the reloadHandler flag.

I'm using serverless-esbuild and this works in my case as well 🎉 ! Just sometimes I need to call the API 2 times for the reload to trigger.

To be clear I am starting the serverless-offline cli service with this command now:

Note! It does make the API call significantly slower: I have a function that scans a small DynamoDB table and returns the filtered results. This takes ~230ms for each call using the reloadHandler flag. If I dont use that flag it takes ~25ms on subsequent calls. But this tradeoff is so much worth it for being able to hot reload the code!

terozio commented 1 year ago

Use the reloadHandler flag.

Thanks! Missed that one in the release notes.

Michael-Xie commented 1 year ago

Use the reloadHandler flag.

I'm using serverless-esbuild and this works in my case as well tada ! Just sometimes I need to call the API 2 times for the reload to trigger.

To be clear I am starting the serverless-offline cli service with this command now:

  • sls offline start --reloadHandler

Note! It does make the API call significantly slower: I have a function that scans a small DynamoDB table and returns the filtered results. This takes ~230ms for each call using the reloadHandler flag. If I dont use that flag it takes ~25ms on subsequent calls. But this tradeoff is so much worth it for being able to hot reload the code!

Thanks this worked~! :+1: