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

Aggregate multi services under one http server #1386

Open
ArielGueta opened this issue Apr 27, 2022 · 9 comments
Open

Aggregate multi services under one http server #1386

ArielGueta opened this issue Apr 27, 2022 · 9 comments
Labels

Comments

@ArielGueta
Copy link

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.

@maximang
Copy link

+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

@fschaeffler
Copy link

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.

@semiautomatixfu
Copy link

+1

@dnalborczyk
Copy link
Collaborator

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.

@ArielGueta
Copy link
Author

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

@dnalborczyk
Copy link
Collaborator

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

@throrin19
Copy link

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

@evanlarkin10
Copy link

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

@throrin19
Copy link

throrin19 commented Mar 10, 2023

I made an issue in compose : serverless/compose#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants