Dynamic service name from param with Serverless Framework V3

At my previous companies, we used serverless version 2.29.0, we loved the ability to pass in the name of the microservice through the option, e.g.
sls deploy --stage dev --microservice payments.
And include it to the service name as something like
service: ${self:custom.appName}-${opt.microservice}
to create a different stack to deploy a different set of APIs. So we can deploy different sets of APIs using the same serverless.yml that can reference the function definitions in different files.

Now with v3, we lose the custom option, and have to use param. So the deploy command will become
sls deploy --stage dev --param="microservice=payments".

We change the service name to

service: ${self:custom.appName}-${param.microservice}.

But the param does not get resolved, and is taken literally as ‘${param.microservice}’ which is an invalid name.

Then I tried to use custom


service: ${self:custom.appName}-${self:custom.microservice}

custom:

microservice: {param:microservice, 'default' }

It generates an error:


Error:

Cannot resolve serverless.yml: "service" property is not accessible (configured behind variables which cannot be resolved at this stage)

So it looks like we cannot resolve any parameter/variables before the service name is accessed. I am now out of solutions. I don’t know how else I can dynamically set the service’s name from the input. Does anyone do things in similar way, and has a solution?

I don’t want to create a different serverlss.yml for different set of APIs, like serverless-orders.yml, serverless-customers.yml, etc. and use different config file to deploy different set of APIs.

I tried downgraded serverless back to 2.29.0, but it only supports node up to 14. I am using node 16 in my project.

Thank you for your help!

3 Likes

I just realized my post has finally been reviewed and visible after 21 days. (not sure when it became visible as I had stopped checking after a week or 2).

I am still wanting help on this. I can use the stackName option to deploy different sets of APIs into different aws stacks. But a few things are still hard-linked to the service name, so I eventually still hitting conflicts.

Appreciate your help!

1 Like

I’m actually facing the same issue with serverless 3.26.0.
On your side, have you found out a solution for such configuration ?
Thanks in advance

need a configuration like this too.

Hi, I already found an alternative, yo can put stackName on provider section

provider:
name: aws
runtime: python3.8
stackName: ms-${param:microserviceGroup}-v1

I can use the stackName option to deploy different sets of APIs into different aws stacks. But a few things are still hard-linked to the service name, so I eventually still hitting conflicts.

no I have not. So currently I am just deploying all lambdas together.

For anyone else looking for a solution, I found a way to fix my issues with dynamic service names in Serverless V3:

CLI options can be used in the service name, but custom CLI options were deprecated. Params also won’t work, and anything set in custom won’t be accessible at runtime for the service name.

Fortunately, environment variables can be used in the name. In my case, I set the environment variable in a script before running the deploy command, then set the service name like so:

service: my_service-${env:MY_CUSTOM_ENV_VAR}