serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

Aggregate multi services under one http server

Open ArielGueta opened this issue 2 years ago • 9 comments

I do not know if this is a feature request or a question, but what I want is the ability to use one server for multiple services. If our application is built as monorepo, and each service has its own serverless file, I would like to be able to run one server for all services.

What I need to do now is setting a different port for each.

Is there anyway to achieve this functionality?

Thanks.

ArielGueta avatar Apr 27 '22 14:04 ArielGueta

+1

Could be really interesting using the new Serverless Framework Compose (serverless-compose.yml) feature. Any planned support? https://www.serverless.com/blog/serverless-framework-compose-multi-service-deployments

maximang avatar Apr 29 '22 08:04 maximang

Support for Serverless Compose would be extremely nice. Running all the services offline won't be too much of an issue. The tricky part is connecting them. Especially if they share resources like an API Gateway.

fschaeffler avatar Jul 07 '22 18:07 fschaeffler

+1

semiautomatixfu avatar Jul 18 '22 04:07 semiautomatixfu

we do have a monorepo at the company I'm working, and we currently explore using @serverless/compose as well. I believe the intend of @serverless/compose is to eventually move the functionality into the main serverless framework, if I'm not mistaken. it might be best to wait until that happens. that said, we won't know until someone finds the time to look into it.

dnalborczyk avatar Aug 12 '22 14:08 dnalborczyk

@dnalborczyk check https://github.com/ngneat/nx-serverless

ArielGueta avatar Aug 12 '22 14:08 ArielGueta

https://www.serverless.com/framework/docs/guides/compose is available with serverless v3.15

dnalborczyk avatar Oct 02 '22 22:10 dnalborczyk

Any solution to use serverless-offline in monorepo architecture using same http server ? After test, it's not compatible with offline

throrin19 avatar Mar 09 '23 12:03 throrin19

It would be great to have offline support using the new serverless-compose available in v3.15

evanlarkin10 avatar Mar 10 '23 00:03 evanlarkin10

I made an issue in compose : https://github.com/serverless/compose/issues/175

After many tests, I find a "solution" (only tested with simple handlers) :

  1. In each subRepos I declare http functions in other file (like subRepoDir/serverless/api.ts)
    import type { Functions } from "serverless/aws";
    
    export default <Functions>{
        testOne: {
            handler: 'src/handler.hello',
            events: [
                {
                    http: {
                        method: 'get',
                        path: '/test-one',
                    }
                }
            ],
        },
    };
    
  2. I use this part in the serverless.ts of the subRepo :
    import type { Serverless } from 'serverless/aws';
    import { baseServerlessConfiguration } from '../../serverless.base';
    import api from './serverless/api';
    
    const stackServerlessConfiguration = <Serverless>{
        ...baseServerlessConfiguration,
        service: 'api-test-one',
        functions: {
            ...api,
        },
        custom: {
            'serverless-offline': {
                httpPort: 8085,
                noAuth: true,
            }
        }
    };
    
    module.exports = stackServerlessConfiguration;
    
  3. I create a function in my monorepo tolls directory used to change the handler path :
    import type { Functions } from "serverless/aws";
    import mapValues from 'lodash/mapValues';
    import { join } from 'node:path';
    
    export default (functions: Functions, path: string): Functions => {
        const modifiedFunctions = mapValues(functions, (func) => {
            if (func.hasOwnProperty('handler')) {
                // @ts-ignore
                func.handler = join(path, func.handler);
            }
    
            return func;
        });
    
        return modifiedFunctions;
    }
    
  4. I create a file serverless.local.ts at the root of my monorepo used only to launch all API functions using serverless-offline :
    import type { Serverless } from 'serverless/aws';
    import { baseServerlessConfiguration } from './serverless.base';
    import changeHandlers from './tools/offline/api';
    import testOneFunctions from './stacks/test-one/serverless/api';
    import testTwoFunctions from './stacks/test-two/serverless/api';
    
    const stackServerlessConfiguration = <Serverless>{
        ...baseServerlessConfiguration,
        service: 'api-offline',
        functions: {
            ...changeHandlers(testOneFunctions, 'stacks/test-one'),
            ...changeHandlers(testTwoFunctions, 'stacks/test-two'),
        },
        custom: {
            'serverless-offline': {
                httpPort: 8085,
                noAuth: true,
            }
        }
    };
    
    module.exports = stackServerlessConfiguration;
    
  5. After that, run sls offline -c serverless.local.ts
  6. Enjoy !

I know, the result is not ideal to try all functions in offline environment. But I think it's a start to try our monorepo project waiting serverless offline and compose are compatibles

throrin19 avatar Mar 10 '23 08:03 throrin19