AWS - Unable to connect to mongo in Lightsail from Lambda - mongodb

The following are the two key points.
I have a mongo instance deployed in AWS Lightsail.
I have set of lambda functions(written in python) which need to communicate with this particular DB
But every time I run the function (triggered from API Gateway) it times out.
The following are the things I have tried with no luck:
Added VPCExecution IAM role to Lambda.
Tried opening the Lightsail instance to the public(0.0.0.0), in which communication happens(obviously), but is definitely not a recommended solution
Tried setting up a NAT Gateway with a static IP and whitelisting it in Lightsail's firewall. This also works but I cannot afford the cost.
Tried enabling the VPC Peering from Lighsail's account panel. Still no luck. (This is where I was hoping it to work)
Is there anything I'm missing. I really don't need to go towards EC2. But if you can offer any advice that results in the same cost, that'd be great.
PS: I am able to connect to the instance after whitelisting my Public IP.
I really need help with this. Any response will be highly appreciated.

Related

MongoDB and Google Cloud Functions VPC Peering?

I've having issues accessing MongoDB Atlas from Google Cloud functions. It is giving me error regarding IP Whitelisting but I've added both (Serverless VPC Access) IP address range and VPC Network Peering IP address range to MongoDB whitelist.
I've also created MongoDB peering with google cloud.
If I allow (access from anywhere) then my mongodb starts working fine, otherwise it gives error regarding IP whitelisting.
I'm not sure what else I should add to MongoDB whitelist when I've added both IP's already.
Can anyone help me regarding this? A simple step by step guide will mean a lot. (images/video can help big if possible)
**Edit
I took (Atlas GCP Project ID & Atlas VPC Name) to create (VPC Network Peering).
And they both are (Active & Available).
And after that I created (Serverless VPC Access).
And added it to my function inside (connection), a function that will connect to mongoDB to get data. It works fine if I set mongoDB to (allow from everywhere) but do not work without it.
And after that I added all 3 IP's/CIDR blocks to the IP Whitelist.
The CIDR Block from MongoDB Atlas as in 1st image.
And CIDR Block from Serverless VPC Access.
And CIDR Blcok from VPC Network as well just like all above.
But I've still confused that when I run this function it still gives me error about IP Whitelist and only works if I allow traffic from everywhere in mongoDB.
Don't know what I'm doing right and what I'm doing wrong. As there aren't any videos available on internet to achieve this.
I even tried this article but still nothing works out.
https://medium.com/better-programming/connecting-google-cloud-functions-with-mongodb-atlas-499a0a82ccf3
This is the error I'm getting.
If you know you need to whitelist specific IPs:
Whitelist all IPs.
Connect successfully.
Download server log.
Figure out which IP the connection came from.
Whitelist that IP.
Verify this IP is in your expected range, etc.
If you know you don't need to whitelist specific IPs:
Reference Atlas documentation that says so and explains how VPC peering is supposed to work (medium posts are not a substitute for official documentation).
If you don't know whether specific IPs need to be waitlisted:
Follow the first procedure and whitelist your IPs.
Then look for official documentation stating what the proper usage would be.

how to connect MongoDB Atlas to GCP(Google Cloud Platform)?

