1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon Linux 2023 から Aurora PostgreSQL 17 に接続する手順

1
Posted at

はじめに

EC2(Amazon Linux 2023)から、Aurora PostgreSQL 17 クラスターに psql で接続するまでの手順をまとめます。SSL 証明書を使った安全な接続と、AWS Secrets Manager を使ったパスワード管理も合わせて解説します。


前提条件

  • EC2 インスタンス(Amazon Linux 2023)が Aurora クラスターと同じ VPC 内、または VPC ピアリング・踏み台経由で疎通できる状態であること
  • EC2 インスタンスに、以下の権限を持つ IAM ロールがアタッチされていること
    • secretsmanager:GetSecretValue
  • Aurora PostgreSQL 17 クラスターおよびデータベース・ユーザーが作成済みであること

手順

1. PostgreSQL クライアントのインストール

Amazon Linux 2023 には psql が含まれていないため、インストールします。

sudo dnf install postgresql17-server -y

postgresql17-server パッケージをインストールすることで psql コマンドも利用できるようになります。


2. PostgreSQL の初期化と起動(ローカル接続確認用・任意)

psql コマンドの動作確認のためにローカルの PostgreSQL を起動したい場合は以下を実行します。Aurora への接続のみが目的であれば、この手順はスキップしても構いません。

sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl status postgresql

3. SSL 証明書の配置

Aurora に sslmode=verify-full で接続するには、AWS が提供するルート証明書が必要です。

sudo mkdir -p /certs
sudo curl -o /certs/global-bundle.pem https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
sudo chmod 644 /certs/global-bundle.pem

global-bundle.pem は AWS の全リージョン向けルート CA 証明書バンドルです。


4. Aurora への接続

接続先のホスト名を環境変数に設定しておくと管理しやすくなります。

export RDSHOST="<クラスターエンドポイント>"

次に、AWS Secrets Manager からパスワードを取得しながら psql で接続します。

psql "host=$RDSHOST port=5432 dbname=<データベース名> user=postgres sslmode=verify-full sslrootcert=/certs/global-bundle.pem password=$(aws secretsmanager get-secret-value --secret-id '<シークレットARN>' --query SecretString --output text | jq -r '.password')"

接続に成功すると以下のように表示されます。

psql (17.7, server 17.4)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
Type "help" for help.

yourdb=>

TLSv1.3 で暗号化された SSL 接続が確立されていることが確認できます。


5. psql の終了

psql を終了する際は \q を使います。

yourdb=> \q

exitexit() は psql では使えません。\q が正しいコマンドです。


よくあるエラーと対処法

root certificate file "/certs/global-bundle.pem" does not exist

証明書ファイルが存在しません。手順 3 の証明書配置を実施してください。


could not translate host name "port=5432" to address: Name or service not known

$RDSHOST が空になっています。su - などでユーザーを切り替えると環境変数がリセットされるためです。

# 再度設定する
export RDSHOST="<クラスターエンドポイント>"

接続前に echo $RDSHOST で値が設定されていることを確認してから実行してください。


Unable to locate credentials

EC2 インスタンスに IAM ロールがアタッチされていないか、ロールに secretsmanager:GetSecretValue 権限がありません。AWS マネジメントコンソールから EC2 インスタンスに適切な IAM ロールをアタッチしてください。


まとめ

手順 内容
1 postgresql17-server をインストール
2 AWS の SSL 証明書を /certs/global-bundle.pem に配置
3 $RDSHOST 環境変数を設定
4 Secrets Manager からパスワードを取得して psql で接続

環境変数はユーザー切り替え時にリセットされる点と、SSL 証明書の配置を忘れずに行う点が特にハマりやすいポイントです。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?