Can I make function name as a variable for different stage?

I have a Lambda function configured like this for staging

memorySize: 256
description: Chameleon transform pageview event to Atlas
handler: chameleon-lambda.handler
timeout: 120s
events: ${file(./serverless-cfg-${opt:stage, self:custom.defaultStage}.yml):events}
environment: ${file(./serverless-cfg-${opt:stage, self:custom.defaultStage}.yml):environment}
BillingBrandCode: CIA

Now I want to deploy this function to production.However, I want the name to be sls-chameleon-prod

How can I achieve this in the configuration? Apologies for my ignorance I’m super new to serverless but I really like the product.

I’m not sure you can. If you look at the doco for variables you’ll see you can’t set the keys in the yaml file using variables.

Note: You can only use variables in serverless.yml property values, not property keys. So you can’t use variables to generate dynamic logical IDs in the custom resources section for example.

I think it’s the same for all providers but I’ve linked to the AWS one.

You should be able to change the path though, because that is a value, not a key.

Taking a step back though, why do you want to do this? It seems to break the usual tradition of building a single artifact and promoting it up through your environments. If you change things when it goes to production, you haven’t really tested what is being deployed in your CI environment.

The Serverless framework automatically names your Lambda based on the service, stage and function name. So if your service is called sls and the function is chameleon and you’re deploying to a stage ci then the lambda will be named sls-ci-chameleon. When you deploy to prod it would become sls-prod-chameleon. You don’t need to do anything for this to happen.

1 Like

Would I be able to do the same, dynamic naming, for other resource, i.e. SNS topic?