I try to connect my app that is hosted on google cloud platform(gcp) app Engine to my Mongo Atlas DB.
And Mongo wants me to whitelist the gcp app ip.
But gcp doesn't have a static IP for me to whitelist.
I want to make sure I apply security best practices, and as far as I understand whitelisting my DB for all the ips is not secures. So how can I do it without opening all ips ?
You have 2 solutions
You can grant the App Engine IP ranges. But it's not secured as described in the documentation:
From this example, we see that both the 8.34.208.0/20 and 8.35.192.0/21 IP ranges can be used for App Engine traffic. Other queries for any additional netblocks may return additional IP ranges.
Note that using static IP address filtering is not considered a safe and effective means of protection. For example, an attacker could set up a malicious App Engine app which could share the same IP address range as your application. Instead, we suggest that you take a defense in depth approach using OAuth and Certs.
You can perform VPC peering. This required several things
Have a paid subscription to Mongo Atlas
Create a {peering between Mongo Atlas and your project](https://docs.atlas.mongodb.com/security-vpc-peering/)
Create a serverless VPC connector and add it to your App Engine to allow it to reach private IP on the VPC (and peering attached to the VPC, like your Mongo Atlas DB)
You have the option of reserving a static IP while creating a VM.
On the"create instance" page, scroll to "networking" you are presented with options for your
I. Internal IP
II. External IP
If you are running M10-Cluster (or higher) on Atlas, VPC-Peering is your way to go. I'd recommend trying this tutorial. They're explaining what CIDR-ranges (what you referred to as IPs) to whitelist.
One thing to notice here, they are using GCPs Kubernetes Engine. With App Engine there is a little extra effort as it is one of GCPs "Serverless"-Solutions, which is the reason why you should not use static IPs or anything like that. You will need to connect your App to the VPC-Network via a Connector:
Create a connector in the same region as your GAE-App following
these instructions. You can find out the current region of your
GAE-App with gcloud app describe. Just give the connector the range
10.8.0.0 for now (/28 is added automatically). Remember the name
you gave it.
Depending on your environment your app has to point to that connector. In NodeJS its your app.yaml file and it looks similar to this:
runtime: nodejs10
vpc_access_connector:
name: projects/GCLOUD_PROJECT_ID/locations/REGION_WHERE_GAE_RUNS/connectors/NAME_YOU_ENTERED_IN_STEP_1
Go to your Atlas project, navigate to Network Access and whitelist
the CIDR-range you set for the connector in Step 1
You may also need to whitelist the CIDR-range from Step 1 for the
VPC-Network. You can do that in GCP by navigating to VPC-Network ->
Firewall

Cannot access google cloud SQL from google container engine

I'm still having problems accessing the cloud SQL instance from a GCE container. When I try to open up mysql, I get the following error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial
communication packet', system error: 0
The connection works fine from my local machine, though (The instance has a public IP and I have added my office's IP to the 'allowed Networks'). So, the instance is accessible through the internet just fine.
I guess the db's access control is blocking my access from the gce network, but I'm unable to figure out how to configure this.
I added my project to "Authorized App Engine Applications" in the Cloud SQL control panel, but that doesn't seem to help.
EDIT:
If I add "0.0.0.0/0" to Allowed Networks, all works well. This is obviously not what I want, so what do I need to enter instead?
EDIT2: I could also add all public IPs from my kubernetes cluster (obtained through gcloud compute instances list) and add them to the cloud sql access list manually. But, this doesn't seem to be right, does it?
The recommended solution is to use SSL connection with that 0.0.0.0/0 CIDR. This is to limit the connection to the correct key. I also read that they won't promise you a specific IP range so the CIDR /14 might not work some times. I had to do the SSL connection with my Cloud SQL for the same reasons.
You should use the public IP addresses of the GCE instances to correctly allow traffic to your Cloud SQL instance (as you mentioned in EDIT2).
You can find more information in Cloud SQL documentation: https://cloud.google.com/sql/docs/gce-access
If you add the /14 CIDR block for your Container Engine cluster as the source address range does that work?
To find the CIDR block for your cluster, click on the cluster name in the Google Cloud Console and find the row labeled "Container address range".

Google Cloud SQL VM refusing connection

I have been stuck trying to figure out why my Cloud SQL VM is refusing my connection from my machine (whom ip address I have added as a subnet). I cann SSH into the VM but i cannot access the VM from a browser to make SQLs. I have scoured the internet for days trying to find a fix but i cannot seem to get pass this point. My apache listens to port 80. Also Id like to add that I have been connecting to my Mysql db for months through php and making sqls so I do not believe the problem is with apache. However if it is please point me to where i should be looking.
It sounds like you have MySQL running on a GCE VM, not an actual CloudSQL instance (that is a different service from GCE). Is that right?
If so, then if you are trying to connect from your local machine directly to the mysql instance, you are probably getting blocked by the firewall. Go to the networks tab (under Compute Engine) on the cloud console and see what firewall rules you have enabled. You might need to add one for 3306 or whatever port you are using.

How to make a Google Cloud SQL Instance accessible for any IP address?

I have just created a Google Cloud SQL instance. When I was looking on the access control of my instance, I found that if I want to access my database, I should authorize my IP address to get the right to access the database, but the problem is that my application will be deployed anywhere where the clients need, and even if I know where they will run the application and also I authorized their IP address, it (the IP) will be changed at least one time every 24 hours because it is not static IP, and then I have to re-authorize the IP again and again!
Is there any way to make the instance accessible from any IP?
Thanks
You can whitelist any subnet. You just need to enter it using CIDR notation: http://en.wikipedia.org/wiki/Cidr
In particular, you can whitelist 0.0.0.0/0 which includes all possible IP Address.
Please note that this is not recommended for security reasons. You want your access to be as restricted as possible.
This is an older post, but I noticed it on the sidebar so I figured I would add my 2c.
If you're able to use Cloud SQL Second Gen (currently in Beta) there is a new feature which allows access to the database without having to whitelist any firewalls: https://cloud.google.com/sql/docs/sql-proxy
Today, I was looking for a way to set-up an MS-SQL server for development purpose and found the similiar problem (how to allow my laptop to access).
This guide, helps.
In short, you need to allow firewall to enable EXTERNAL access to your VM instance at port 1433.