Skip to main content

Add a new Serverless service

Generate a new service

Let's add a new users service to the project by running the following command at the root of the repository:

pnpm generate-service users
info

The generate-service command is a shorthand for the following command defined in the root package.json file:

pnpm nx generate @swarmion/nx-plugin:service

To have more info on the @swarmion/nx-plugin, you can check its documentation page.

The services/users folder containing the users service has been added. The project dependency graph now looks like the following:

Swarmion app with users serviceSwarmion app with users service

Deploy the service

By deploying the service, the following architecture will be deployed:

users architecture

The service defines a lambda called my-project-name-users-dev-health. A new API Gateway is deployed for the users service, decoupling it from core. It exposes a new endpoint GET /users/health which is an integration with the Lambda. Calling the endpoint will execute the Lambda and return the result.

You can find the definition of the lambda in the services/users/functions/health/config.ts file.

You can deploy the service by running the following command at the root of the repository:

cd services/users
pnpm serverless deploy
# It should prompt something like the following:
Deploying my-project-name-users to stage dev (us-east-1)

✔ Service deployed to stack my-project-name-users-dev (116s)

endpoint: GET - https://b1c15ee1u8.execute-api.us-east-1.amazonaws.com/users/health
functions:
health: my-project-name-users-dev-health (933 B)

Calling the endpoint will return the following response:

http https://b1c15ee1u8.execute-api.us-east-1.amazonaws.com/users/health
HTTP/1.1 200 OK
Apigw-Requestid: XejEWgsdoAMEV8w=
Connection: keep-alive
Content-Length: 2
Content-Type: application/json
Date: Fri, 26 Aug 2022 15:22:54 GMT

ok

Deploy the whole project in order

Now that the project has two services, it would be really cumbersome to deploy them one after the other. Thankfully, we can deploy the whole project in order by running the following command at the root of the repository:

pnpm run deploy

This command will leverage the dependency graph of the project and deploy all the services in the right order. In our example, there is no dependency between our services, which means that core and users will be deployed in parallel.

Wrap up

You now have deployed an application with two microservices: core and users. Let's take a look on how to add a shared TypeScript library to the project, to allow code sharing between the two services.