将 Heroku 应用程序指向 AWS RDS 数据库 [英] Point Heroku application to AWS RDS database
问题描述
我需要将 Heroku 应用程序指向我的 AWS RDS 数据库.我的 RDS 数据库已启动并正在运行,并且有一个具有 0.0.0.0/0 访问权限的安全组.
I need to point my Heroku application to my AWS RDS database. My RDS database is up and running and has a security group with 0.0.0.0/0 access.
目前,我已经删除了我的 Heroku postgreSQL 数据库,并且我正在尝试将我的 Heroku 应用程序指向我的 RDS 数据库.由于某种原因,我的应用程序崩溃了.我相信我正在进行的步骤是在 Heroku 端设置我的 DATABASE_URL.
Currently, I've removed my Heroku postgreSQL database and I am attempting to point my Heroku application to my RDS database. For some reason my application is crashing. The step that I believe I am on is setting my DATABASE_URL on the Heroku side.
假设我的数据库凭据是:
Let's say that my database credentials are:
数据库实例:mydb
数据库名称:mydb
用户: wcronyn
pass:密码
db instance: mydb
dbname: mydb
user: wcronyn
pass: password
我试过了:
heroku config:set DATABASE_URL=postgres://wcronyn:password@mydb.XXXXXXX.us-east-1.rds.amazonaws.com:5432/mydb
heroku config:set DATABASE_URL=postgres://wcronyn:password@mydb.XXXXXXX.us-east-1.rds.amazonaws.com:5432/mydb
并且我尝试通过将 .pem 文件下载到我的配置文件夹中然后引用它来设置权限:
and I've attempted to set the permissions by downloading the .pem file into my config folder and then referencing it:
DATABASE_URL=postgres://wcronyn:password@mydb.XXXXXXX.us-east-1.rds.amazonaws.com:5432/mydb?sslca=config/amazon-rds-ca-cert.pem&sslmode=require&加密=真
DATABASE_URL=postgres://wcronyn:password@mydb.XXXXXXX.us-east-1.rds.amazonaws.com:5432/mydb?sslca=config/amazon-rds-ca-cert.pem&sslmode=require&encrypt=true
我已经尝试过这两个数据库网址,但我的应用程序一直崩溃.
I have tried these two database urls but my application keeps crashing.
有人可以概述成功托管我的 RDS 数据库并将我的应用程序指向它需要采取的步骤吗?
Can someone outline the steps that I need to take to successfully host my RDS database and point my application to it?
推荐答案
鉴于以下设置,以下步骤对我有用(2017 年 2 月):
The following steps worked for me (Feb 2017), given the following setup:
- AWS RDS 区域 eu-west-2(使用 VPC 安全组,而不是 DB安全组)
- Postgres 9.6
- Heroku,托管 Flask 应用程序(例如 appname:
heroku-app-stage
) - 将远程添加到 Heroku 应用程序的 Git(例如远程:
stage
) postgresql://username:password@awsrdshost:5432/dbname
的 DATABASE_URL 值
- AWS RDS Region eu-west-2 (which uses VPC Security Groups, not DB Security Groups)
- Postgres 9.6
- Heroku, hosting a Flask application (eg appname:
heroku-app-stage
) - Git with a remote added into the Heroku app (eg remote:
stage
) - DATABASE_URL value of
postgresql://username:password@awsrdshost:5432/dbname
大致有四个步骤:
- 下载 Amazon RDS SSL 根证书并将其安装到您的 Heroku 应用程序中
- 将您的 Heroku 应用程序配置为引用所述根证书
- 在您的 RDS 实例上启用 SSL
- 配置您的 RDS 安全组以允许传入流量的所有 IP 地址范围
下载并安装 Amazon RDS SSL 根证书
- 从下面的 Amazon RDS 链接下载 .pem 证书文件.
- 将文件放入您的应用程序文件夹(记下位置,我已将其与我的 .py 文件放在根文件夹中)
- 将该文件提交到您的 git 存储库中,并将所述提交推送到 Heroku 远程(
git push stage master
) - 验证证书是否已上传到预期路径(您可以执行
heroku run bash --app heroku-app-stage
以在 dyno 中查看您的文件)
- Download the .pem certificate file from the Amazon RDS link below.
- Put the file into your app folders (make a note of location, I have placed it in my root folder with my .py files)
- Commit that file into your git repository, and push said commit into the Heroku remote (
git push stage master
) - Verify that the certificate has been uploaded into the expected path (you can do
heroku run bash --app heroku-app-stage
to see your files in the dyno)
配置 Heroku 以引用根证书
- 通过 Heroku 仪表板,导航到
heroku-app-stage
,转到设置"选项卡,然后单击显示配置变量" - 通过添加
?sslrootcert=rds-combined-ca-bundle.pem&sslmode=require
来更新您的 DATABASE_URL 变量.新值现在应该是postgresql://username:password@awsrdshost:5432/dbname?sslrootcert=rds-combined-ca-bundle.pem&sslmode=require
- Via the Heroku dashboard, navigate to
heroku-app-stage
, go to Settings tab, and click on Reveal Config Vars - Update your DATABASE_URL variable by adding
?sslrootcert=rds-combined-ca-bundle.pem&sslmode=require
. The new value should now bepostgresql://username:password@awsrdshost:5432/dbname?sslrootcert=rds-combined-ca-bundle.pem&sslmode=require
请注意,此答案使用根证书;可能还有其他选项可能是您想要的,在这种情况下,请参考以下 SO:
Note that this answer uses a root certificate; there may be other options which may be what you want in which case refer to the following SO:
在您的 RDS 实例上启用 SSL
- 通过您的 RDS 控制台,导航到您的实例详细信息并记下它正在使用的参数组
- 转到仪表板上的参数组"屏幕
- 如果您使用默认参数组,则需要创建另一个参数组,因为您将无法编辑默认参数组.
- 修改 force_ssl 参数使其值为 1 并保存.
- 验证您的 RDS 实例现在是否启用了 SSL.如果您运行
psql postgres -h awsrdshost -p 5432 -U username
,您应该会在连接详细信息中看到 SSL
- Via your RDS console, navigate to your instance details and note down the Parameter Group that it is using
- Go to the Parameter Group screen on the dashboard
- If you are using the default parameter group, you will need to create another parameter group, as you will not be able to edit the default one.
- Modify the force_ssl parameter to have value 1 and save.
- Verify that SSL is now enabled on your RDS instance. If you run
psql postgres -h awsrdshost -p 5432 -U username
, you should see SSL in the connection details
配置 RDS 安全组以允许所有传入 IP 范围
- 通过您的 RDS 控制台,检查您的实例的活动安全组
- 导航到 EC2 控制台(在 Compute > EC2 下),然后选择 Security Groups
- 选择相关的安全组(从第 1 步开始)并转到底部的入站"选项卡.您应该会看到其中列出了一个 PostgreSQL 项目.如果您点击编辑",您应该可以选择将源"更改为任何地方".
注意:仅当您使用使用 VPC 安全组的 RDS 设置时,说明才相关
Note: instructions are only relevant if you're using an RDS setup that uses VPC Security Groups
就是这样!
所用参考页面的链接:
Amazon 的 Postgres SSL 指南http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts.General.SSL
Amazon's guide to SSL on Postgres http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts.General.SSL
Heroku 的(非常简短的)Amazon RDS 指南https://devcenter.heroku.com/articles/amazon-rds
Heroku's (very short) guide to Amazon RDS https://devcenter.heroku.com/articles/amazon-rds
这篇关于将 Heroku 应用程序指向 AWS RDS 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!