blu*_*e13 5 amazon-web-services amazon-rds docker aws-lambda aws-sam-cli
aws ssm start-session --target <instance-id> --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters host="mydb.example.us-east-2.rds.amazonaws.com",portNumber="3306",localPortNumber="3306"
Run Code Online (Sandbox Code Playgroud)
http://127.0.0.1:3306/这样的 GUI在本地连接到它。无需使用 SSH。http://127.0.0.1:3306/,则会收到错误Connection refused。127.0.0.1解析为 docker 容器的本地主机而不是我机器的本地主机。host.docker.internal ... resolves to the internal IP address used by the hosthttp://host.docker.internal:3306/,则会收到错误Name or service not known。我在https://github.com/bluprince13/sam-app-connect-to-host-localhost创建了一个 MWE 。我们可以在本地运行一个 Python 服务器,然后尝试让 lambda 连接到它,而不是尝试连接到数据库。
提供的 MWE 看起来配置正确。问题出在 docker 配置上。正如OP所指出的,配置中有一个覆盖。dns(Docker -> 首选项 -> Docker 引擎)被覆盖。删除后,一切正常host.docker.internal。
一般来说,要从容器连接到主机的本地主机,您必须host.docker.internal在mac和windows中使用。有关其他平台中的配置,请参阅 SO帖子。针对 macOS 中的 SAM,建议具有以下内容,以避免参数硬编码:
Environment变量template.yaml。
Properties:
Environment:
Variables:
DB_HOST: *your_database_url*
Run Code Online (Sandbox Code Playgroud)
env.json使用以下配置创建一个文件。 {
"*Logical_ID of your resource*": {
"DB_HOST": "host.docker.internal"
}
}
Run Code Online (Sandbox Code Playgroud)
env.jsonSAM sam local invoke --env-vars env.json。| 归档时间: |
|
| 查看次数: |
1651 次 |
| 最近记录: |