AWS cross region lambda database access - amazon-web-services

Recently some service in us-west-1 seems not working, so I'm trying to create a lambda that can access cross-region RDS (from us-east-1 to us-west-1), but I cannot find any solution or sample with these keywords. Does that means cross-region database is unavailable?
As I know, use lambda reach to database in VPC may cause out of IP issues, so is there any best practice for this?
Thanks

Lambda functions run outside the VPC by default. In the setup of the Lambda you can opt to give them access to a VPC which creates a network endpoint inside your VPC. Lambda uses that network endpoint to access the VPC.
The challenge you face is that your us-west-1 VPC is not visible in us-east-1.
Suggestion: Setup VPC peering between the two VPCs and run the lambda in the us-east-1 VPC.

Related

Proper way of working with DocumentDB and Lambda

I am connecting to AWS DocumentDB from a Lambda function. In order to be able to do this I had to attach lambda to the default VPC (that's where DocumentDB cluster is running) and the default (public) subnets. But, this has caused my Lambda to timeout whenever trying make an outbound request, e.g. push message to SQS. This, I want to avoid.
So what is the recommended way of connecting to DocumentDB without loosing functionality that occurs when putting Lambda in the VPC? There's gotta be a simple solution.
Lambda functions in a VPC never get a public IP address. So if the function needs to access both VPC resources and other resources outside of the VPC the function has to be deployed only to private subnets with routes to a NAT Gateway.
Alternatively, if the only external resources you need to access are other AWS services, then you could add VPC Endpoints for those services to the VPC.

Connecting AWS Lambda and RDS in different AWS Accounts

We have multiple AWS accounts for various departments. I want to give a Lambda function in one account access to an RDS instance in another.
If the Lambda function is not assigned to a VPC and the RDS is publicly accessible is there a way to limit the access to the RDS to only the Lambda function?
Is there a way to do this if the RDS instance is private and the Lambda function is not assigned to a VPC?
As best practice you should always keep your db in private subnet WITHOUT public ip and access. And then in your case assume all your AWS accounts are in a single organisation, you can share the subnets that the RDS located in account A to account B by https://aws.amazon.com/premiumsupport/knowledge-center/vpc-share-subnet-with-another-account/
Then in account B you config the Lambda to sit in the shared subnets from A, and set security group of Lambda functions, and in RDS instance security group you only allow connection from Lambda's security group, then all set.
May I know why you don't want to put Lambda into VPC? If you have concerns on Lambda cold start performance issue in VPC, notice that AWS already solve that in late 2019: https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
Public Amazon RDS database
If the Lambda function is not assigned to a VPC, then it is connected to the Internet. It can then connect to a publicly-available RDS database. However, there is no way to predict the IP address that will be used by the Lambda function.
Private Amazon RDS database
If the RDS database is only available within a VPC, then the Lambda function needs to be somehow connected to the VPC. Since the Lambda function is in a different AWS Account, you might be able to try:
VPC Peering to make the RDS database in one account accessible to the Lambda function in a different account
Shared VPCs: "In a shared VPC, each participant pays for their application resources including Amazon EC2 instances, Amazon Relational Database Service databases, Amazon Redshift clusters, and AWS Lambda functions."
The Shared VPC looks like it might be a great option for you, since (it seems) the AWS Lambda function from Account A could be placed into the same VPC as an Amazon RDS database from Account B.
See also: VPC sharing: A new approach to multiple accounts and VPC management | Networking & Content Delivery
Let us know if it worked for you!

AWS Lambda Function Timeout on Connecting To RDS Database through RDS Proxy

I'm trying to test AWS RDS proxy so I created a lambda function and done all steps that are present in this official link
https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda
store RDS credentials in Secret Manager
create new role and also add Trust Policy
in lambda function, from the AWS console, add proxy and its status is available.
When I execute the lambda function, it times out with no errors it seems like the error might be on connecting to db with rds proxy because when I run the lambda function again without proxy, it works just fine.
I initially thought that it might be a security group issue, so I edit the security group of RDS Proxy and update inbound and allow 0.0.0.0 (outbound was already 0.0.0.0).
I used defaut VPC in RDS Database and RDS Proxy. The endpoint of RDS database is public.
Since RDS proxy is not available outside the VPC. Configure your lambda function to run inside the VPC. The following link will help:
https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html
Late answer.. thought these might help others.
You have to keep your lambdas inside the same VPC and subnets to access RDS proxy.
In any case if you want to access third party web api from your lambda, you have make the lambda subnets private (no Internet Gateway in route table) and assign a NAT gateway which is tied with a public subnet.
If you are accessing other AWS services which are out of VPC like S3, Secret Manager etc. then you have to create VPC endpoints for those services in your VPC.

AWS EC2 access only from Lambda

I have a database running on an EC2 instance in a public subnet.
I would like to protect the EC2 instance so it can only be accessed from the lambdas.
I do not want to attach the lambda through an ENI as it is not a scalable solution, due to the ENI that must be created to allow the lambda access to the VPC.
I had in mind to use a NACL and to only allow inbound traffic from the ip range of the AWS lambda service, but I do not know how often AWS could update its IP ranges.
Please any suggestions regarding security issues will be welcomed
Whitelisting AWS Lambda IP range won't work since we don't have any control over the IP range when Lambda is outside the VPC.
If you are keeping the EC2 in a public subnet there is hardly any approach available to restrict to requests only for Lambda unless you put the Lambda function inside the VPC.
If you can use AWS RDS for the database it is now possible to restrict access via IAM (AWS recently introduced this).

Elasticache in a different VPC and lambda needs to access it

I have two VPCs in AWS. elasticache is in one of the VPC.
I also have a lambda that needs to access the elasticache. I have attached the following policies to the lambda to access the elasticache.
AWSLambdaVPCAccessExecutionRole (as per mentioned here: https://docs.aws.amazon.com/lambda/latest/dg/vpc-ec-create-iam-role.html)
AmazonElastiCacheFullAccess
But I still cannot connect to the Elasticache endpoint from the lambda. It is throwing the following error:
Task timed out after 63.06 seconds
Essentially indicating it failed to connect. But any ec2 instance within the VPC can connect to the elasticache.
AWS Lambda uses elastic network interfaces (ENIs) that enable your function to connect securely to other resources. As you mentioned, lambda function is running in your VPC, not in AWS managed VPC, I recommend couple of below checks to perform which could cause connectivity issue between Lambda and ElastiCache (EC).
IAM Role: Make sure IAM role has required permission to access other VPCs resources.
Network: Verify that both VPC subnet network access control lists (ACLs) allow traffic on the port that you are connecting, and also for the Lambda security group.
Your Lambda function has no route to access the Elasticache VPC. Assuming the VPC CIDRs do not overlap:
If the VPCs are in the same region, you can create a VPC peering between the VPCs. The routing table entry to create a route between the VPCs will be automatically added when you create a peering connection. See: VPC Peering
If the VPCs are in different regions, check Does AWS offer inter-region / cross region VPC Peering? and if peering is available, you can create a peering connection just like #1
If the VPCs are in different regions, and peering connections is not supported, then you have to have a VPN in each VPC and route the traffic through the VPN.
Let's say lambda-A needs access to the ElastiCache in a different VPC(VPC-B).
Options:
You can create another lambda-B in your VPC-B, and let your lambda-A call lambda-B which then call ElastiCache.
Create a NAT instance in the same VPC as your cache cluster but in a public subnet.
Use VPC peering.
Ref: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/accessing-elasticache.html