Multi-tenant Serverless Web App: How to deal with AWS credentials?

I’m building a multi-tenant serverless web app, fully on top of AWS services. I’m also adopting a “pooled” architecture.

This architecture means:

  1. Services will run on Lambdas, which will have execution roles scoped to the service.
  2. Users will federate using Cognito, and within the service Lambdas they will retrieve temporary AWS security credentials using AssumeRoleWithWebIdentity (by STS), sending a session policy as parameter to scope permissions down to tenant access.

The second item above means that federated users will assume a role (pre-built on IAM), and on top of that, a dynamically created “session policy” will be added, scoping the resulting AWS credential’s access down to tenant level resource minutia (like DynamoDB tenant rows and S3 tenant tags).

With that in mind, I have two questions

  1. Will the temporary security credentials generated by STS substitute the Lambda’s execution role (thus invalidating it), or will the Lambda’s execution role still apply?
  2. Once I retrieve the temporary security credentials (access keys + security token) from STS, how should I store them for subsequent requests made by that Lambda for that user?
    (I don’t want to have to generate new credentials on every single request, I want to use them until they expire, which is 1h by default)

1 The lambda’s execution role is the default and will still apply. What I’ve done is after assuming the role you use the credentials returned to instantiate an AWS service. Then use this service to run your commands, it’ll use the user’s role not the lambda role.

2 I’d personally just get new credentials as I don’t see their a big latency issue however if this became an issue (after verifying the token) you could look up the creds in a dynamo session table with a TTL.

Thanks for the reply.

Regarding (2), the situation I imagine is this: Imagine the app displays a Calendar filled with many user appointments. As the user clicks “next month” or jumps through the calendar, the app needs to request from the backend a new batch of appointments.

Imagine if, quickly jumping around the calendar a lot, the user makes 10 requests in a space of 5 seconds. Then he deletes 10 appointments one by one in another 5 seconds (10 more requests).

This would make 20 data requests in the space of a few seconds. Does it make sense to, for every single one of these requests, repeat the entire interaction with Cognito and STS?

Each credentials retrieval is comprised of cognito:GetId, cognito:GetOpenIdToken and sts:AssumeRoleWithWebIdentity.

Every single request will demand 3 extra calls, to 2 distinct AWS services. But those retrieved temporary credentials actually take 1 hour to expire, so it really seems to make sense to store them somewhere.

One thing to keep in mind is pre-optimization. Your use case above could happen all the time or it could happen once a month, all depending on the specific application. If the application has this type of regular use (many quick requests in succession) then it seems like caching the creds can be a big saver. Sometimes it makes sense to delay feature development if you aren’t sure :+1:

Also keep in mind that the 3 calls will be at best reduced to 1 call. So you’d be saving 2 transactions. Then keeping in mind the situation where you check the session and it’s not there, then it’s an additional one transaction to save it. And since you’d be saving the credentials somewhere, there’s the additional security overhead. Regardless it could still be the correct choice, just something to think about!

I found that REST APIs from API Gateway have “API caching“, which seems it would be the optimal solution. Additionally, Lambda Authorizers from REST APIs can cache IAM Policies. But I’m using the new HTTP APIs, so I don’t have this option.

I’m now considering storing the credentials in the client’s cookie (encrypted), which seems to be somewhat less secure and not very flexible (I can only hold ~4k of data, which I think can only hold around 4 AWS credentials—I was thinking of assuming service/action scoped IAM Roles, so I’d probably need more space).

I also considered Secrets Manager and Systems Manager Parameter Store. But the former is expensive at scale ($0.40 per secret per month), and the latter is free but has a low throttling limit (~100 requests per second).

So, of all these options, so far to me a cookie seems the most simple, cheap, fast and scalable. However I’m trying to assess better its security vulnerabilities, as well as how to get around that lack of space when dealing with various credentials. I also thought of IndexedDB or something, idk well how it works.

I do think my app will often deal with many sequential requests in short periods of time. So, I’m invested in discerning the right solution for this.

What if you updated your roles to be simpler and then just planned to cache the most used in the clients cookies? That seems the best approach as cookies are already baked in and would be simple to implement. You can also use local storage and just add some additional client logic.

Good luck!

For the sake of security, I’m now trying to avoid storing on the client, and found a solution that is looking pretty neat: ElastiCache for Redis.

It’s been suggested by Judah Bernstein who is part of the AWS SaaS Factory team.

Since I’m aiming at thousands of tenants, backend solutions like Secrets Manager and Parameter Store end up being costly, because they charge monthly per secret/parameter. So 10,000 users would end up costing hundreds or even thousands of dollars monthly.

But ElastiCache for Redis charges per hour of cluster operation. A “cache.t3.micro” cluster costs $0.017 per hour ($12.24/mo), and it can store 500 MB, which would fit 500,000 AWS credentials of 1 KB each. If I’m calculating right, it looks super cheap, and the first year is free.