SQS as a NAT Gateway workaround

Making a phone app using API Gateway and Lambda functions. Most of my app lives in a VPC. However I need to add a function to delete a user account from Cognito (per app store rules).

As I understand it, I can’t call the Cognito API from my VPC unless I have a NAT gateway. A NAT gateway is going to be at least $400 a year, for a non-critical function that will seldom happen.

Soooooo… My plan is to create a “delete Cognito user” lambda function outside the VPC, and then use an SQS queue to message from my main “delete user” lambda (which handles all the database deletion) to the function outside the VPC. This way it should cost me nothing.

Is there any issue with that? Yes I have a function outside the VPC but the only data it has/gets is a user ID and the only thing it can do is delete it, and the only way it’s triggered is from the SQS queue.

Thanks!

Yes, that would be a perfectly fine way to architect around that. Ideally only functions that actually need access to private resources should be attached to a VPC anyway

Thanks! Most of my API functions need access to the RDS database, but that’s good that my “batch-type” functions don’t need to be in the VPC, and those are really the ones that need to access the internet.

You can also just do an asynchronous invoke of your non vpc lambda, if you don’t want to manage the sqs queue yourself.

No issues.

Thanks. That’s true. For me, I’m pretty comfortable with SQS and it seems like a better chance of catching a problem as the message will end up in the DLQ if it doesn’t get processes.

Could also just do an async lambda invocation with a DLQ setup on it. Skips the whole need for a queue consumer event source on the lambda.

True. Thanks for the tip!

You don’t really need to get a VPC involved here at all, right?

Just create a Lambda outside of your VPC that your app can call and have it call Cognito.deleteUser.

Well I have the lambda that does all the database cleanup to eliminate the user, and that has to be in the VPC because it accesses the RDS database. But correct, that function will call my “delete cognito user” function outside the VPC, and it will do the last bit, which is actually deleting the user from the cognito user pool.

FYI rds data API doesn’t require a vpc

Have you looked at data api? That way your lambda uses an http layer to call instead of needing a direct connection.

Thanks. Only on Aurora though, as far as I know. I’m using RDS MySQL.

Since it’s not a critical function don’t forget you can use a NAT instance which will cost a fraction of a NAT gateway and save yourself the complicated (relatively..) but valid solution you describe.

Be aware that lambda has async polling and will result in a lot of sqs polling. Not a big deal, but just be aware you’ll hit about 500k polls in a month

cries in pennies

You could also use a VPC endpoint, but that would still cost around $80USD/year.

Or just use https://fck-nat.dev/v1.3.0/, according to your calculations it would be around 40$

No doubt, bit I see complaints about why there’s 600k polls to sqs in a month when they only send a free requests to sqs

Keeping every single thing inside a VPC is overrated.

Cognito isn’t in your VPC. Think about that conceptually for a minute.