# Stages in Serverless Framework

**URL:** <https://discourse.sst.dev/t/stages-in-serverless-framework/35>\
**Category:** Chapter Comments\
**Created:** [May 9, 2018, 6:39pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35 "2018-05-09T18:39:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 6:39pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/1 "2018-05-09T18:39:09Z")

</div>

_From @jayair on Fri Oct 06 2017 22:39:56 GMT+0000 (UTC)_  
  
Link to chapter - [https://serverless-stack.com/chapters/stages-in-serverless-framework.html](https://serverless-stack.com/chapters/stages-in-serverless-framework.html)  
  
_Copied from original issue: [https://github.com/AnomalyInnovations/serverless-stack-com/issues/149](https://github.com/AnomalyInnovations/serverless-stack-com/issues/149)_

---

<div class="post-metadata">

**Author:** ![cpritch](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/cpritch/32/593_2.png) [@cpritch](https://discourse.sst.dev/u/cpritch)\
**Post date:** [November 15, 2019, 12:16pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/2 "2019-11-15T12:16:57Z")

</div>

Could someone help further explain how to set up stages for API endpoints?

Right now, I have 3 stages setup: DEV, STAGING, and PROD. Each work with my frontend, but only with the latest deployment. For example…

- I deploy the API to DEV, I can’t access the STAGING or PROD APIs anymore from the client.
- I deploy the API to STAGING, I can’t access the DEV or PROD APIs anymore from the client.
- I deploy the API to PROD, I can’t access the DEV or STAGING APIs anymore from the client.

I looked into it and in the API Gateway, the stages are being created without a problem. However, the lambda functions backing the API are replaced with each stage. So, if I have a DEV-{function} lambda function, when I deploy using `--stage STAGING`, it is replaced with STAGING-{function}. I would expect DEV-{function} to not get replaced and STAGING-{function} to just be added (or updated).

I think this might be me misunderstanding the concept, as it’s explained that it is better to have separate APIs, one for each stage. However, I don’t know how to set this up in the serverless.yml.

Can anyone help?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [November 16, 2019, 6:27pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/3 "2019-11-16T18:27:47Z")

</div>

So the process you described of how Lambda functions are named based on the stage is accurate. However Serverless Framework does not replace them. It duplicates them. So you’ll have multiple versions of the Lambda function and API endpoints tied to the stage that you create.

In our guide we have two stages and the frontend of the app has both listed in the config:

> <https://github.com/AnomalyInnovations/serverless-stack-demo-client/blob/master/src/config.js>

For reference, here is the backend that we deploy:

> **[AnomalyInnovations/serverless-stack-demo-api](https://github.com/AnomalyInnovations/serverless-stack-demo-api)**
>
> Source for the demo app API in Serverless-Stack.com - AnomalyInnovations/serverless-stack-demo-api

I’m not sure why your endpoints are not accessible anymore. What’s the error that you are getting?

---

<div class="post-metadata">

**Author:** ![cpritch](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/cpritch/32/593_2.png) [@cpritch](https://discourse.sst.dev/u/cpritch)\
**Post date:** [November 16, 2019, 6:46pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/4 "2019-11-16T18:46:41Z")

</div>

Specifically, I get a 500 error when hitting an API endpoint that I didn’t deploy last. The endpoint that I most recently deployed is accessible. For example, I just ran `serverless deploy --stage DEV` yesterday and my lambda statements look like this:

 ![24%20PM](https://canada1.discourse-cdn.com/flex032/uploads/serverless_stack/original/1X/98b046107cf5d63671891fc22d08a375dd19fa67.png)

I can hit my API `~/DEV` successfully. However, when I run `serverless deploy --stage STAGING` all those functions are replaced. I end up with…

- `STAGING-submit-module`
- `STAGING-get-course`
- `STAGING-get-module`

and my `DEV` functions are no longer there. Then, I can’t hit my `~/DEV` api anymore, but I can hit my `~/STAGING` now.

---

<div class="post-metadata">

**Author:** ![anchenqq](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anchenqq/32/647_2.png) [@anchenqq](https://discourse.sst.dev/u/anchenqq)\
**Post date:** [November 16, 2019, 7:55pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/5 "2019-11-16T19:55:10Z")

</div>

Hey Cpritch,

can you maybe provide your github repo to this?  
Would love to look at the files and figure out why its doing this.

---

<div class="post-metadata">

**Author:** ![cpritch](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/cpritch/32/593_2.png) [@cpritch](https://discourse.sst.dev/u/cpritch)\
**Post date:** [November 16, 2019, 7:56pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/6 "2019-11-16T19:56:39Z")

</div>

I can’t provide the source code for this, but I could provide pieces of it. Perhaps my serverless.yml would be helpful?

---

<div class="post-metadata">

**Author:** ![cpritch](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/cpritch/32/593_2.png) [@cpritch](https://discourse.sst.dev/u/cpritch)\
**Post date:** [November 16, 2019, 8:01pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/7 "2019-11-16T20:01:41Z")

</div>

Here’s my serverless.yml. I replaced some of the identifying names with arbitrary ones.

```
service: my-service-name

# Create an optimized package for our functions
package:
  individually: true

plugins:
  - serverless-bundle # Package our functions with Webpack
  - serverless-offline
  - serverless-delete-loggroups

provider:
  name: aws
  runtime: nodejs10.x
  stage: ${opt:stage, 'DEV'}
  region: us-east-1
  stackName: my-stack-name
  apiName: my-api-name
  logRetentionInDays: 7
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:us-east-1:*:*"
  deploymentBucket:
    name: my-deployment-bucket
    blockPublicAccess: true
  environment:
    stage: ${self:provider.stage}

functions:
  get-course:
    name: ${self:provider.stage}-get-course
    handler: src/get-course.main
    events:
      - http:
          path: course
          method: get
          cors: true
          authorizer: aws_iam
  submit-module:
    name: ${self:provider.stage}-submit-module
    handler: src/submit-module.main
    events:
      - http:
          path: course/{moduleNo}
          method: post
          cors: true
          authorizer: aws_iam
  get-module:
    name: ${self:provider.stage}-get-module
    handler: src/get-module.main
    events:
      - http:
          path: course/{moduleNo}
          method: get
          cors: true
          authorizer: aws_iam

# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(resources/api-gateway-errors.yml)}
```

---

<div class="post-metadata">

**Author:** ![anchenqq](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anchenqq/32/647_2.png) [@anchenqq](https://discourse.sst.dev/u/anchenqq)\
**Post date:** [November 16, 2019, 8:02pm UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/8 "2019-11-16T20:02:11Z")

</div>

Maybe not serverless.yml -\> don’t think this is the file that has an issue.

---

<div class="post-metadata">

**Author:** ![ashishmalg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/ashishmalg/32/693_2.png) [@ashishmalg](https://discourse.sst.dev/u/ashishmalg)\
**Post date:** [May 31, 2021, 7:15am UTC](https://discourse.sst.dev/t/stages-in-serverless-framework/35/9 "2021-05-31T07:15:50Z")

</div>

Doesn’t this cause our environment variable values to end up in version control since we are entering them directly in serverless.yml file? Thanks.
