serverless-offline
serverless-offline copied to clipboard
Aggregate multi services under one http server
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.
+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
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.
+1
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 check https://github.com/ngneat/nx-serverless
https://www.serverless.com/framework/docs/guides/compose is available with serverless v3.15
Any solution to use serverless-offline in monorepo architecture using same http server ? After test, it's not compatible with offline
It would be great to have offline support using the new serverless-compose available in v3.15
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) :
- 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', } } ], }, };
- 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;
- 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; }
- 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;
- After that, run
sls offline -c serverless.local.ts
- 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