Introducing multi-service deployments via Serverless Framework Compose
Tags: serverless, serverlessframework, serverlesscompose, yaml
Published: 2022-04-20 16:43:53

Originally posted at [Serverless](https://www.serverless.com/blog/serverless-framework-compose-multi-service-deployments)

![](https://cdn-images-1.medium.com/max/5200/0*W3gujvtQTdEpObS8.png)

We are excited to announce Serverless Framework Compose: a new feature enabling you **to deploy multiple services in one command, in parallel, or ordered by dependencies!**

To use this new feature, [upgrade the serverless CLI](https://www.serverless.com/framework/docs/getting-started#upgrade) to v3.15 or greater and follow the guide below.

## Composing multiple Serverless Framework services

Deploying multiple services in a monorepository is a very common pattern across larger teams. Orchestrating these deployments can become painful.

Let’s take the following (simple) example project:
{% gist https://gist.github.com/richardgrantserverless/03f42b09dc41d7483ff0227a6ec89b15.js %}
Our application contains 2 services. Each service can be deployed separately by running serverless deploy in each folder.

With Serverless Framework Compose, it is possible **to deploy all those Serverless Framework services via a single serverless deploy command.** To do so, create a serverless-compose.yml file at the root:
{% gist https://gist.github.com/richardgrantserverless/54a553dc8a0ab9241f4eefa0c88d3299.js %}
The new “serverless-compose.yml” configuration file references the existing Serverless Framework projects:
{% gist https://gist.github.com/richardgrantserverless/6efe4b79c1ea91bcc2b5c74ace23182c.js %}
As you can see above, each existing Serverless Framework service is referenced via a relative path. When running “serverless deploy” in the root directory, Compose will deploy both services (“products” and “orders”) in parallel.

![](https://cdn-images-1.medium.com/max/5200/0*lohbydjvhaKkseQJ.png)

While Compose triggers the deployment, each deployment runs as usual, in the exact same conditions as a traditional Serverless Framework deployment. This was designed so that introducing Compose in an existing project would have no side-effect.

Note that it is also possible to deploy all services to a specific stage with the --stage option.

## Solving service dependencies

Sometimes, services have dependencies between them. The output of one service (for example its API URL, a table name, a queue URL, etc.) might be used by another.

Passing outputs between dependencies no longer requires complex solutions with CloudFormation imports. In the serverless-compose.yml file, you can now pass outputs from one service to another:
{% gist https://gist.github.com/richardgrantserverless/f8abbd1953ce8114afa30ef36fafb421.js %}
Let’s break it down into 3 steps:

 1. ${products.tableName} will resolve to the "tableName" output of the "products" service.

The outputs of a Serverless Framework service are taken from its CloudFormation outputs. Here is how we can expose the “queueUrl” output in the products/serverless.yml config:
{% gist https://gist.github.com/richardgrantserverless/0f2a5b8f2509091086bc4ae618a02684.js %}
2. Because of the dependency introduced by the variable, serverless deploy will automatically **order deployments** and deploy "products" first, and then "orders".

3. The value will then be passed to the “orders” service as a parameter named “productsTableName”. [Parameters can be referenced](https://www.serverless.com/framework/docs/guides/parameters) in Serverless Framework configuration:
{% gist https://gist.github.com/richardgrantserverless/e225e2903cb1ad573db6c448754bf524.js %}
Compose is designed to integrate with existing serverless.yml features (CloudFormation outputs for outputs, [Serverless Framework parameters](https://www.serverless.com/framework/docs/guides/parameters) for inputs). That way, using Compose does not require a huge rearchitecture of existing monorepositories.

## Setting dependencies without variables

Using variables allows us to exchange values *and* order deployments. It is also possible to order deployments without variables via the “dependsOn” feature:
{% gist https://gist.github.com/richardgrantserverless/06ac87bf18ab0969b3e717eb3e989a3d.js %}
In the example above, the “products” service will be deployed before “orders”. This can be useful in scenarios where a service interacts with another and we want to make sure API or schema changes are deployed in a specific order.

## Global commands and service commands

An interesting benefit of grouping multiple services behind a single configuration is that **we can run one command in all services** at once. For example:

![](https://cdn-images-1.medium.com/max/5200/0*PON-rszyOluJ3Mup.png)

We can also run **service-specific commands** without having to change directories:

![](https://cdn-images-1.medium.com/max/5200/0*v929CE2CNYcfLQTr.png)

## What’s next

Serverless Framework now lets you compose services to orchestrate their deployments.

Services can integrate together via variables and are deployed in order based on their dependencies.

There is a lot more to Serverless Framework Compose, [head over to the documentation](https://www.serverless.com/framework/docs/guides/compose) to learn more and get started.

We are also exploring exciting ideas to improve Compose in the near future, like:

* accelerating deployments for services that haven’t changed

* deploying only specific services via a filter flag

* multi-region deployments

Get involved in the [Serverless Framework repository](https://github.com/serverless/serverless) to discuss these ideas!

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/serverless-framework-compose-multi-service-deployments).*