25
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HerokuアプリケーションのDBをAmazonRDSにする

Posted at

HerokuアプリケーションのデフォルトDBであるPostgreSQLをAmazonRDSに変更する手順を記します。
https://devcenter.heroku.com/articles/amazon_rds

環境

MacOSX

準備

Herokuのアプリケーション
Amazon Web Serviceのアカウント
Amazon RDSのインスタンス(us-eastリージョンのみ)
Amazon RDS Command Line Toolkit
・brew install rds-command-line-tools
Gem
・gem install heroku
・gem install sqlite3

AWSの準備

環境変数

~/.bashrcとか~/.bash_profileとか
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Home"
export EC2_PRIVATE_KEY="$(/bin/ls $HOME/.ec2/pk-aws-account.pem)"
export EC2_CERT="$(/bin/ls $HOME/.ec2/cert-aws-account.pem)"
export AWS_RDS_HOME="/usr/local/Cellar/rds-command-line-tools/1.12.002/libexec"

EC2_PRIVATE_KEYとEC2_CERTは下記の手順でダウンロードして.ec2フォルダに配置する
rds-command-line-toolsのバージョンとか自分の環境に合わせる
AWS Management Console > Security Credentials > X.509 Certificates

インスタンス情報の取得(疎通確認)

> rds-describe-db-instances
DBINSTANCE  test_databases  2013-08-05T15:59:54.972Z  db.t1.micro  mysql  20  test_databases  available  test_databases.camuimfljs7b.us-east-1.rds.amazonaws.com  3306  us-east-1a  1  n  5.6.12  general-public-license
      SECGROUP  default  active
      PARAMGRP  default.mysql5.6  in-sync
      OPTIONGROUP  default:mysql-5-6  in-sync

インスタンス情報が返ってくれば、rds-command-line-toolsでAPIにアクセスできることが確認できる

Heroku設定

Herokuからの通信許可

> rds-authorize-db-security-group-ingress \
--db-security-group-name default --ec2-security-group-name default \
--ec2-security-group-owner-id 098166147350

SECGROUP  default  default
      EC2-SECGROUP  default  sg-2aca2f43  098166147350  authorizing
      IP-RANGE  1.1.1.1/32         authorized
      IP-RANGE  118.109.37.163/32  authorized

HerokuアプリケーションにAddOnとしてAmazonRDSの追加

> heroku addons:add amazon_rds --url=mysql2://test_databases:test_databases@test_databases.camuimfljs7b.us-east-1.rds.amazonaws.com/test_databases
Adding amazon_rds on test_databases... done, v10 (free)
Use `heroku addons:docs amazon_rds` to view documentation.

アプリケーションに設定されているDBを確認する

> heroku config:get DATABASE_URL
mysql2://test_databases:test_databases@test_databases.camuimfljs7b.us-east-1.rds.amazonaws.com/test_databases

参考

Heroku で Flask + SQLAlchemy + Amazon RDS をやってみる。
http://d.hatena.ne.jp/tell-k/20111106/1320583957

25
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?