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?
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?
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:
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.
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.
Interesting. I was trying to find some resources online and came across: 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?
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, they fetch and iterate over them to build configuration that runs a deployment script. 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, unfortunately without dependencies between stacks. But you should be able to figure it out by reading Lerna docs.
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?
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?
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.
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.
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
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.
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?
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
u/jxd73 Look forward to your response