# How should REST API's communicate in a serverless application

**URL:** <https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212>\
**Category:** AWS Compute Discussion\
**Tags:** lambda\
**Created:** [January 7, 2021, 1:40am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212 "2021-01-07T01:40:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 7, 2021, 1:40am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/1 "2021-01-07T01:40:52Z")

</div>

I am writing my REST APIs using Lambda and API Gateway. If API\_A needs to call API\_B, How should that ideally be handled?

Also, how should the order of deployment of APIs be handled such that API\_B gets deployed before API\_A in this case?

* * *

**Related discussions:**

- [How are you handling webhook retries and event processing at scale on AWS?](https://awshq.com/t/how-are-you-handling-webhook-retries-and-event-processing-at-scale-on-aws/21359)
- [Announcing AWS Lambda Web Functions](https://awshq.com/t/announcing-aws-lambda-web-functions/21635)
- [Tip for staying under the 250mb Lambda limit](https://awshq.com/t/tip-for-staying-under-the-250mb-lambda-limit/21504)
- [Hardest Problems Lambda MicroVMs Can Solve Now?](https://awshq.com/t/hardest-problems-lambda-microvms-can-solve-now/21419)
- [Self-hosted GitHub Actions runners on Lambda MicroVMs](https://awshq.com/t/self-hosted-github-actions-runners-on-lambda-microvms/21428)

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [January 7, 2021, 10:41am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/2 "2021-01-07T10:41:26Z")

</div>

Calling other API is done directly from the Lambda, API Gateway does not take part in that. So you have Lambda\_A behind API\_A and Lambda\_B behind API\_B. Then Lambda\_A needs to just call API\_B endpoint.

What matters here is how the Lambda\_A should get the endpoint URL of API\_B. You could deploy API\_B first, take URL and hardcode it in Lambda\_A, which is obviously a bad idea. Instead, depending on the tools you use, you should save URL somewhere and reference it from Lambda\_A. If you are using CloudFormation or Serverless Framework (that uses CloudFormation) then 2 best options are:

- using CF Output
- using SSM (Systems Manager Parameter Store)

I suggest using the second option. So you create both APIs and save their endpoint URL to SSM like this (CF, you can put this in Resources in Serverless Framework):

ApiEndpointParam:  
Type: AWS::SSM::Parameter  
Properties:  
Name: /serviceB/apiEndpoint  
Value: !GetAtt HttpApiB.ApiEndpoint  
Type: String

Then in Lambda\_A you either take this value and assign it to the env variable during deployment (in serverless: `${ssm:/serviceB/apiEndpoint}`) or use AWS SDK in Lambda to get value from SSM in runtime. It all depends on the details of your use case and project.

For the deployment part - you can put everything (both APIs and Lambdas) in one stack and use DependsOn in CF to make sure the API\_B will be deployed before Lambda\_A. But the better option is to have separate stacks, as since you have two APIs, those are probably two separate (micro)services. The common solution with Serverless Framework is to have a monorepo with multiple stacks.

You can then use Lerna to automate the deployment of all stacks. Lerna can handle dependencies between packages, so if you will put a dependency on Stack\_B in Stack\_A then Stack\_A will be deployed first, API\_B and SSM param with endpoint URL will be created, and then API\_A and Lambda\_A will be created and can use those params. Although, if you want to resolve params in runtime with AWS SDK call to SSM, you can deploy all stacks at once, as you do not need the deployment to be in order.

So - there are several ways. The tooling I put as an example above is if you use Serverless Framework, but the idea is the same with other tools and languages.

---

<div class="post-metadata">

**Author:** ![jxd73](https://awshq.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jxd73](https://awshq.com/u/jxd73)\
**Post date:** [January 7, 2021, 12:31pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/3 "2021-01-07T12:31:28Z")

</div>

The order of your api deployment should not matter ideally, you will just keep each backward compatible. If it comes time when that’s too much trouble you can create new stages.

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 8:34am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/4 "2021-01-08T08:34:48Z")

</div>

> [@jxd73](#):
>
> The order of your api deployment should not matter ideally, you will just keep each backward compatible. If it comes time when that’s too much trouble you can create new stages.

Can you please confirm how does Serverless framework takes care of the order of deployment? Forex: If we added a new endpoint in microservice B that microservice A is calling then we have to make sure that microservice B is deployed before microservice A.

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 8:39am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/5 "2021-01-08T08:39:23Z")

</div>

> [@mradzikowski](#):
>
> Calling other API is done directly from the Lambda, API Gateway does not take part in that. So you have Lambda\_A behind API\_A and Lambda\_B behind API\_B. Then Lambda\_A needs to just call API\_B endpoint. What …

Interesting. I was trying to find some resources online and came across: [https://github.com/fourTheorem/slic-starter](https://github.com/fourTheorem/slic-starter) I don’t see them specifying the order of deployment anywhere or using a tool like lerna. How is it being taken care of here?

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [January 8, 2021, 1:03pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/6 "2021-01-08T13:03:20Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> Interesting. I was trying to find some resources online and came across: [https://github.com/fourTheorem/slic-starter](https://github.com/fourTheorem/slic-starter) I don’t see them specifying the order of deployment …

As I said, there are plenty of possibilities and tools to make CI/CD. They have a little bit different one from the one that I mentioned.

Quick look in that repo shows that they have all modules [listed manually](https://github.com/fourTheorem/slic-starter/blob/master/cicd/modules.ts), they [fetch and iterate](https://github.com/fourTheorem/slic-starter/blob/master/cicd/lib/cicd-stack.ts) over them to [build configuration](https://github.com/fourTheorem/slic-starter/blob/master/cicd/lib/module-pipeline.ts) that [runs](https://github.com/fourTheorem/slic-starter/blob/20b13ad1f79c4ac8fe8baf8fe4ae3e7e9bd68ea0/cicd/lib/projects/module-deploy-project.ts) a [deployment script](https://github.com/fourTheorem/slic-starter/blob/master/build-scripts/deploy-module.sh). So on the first look it seems the modules are deployed in given order, which resolves problem of dependencies. Although it makes the config not dynamic, you need to add new service to the list. Not great, not terrible.

Like u/jxd73 mentioned, best if you minimize deployment-time dependencies between stacks. You can achieve this by saving all values that you need to share in SSM and then reading them in runtime, as I explained above.

[Here you have an example config for yarn+lerna](https://github.com/PatrykMilewski/serverless-monorepo-presentation/tree/yarn_and_lerna_enchanced), unfortunately without dependencies between stacks. But you should be able to figure it out by reading Lerna docs.

---

<div class="post-metadata">

**Author:** ![jxd73](https://awshq.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jxd73](https://awshq.com/u/jxd73)\
**Post date:** [January 8, 2021, 1:59pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/7 "2021-01-08T13:59:37Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> Can you please confirm how does Serverless framework takes care of the order of deployment? Forex: If we added a new endpoint in microservice B that microservice A is calling then we have to make sure that microservice …

I can’t confirm that, but in my opinion since you each api is an application onto itself, and you should not create hard dependencies, otherwise it becomes a monolith.

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 5:10pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/8 "2021-01-08T17:10:48Z")

</div>

> [@jxd73](#):
>
> I can’t confirm that, but in my opinion since you each api is an application onto itself, and you should not create hard dependencies, otherwise it becomes a monolith.

Isn’t it natural for an API to be calling another API though? Doesn’t it automatically become a dependency in that case?

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 5:15pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/9 "2021-01-08T17:15:54Z")

</div>

> [@mradzikowski](#):
>
> As I said, there are plenty of possibilities and tools to make CI/CD. They have a little bit different one from the one that I mentioned. Quick look in that repo shows that they have all modules [listed …

u/mradzikowski Thanks for your response. I like the idea of using SSMs and they are being used in the link I shared above as well. so I understand storing the ARNs/URLs in SSM and fetching them from SSM in the API where we need to use it. But, When I use CI/CD to deploy all my APIs for the first time in the AWS account let’s say, those SSM values for the microservice being called will only be present if the particular microservice has already been deployed. Did I miss something here? Could I possibly completely remove these dependencies from one microservice to another. How does synchronous interservice communication happen then?

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 5:19pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/10 "2021-01-08T17:19:39Z")

</div>

> [@mradzikowski](#):
>
> As I said, there are plenty of possibilities and tools to make CI/CD. They have a little bit different one from the one that I mentioned. Quick look in that repo shows that they have all modules [listed …

For example, I would also like to share a lambda authorizer across all my API gateways. In this case, When the lambda authorizer gets deployed, it will store the ARN in the SSM. In my API gateway in serverless.yml file, I would like to fetch the arn for the authorizer from SSM. But In order to do that, we need to make sure that the Lamba authorizer was deployed first.

---

<div class="post-metadata">

**Author:** ![jxd73](https://awshq.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jxd73](https://awshq.com/u/jxd73)\
**Post date:** [January 8, 2021, 8:52pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/11 "2021-01-08T20:52:43Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> Isn’t it natural for an API to be calling another API though? Doesn’t it automatically become a dependency in that case?

I can’t say if it’s natural that depends on your app.

It will be a dependency but not a hard one. as in if B failed deployment it won’t block A from deploying. Or if you make a change in A that doesn’t require B, but B has also changed but not ready for prod. And then multiple situations like this by the number of environments you have.

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 8, 2021, 9:27pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/12 "2021-01-08T21:27:54Z")

</div>

> [@jxd73](#):
>
> I can’t say if it’s natural that depends on your app. It will be a dependency but not a hard one. as in if B failed deployment it won’t block A from deploying. Or if you make a change in A that doesn’t require B, but B …

> For example, I would also like to share a lambda authorizer across all my API gateways. In this case, When the lambda authorizer gets deployed, it will store the ARN in the SSM. In my API gateway in serverless.yml file, I would like to fetch the arn for the authorizer from SSM. But In order to do that, we need to make sure that the Lamba authorizer was deployed first.

Is there a better way to manage this? u/Zestyclose-Ad2344

---

<div class="post-metadata">

**Author:** ![mradzikowski](https://awshq.com/letter_avatar_proxy/v4/letter/m/ee7513/32.png) [@mradzikowski](https://awshq.com/u/mradzikowski)\
**Post date:** [January 8, 2021, 10:18pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/13 "2021-01-08T22:18:48Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> For example, I would also like to share a lambda authorizer across all my API gateways. In this case, When the lambda authorizer gets deployed, it will store the ARN in the SSM. In my API gateway in serverless.yml file, …

Yes. You either keep things that depend on each other in one stack, or you need to maintain deployment order in this case. Because here you need this dependency to be resolved at deployment time. So you can make sure the deployment order is correct with either a script that deploys things one at the time like in this repo, or with lerna and dependencies between modules. Or probably with a lot of other tools.

---

<div class="post-metadata">

**Author:** ![jxd73](https://awshq.com/letter_avatar_proxy/v4/letter/j/df705f/32.png) [@jxd73](https://awshq.com/u/jxd73)\
**Post date:** [January 9, 2021, 12:12am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/14 "2021-01-09T00:12:29Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> Is there a better way to manage this? u/Zestyclose-Ad2344

It sounds like you should move your lambda authorizer into a nested stack

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 9, 2021, 12:21am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/15 "2021-01-09T00:21:41Z")

</div>

> [@jxd73](#):
>
> It sounds like you should move your lambda authorizer into a nested stack

u/jxd73 Thanks for your response. Does serverless take care of deploying nested stacks first? If 10 microservices have lambda authorizer as nested stack then lambda authorizer will be deployed first (once) and then rest of the 10 microservices. Is that correct?

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 9, 2021, 12:31am UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/16 "2021-01-09T00:31:00Z")

</div>

> [@jxd73](#):
>
> It sounds like you should move your lambda authorizer into a nested stack

Also, How about microservices that are in different stacks but are coupled i.e one API calls the other API (by fetching arn from ssm) u/jxd73

---

<div class="post-metadata">

**Author:** ![Zestyclose-Ad2344](https://awshq.com/letter_avatar_proxy/v4/letter/z/7ea924/32.png) [@Zestyclose-Ad2344](https://awshq.com/u/Zestyclose-Ad2344)\
**Post date:** [January 11, 2021, 11:46pm UTC](https://awshq.com/t/how-should-rest-apis-communicate-in-a-serverless-application/12212/17 "2021-01-11T23:46:51Z")

</div>

> [@Zestyclose-Ad2344](#):
>
> u/jxd73 Thanks for your response. Does serverless take care of deploying nested stacks first? If 10 microservices have lambda authorizer as nested stack then lambda authorizer will be deployed first (once) and then rest …

u/jxd73 Look forward to your response
