APIs getting replaced on deployment

I’m trying to setup different stages, but every time I make a new stage deployment, the previous one is replaced. I have 3 stages: DEV, STAGING, and PROD.

For example (assume I have already deployed the API to DEV),

  • When I deploy the API to STAGING, I can access the STAGING API, but no longer the DEV or PROD.
  • When I deploy the API to PROD, I can access the PROD API, but no longer the DEV or STAGING apis.

Looking further into the issue, I noticed that whenever I run serverless deploy --stage {stage} it replaces the lambda functions (e.g. DEV-function is replaced with STAGING-function). So, when I try to hit the DEV endpoint from my DEV environment, it returns a 500 error since the backing lambda function no longer exists.

After reading what’s described here, it seems like I might want different API endpoints per environment.

Right now I have this:

But maybe I want this?

If I do need the latter, how do I set that up in my serverless.yml? If I don’t want the latter, how do I make sure my dev stage always hits my dev lambdas, my staging always hits my staging lambdas, and my prod always hits my prod lambdas, without having other stages removed during deployment?

I hope I’ve been able to explain things well enough to get some help. Let me know if I can clarify anything else.
Thanks in advance!

Hey Cpritch,

In AWS API Gateway, staging allows you to set different end path but your API GATEWAY invoke-url should be be same, but the end path will change… between which ever staging name you change to.
It could be “dev”
It could be “prod”
It could be “staging”

The reason its still the same is because your AWS API gateway was setup with this identity with the correct region added into the invoke url. Your specific API gateway was initially setup with proper lambda functions to do certain functions with other AWS services -> “/prod”

Now if you added or changed lambda functions to do other things and you want this to not affect your current stage “prod”. You can create a new stage that is independent and test it or deploy it.
When you create a new stage with these new functions/things you added, it will change the end path of your API GATEWAY from “/prod” to “/what ever you name here”

Hope this helps.



1 Like

It kind of helps. I think you described what I currently have. So, maybe I just have it setup wrong.

When I deploy using serverless deploy --stage DEV, I can hit https://abc12345.execute-api.us-east-1.amazonaws.com/dev, but I cannot hit ~/staging or ~/prod.

Similarly, when I deploy using serverless deploy --stage STAGING, I can hit https://abc12345.execute-api.us-east-1.amazonaws.com/staging, but I cannot hit ~/dev or ~/prod.

Same pattern occurs when I deploy to PROD.

I noticed in the AWS console, that every time I deploy, the old lambda functions are replaced. So, for example, when I run serverless deploy --stage STAGING, it replaces my function DEV-FunctionA with STAGING-FunctionA. Which, I think is the reason I can only hit the stage that I most recently deployed.

I would expect the lambda DEV-FunctionA to only be replaced when I’m doing serverless deploy --stage DEV, not when the stage is STAGING or PROD.

I have a feeling this might have something to do with my serverless.yml setup, but I’m not sure. Any ideas?

Hey Cpritch,

Out of curiosity… Why is your staging path;

How come it’s abc12345 and not your own identity from API gateway?
How are you communicating with this url? Did AWS actually create an identity “abc12345” for you?

Ahh, no. I just replaced my own identity with something arbitrary. Wasn’t sure if I should be posting the real URL on a public forum.

AWS created the identity for me automatically when I ran serverless deploy for the first time. I then use the AWS API library to communicate with the endpoints from the client (just like the serverless-stack tutorial instructed).

I can confirm my communication always works with the latest API deployed stage (i.e. I can communicated with DEV, if I last deployed DEV, but no longer with STAGING or PROD). Same pattern occurs if I deploy STAGING or PROD.

ahhh I got it.

Let me try to replicate your issue and see what’s happening. Glad to see that you are troubleshooting/debugging yourself.
I used to hate debugging cause I thought it was a waste of time and I was super impatience. But debugging really helps solidifying your understanding with what you are learning.

1 Like

Great! Thank you!! I’ll be actively watching this thread.

1 Like

For reference, I asked a similar question in the chapter comments thread here.