How do I specify the stage in a function so I can deploy to stage and production

I’m super new to serverless. I would like to be able to deploy the same function but different AWS account profile. Stage and Production.

This is my function configuration

functions:
sls-chameleon-ci:
memorySize: 256
description: Chameleon transform pageview event to Atlas
handler: chameleon-lambda.handler
timeout: 120s
events:
- stream:
type: kinesis
arn:
batchSize: 100
startingPosition: LATEST
environment:
SQS_QUEUE:
tags:
BillingBrandCode: CIA

I have this function that I use this command to deploy.

…/node_modules/.bin/serverless deploy --stage n -v --aws-profile nonproduction

How do I deploy the same function that could deploy to my production AWS account with production Kinesis and SQS. Right now I just hardcode that in the YAML file.

Hey,

You can do this using the profile flag on the cli for example:
serverless deploy --aws-profile prodProfile

Loads more methods and explaination can be found here:

Per account creds info:

Hope this helps :slight_smile:

1 Like

Thanks for your reply. So, do I create another function for that? Or I can reuse the same function? Because the prod account has different Kinesis stream name and SQS

Don’t hard code stuff into your application or your serverless.yaml. The simplest solution is to add:

custom:
  stage: "${opt:stage, self:provider.stage}"

then include ${self:custom.stage} as part of the resource name. You can do a similar thing with region if you need to deploy the same stage to different regions.

Then pass the relevant information to your functions using environment variables.

1 Like

Thanks! I think I’m starting to get it now.

From what I see, you can put the stage directly into the provider section and it will be used in naming. However, you can provide the default stage in the custom section.

I’ve adapted the example from the documentation as follows:

provider:
  name: aws
  ...
  stage: ${opt:stage, self:custom.defaultStage}

custom:
  defaultStage: dev

The corresponding cloud formation stack will then have the name <service>-<stage> and the stage will default to dev, if no stage is provided.