Monolith app to lambdas: how to do auth?

Hi folks,

we have currently a monolithic app deployed on an ECS cluster. We want to use serverless functions as additional features for our monolithic app to utilize. For example, some might be invoked in a synchronous way.

Our initial thought is to expose each function that could be invoked by our monolithic app using an API Gateway. Our monolith would do a POST to the gateway, run the lambda, and then utilize the results from the lambda for its own purposes.

The question that arises from this is security-related: what is a good and straightforward enough matter to authenticate our own app for this? Is there a best practice at AWS for authentication app-to-app calls (such as setting some APP_TOKEN, and ensuring at the API Gateway level that our calls contain this token). Do we store the key to this token in Secrets Manager, and pass the APP_Token as environmental variable

Or is it better advised to privatize these lambdas, put them in a VPC?

Finally, would you say that doing this monolithic-app => lambdas calls is a red flag, not to do, due to potentially added overhead?

TL;DR - How to do monolithic-to-Api-gateway authentication in a clean way?

On security, AIM might already have you covered: if the monolith is running on AWS, it will most likely have a role it assumes to make these calls, and you can give this role the needed permissions.
Overall, the biggest danger I have seen with these kind of setups, is too many hops. If you call a service in a monolith, because everything is running on one server (and often even in one process), the waiting time for the service and the fail rate are often negligible. But if you call the monolith, which calls a lambda, which calls another lambda, … then response times add up and if one link in the chain fails, the whole chain breaks. So you need to give extra care to making sure that the overall chain is working, not only the links.

For lambdas outside of voc usually ajthorizer is configured. Jwt token can be passed in the header or any other custom token. The autborizer lambda returns a policy. The api gateway resolves that and allow or denies the request based on this. So you compose the policy based on user credentials and your application rules

Yes, IAM auth is the way to go. You can either directly call the Lambdas using the Lambda API or put them behind API Gateway, the latter is usually seen as cleaner, but in my opinion there’s nothing really wrong with the former.

Assuming your monolithic app runs on EC2 you would need to provide a role with appropriate permissions to the EC2 instances which need to make those calls to Lambda.

when calling API gateway you would need to sign the HTTP requests using something like this https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/AWS4Signer.html

Using IAM policies is the most straightforward. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html

That said, what is your reason for using api gateway? The answer to that may suggest using some other auth approach.

Note that attaching the Lambda functions to a VPC does not make them any more private.

There’s no need for API GW in your use case. Your monolith can use the AWS SDK to invoke the Lambda functions directly. Then auth is handled by IAM policies.

Alternatively if you really desire API GW for some reason, you configure it to use AWS_IAM auth and use IAM policies again

Can you invoke the Lambda functions directly using AWS SDK? The access will be permitted using IAM Roles. You can put the Roles in ECS Task Execution Role.

My question is more towards why would you want to utilize Lambda? Are you trying to slowly break down the monolithic app into microservices?

Excellent, I am glad to see so much consensus in the answers here :). Will rely on IAM in that case

The many hops is definitely a good point. We have essentially a few extra serverless flows that we want to interact mostly async with (e.g. dispatching jobs), but for these few rare sync ones, we will accept that there is a tiny bit of overhead at the benefit of not overloading our monolith.

I will perhaps eat my words in the future :slight_smile:

Ah interesting, I did not know this about lambdas in VPCs.

I indeed agree for the most part, though we want some of our APIs to become publishable to the public via the API GW, hence I started reading about it. In that case, I think we will work with custom authorizers (as we will probably use some token mechanism for it then).

But IAM is the way to go for now!

I suspect I will try this custom authorizer soon as well. I like the simplicity of using IAM though, but am intrigued by all methods of auth!

It’s a good point, we have some extra jobs we want to run via lambda functions. For the majority, these can probably be invoked via the sdk, though we might want to make some of the api’s public use as well. Right now, our API would serve as a proxy, but being able to directly call certain lambdas via the API GW could alleviate some overhead.

I think as time goes on, we intend to add more functionality as microservices (either more fargate tasks, or more serverless functions, depending on the scope). All of the logic directly related to our business logic will live in the monolith for now, and all jobs / misc sync tasks (like quick writes to files) might be done using this method we experiment with.

Interesting, I never considered using cognito. Our user is essentially another fargate task (not necessarily any underlying identifiable user). Would it make sense then to still use cognito?

Ah that makes perfect sense to use API GW in that case. It’s nice to have that abstraction.

One extra thing I’ll note is that there are two flavours of AWS API GW. “HTTP” and “REST” APIs. HTTP is newer and faster, but doesn’t have as many features. E.g. both support IAM auth, but only REST APIs work across different AWS accounts.

And if you decide to use tokens, I am about 80% sure that REST supports per-token quotas and rate limits, but HTTP doesn’t. But HTTP is a lot cheaper. So have fun making that decision :sweat_smile: