在本地运行SAM时如何连接RDS实例? [英] How to connect RDS instance when running SAM locally?
问题描述
我正在使用SAM(无服务器应用程序模型)在本地测试连接到云中Aurora RDS实例的Lambda函数.
I am using SAM (Serverless application model) to test Lambda functions locally that connect to an Aurora RDS instance in the cloud.
使用以下命令:
sam local invoke "lambda function name" --event event.json
正在执行Lambda函数,但在返回SQL结果时,它会返回null作为输出.
The Lambda function is being executed but when it comes to returning SQL results, it's returning null as output.
如何配置Docker容器与RDS实例通信?
How can I configure the Docker container to communicate with the RDS instance?
推荐答案
如sam本地调用帮助中所述,您可以将Docker容器连接到现有Docker网络:
As mentioned in the help for sam local invoke, you can connect your Docker container to an existing Docker network:
▶ sam local invoke --help
...
--docker-network TEXT Specifies the name or id of an existing
docker network to lambda docker containers
should connect to, along with the default
bridge network. If not specified, the Lambda
containers will only connect to the default
bridge docker network.
因此,要列出您的Docker网络:
So, to list your Docker networks:
▶ docker network ls
NETWORK ID NAME DRIVER SCOPE
25a03c8453a6 bridge bridge local
00de89cf09d0 host host local
41597d91a389 none null local
然后,将Lambda函数的Docker容器连接到主机网络:
Then, to connect your Lambda function's Docker container to the host network:
▶ sam local invoke "lambda function name" --event event.json \
--docker-network 00de89cf09d0
请注意,您还可以使用环境变量 SAM_DOCKER_NETWORK
:
Note that you can also use the environment variable SAM_DOCKER_NETWORK
:
▶ SAM_DOCKER_NETWORK=00de89cf09d0 sam local invoke "lambda function name" \
--event event.json
如此处所述.
假设主机网络可以访问RDS实例,那应该可以解决您的问题.
Assuming the host network can access the RDS instance, that should fix your problem.
这篇关于在本地运行SAM时如何连接RDS实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!