Lambda + RDS + Internet... without paying extra...

Is there any way to accomplish this?

If I create an inbound rule to my RDS that’s open to the internet then my Lambda works but obviously I don’t want to do that. It would be great if I could lock it down to just the ip of the Lambda.

How would I find out what IP is being used?

What are you actually trying to do? You want a Lambda to interact with an RDS securely? That’s pretty straightforward. Both of them should go in the same VPC and use IAM roles to control access.

Lambda does not have a static IP when not in a VPC. You could use the entire EC2 range, that’s as limited as you can go. So your function really should go in a VPC. Or if it needs to do things over the internet and with RDS, you could break it up into two functions, and have one function call the other with one in a VPC and one not.

I need my Lambda to connect to RDS and the internet.

Yes, but you lose internet access when you connect a Lambda to the VPC, unless you pay extra for NAT.

I’m not sure I can break it up but this gives me something to research. Thanks!

Ideally you’d either use a NAT gateway or a NAT instance, there are a few NAT solutions out there like alternat which manage this for you.

There was a hack to get a public IP address on a Lambda function posted recently. I’ve not tried it and wouldn’t trust it in production myself but it may be worth a go - https://dev.to/slsbytheodo/deploy-a-lambda-with-a-static-ip-for-free-4e0l

Yeah, as others have mentioned, there isn’t many straight-forward options. A Lambda in a VPC can’t access the internet without a NAT gateway (or something similar), so you’d have to around that. Breaking up the Lambda into one which exists in the VPC and one which exists outside of it is an option, but cumbersome.

If you’re using Aurora, you can enable the Data API to be able to access your database through an HTTP endpoint. Sometimes that’s acceptable and sometimes it’s not, but it works well with Lambdas.

Consider running NAT instances instead of NAT Gateway for ~10% the cost.

https://fck-nat.dev/

I’ll echo others comments with breaking up the logic if possible, having one Lambda accept data from the internet and pass it to the other Lambda - SQS is often used as an intermediary here.

I’ll also suggest if you do end up determining you need to use NAT, this is a more cost-sensitive approach:

https://fck-nat.dev/v1.3.0/

You could have two (2) lambdas,

  1. Outside of VPC with internet access to fetch whatever it is you need, save it to S3
  2. Inside a VPC, to pick up the content from S3 and load it into RDS

Bonus points if you have Aurora can have the first lambda save it in a CSV so you can use LOAD DATA FROM S3

I need my Lambda to connect to RDS and the internet.

Consider using a Lambda to connect to RDS, and then call a second Lambda, which access the internet.

(Or in the opposite order). I did this, and it’s much cheaper for low volumes than running NAT instances/gateways

It’s not a Gateway, it’s a Gatekeeper

In VPC mode, the RDS inbound rule can reference the Lambda security group. That would limit it to just the related lambda hyperplane ENI, no need for the whole VPC CIDR.

It takes less than 10 minutes to see attacks on ports open to 0.0.0.0, so obviously don’t do that, as you already said.

Using a Lambda and plain vanilla RDS is not a great idea, as lambdas scale horizontally and can spam the available ports in no time. Especially of the Lambda is publicly accessible. Dynamo can take it, RDS is another story entirely.

What you may wanna look at is RDS proxy:

https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/

Money-wise, it should probably not set you too far back.

https://aws.amazon.com/rds/proxy/pricing/

RDS Proxy pricing correlates to the number of vCPUs of the underlying database instance for which it is enabled. For example, if you are running a single-AZ or multi-AZ RDS MySQL m5.large database instance that has 2 vCPUs and you have enabled the proxy, you will be charged $0.030 per hour for the proxy ($0.015 per vCPU-hour x 2 vCPUs). For a 30-day month your bill would show 1,440 vCPU-hours (2 vCPU x 24 hours x 30 days): $21.60

Otherwise, if you want no additional cost, deploy the lambda in a VPC and put the DB in the private subnet with routes set up accordingly.

I’m not sure but have you tried enabling ipv6 outbound? If RDS supports it can access without a NAT.

You can use an ec2 as nat instance and route the internet access from/to your lambda through the nat:

https://docs.aws.amazon.com/pt_br/vpc/latest/userguide/VPC_NAT_Instance.html

Also everything can be over free tier Aws so nothing to pay for it

And when the instance fails and you are hard down?

Autoscale 1/1/1

I saw it in one of the newsletters about aws:
https://github.com/1debit/alternat