[英]How to connect MySQL instance from GCP project to AWs Lambda function?
I've hosted my MySQL instance in GCP project and I want to use it's database in AWS Lambda Function.我在 GCP 项目中托管了我的 MySQL 实例,我想在 AWS Lambda Function 中使用它的数据库。 I've tried all the ways to connect to my DB in MySQL instance in GCP but the Lambda Function give me Timeout Error even though I've kept my Timeout period enough to run the function.
I've tried all the ways to connect to my DB in MySQL instance in GCP but the Lambda Function give me Timeout Error even though I've kept my Timeout period enough to run the function. I've also Zipped the Package with MySQL and pymysql installed and then uploaded to Lambda but the issues still persists.
我还压缩了 Package 并安装了 MySQL 和 pymysql,然后上传到 Lambda,但问题仍然存在。
Here's the code that I've written for connecting to my DB:这是我为连接到我的数据库而编写的代码:
import json
import boto3
import mysql.connector
import MySQLdb
def lambda_handler(event, context):
mydb = MySQLdb.connect(
host="Public Ip of MySQL Instance",
user="Username",
password="Password",
db="DbName"
)
cur = db.cursor()
cur.execute("SELECT * FROM budget")
for row in cur.fetchall():
print(row[0])
db.close()
Here's the Error that I receive:这是我收到的错误:
{
"errorMessage": "(2003, \"Can't connect to MySQL server on '36.71.43.131' (timed out)\")",
"errorType": "OperationalError",
"stackTrace": [
" File \"/var/lang/lib/python3.8/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n",
" File \"/var/lang/lib/python3.8/imp.py\", line 171, in load_source\n module = _load(spec)\n",
" File \"<frozen importlib._bootstrap>\", line 702, in _load\n",
" File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 783, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
" File \"/var/task/lambda_function.py\", line 10, in <module>\n connection = pymysql.connect(host='36.71.43.131',\n",
" File \"/var/task/pymysql/connections.py\", line 353, in __init__\n self.connect()\n",
" File \"/var/task/pymysql/connections.py\", line 664, in connect\n raise exc\n"
]
}
Please help me to resolve this.请帮我解决这个问题。 I've tried all different ways to connect to my SQL instance but nothing works.
我尝试了所有不同的方法来连接到我的 SQL 实例,但没有任何效果。
According to the error message, AWS Lambdathe tried to connect the Public IP address of MySQL instance directly.根据错误信息,AWS Lambda尝试直接连接MySQL实例的Public IP地址。
You have to configure your MySQL instance to have a public IPv4 address, and to accept connections from specific IP addresses or a range of addresses by adding authorized addresses to your instance .您必须将 MySQL 实例配置为具有公共 IPv4 地址,并通过向您的实例添加授权地址来接受来自特定 IP 地址或地址范围的连接。
To configure access to your MySQL instance:要配置对 MySQL 实例的访问权限:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.