はじめに
「DB接続に挑戦」 の2つ目の記事です。
1つ目の記事はこちらです
DB接続に挑戦(1)ポートフォワードについて&シェルファイルに記載されていることを調べた
目次
やりたいこと
DB接続する前にシェルスクリプトを実行したい。
<手順>
実行権限を付与する
chmod +x test_file.sh
シェルスクリプトを実行する
./test_file.sh
または(sh test_file.sh)
使用するシェルファイル
パス:/Users/test/db/shell
ファイル名:test_file.sh
とします。
1. シェルスクリプトの実行結果
user ~ % cd /Users/test/db/shell
user shell % ls
test_file.sh
user shell % chmod +x test_file.sh
user shell % ./test_file.sh
SSL validation failed for https://ecs.ap-northeast-1.amazonaws.com/ [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1032)
1-1. エラー内容
スクリプトの実行でエラーが発生しました。
AWS CLI実行時のSSL証明書の検証に失敗しているようです。
1-1-1. AWS CLIが正常に動くかチェック
user shell % aws sts get-caller-identity
Unable to locate credentials.
You can configure credentials by running "aws configure".
1-1-2. エラー内容
AWS CLIが使用する認証情報(アクセスキー、シークレットアクセスキー)が設定されていないため発生するエラー。
1 )アクセスキーとシークレットアクセスキーを作成
AWSのコンソールにログイン
↓↓
IAM > ユーザー > 自分のアカウント > 「セキュリティ認証情報」のタブをクリック
↓↓
アクセスキーとシークレットアクセスキーを作成。
シークレットアクセスキーは作成時しか表示されないので、csvのダウンロード&メモを取りました。
2 )シェルファイルの値を変更
次に、シェルファイル内の
export AWS_ACCESS_KEY_ID="XXXX"
export AWS_SECRET_ACCESS_KEY="XXXXX"
を
export AWS_ACCESS_KEY_ID="アクセスキー"
export AWS_SECRET_ACCESS_KEY="シークレットアクセスキー"
に変更して保存。
2. アクセスキーをAWS CLIに設定
アクセスキーを作成したので、AWS CLIに設定します。
ターミナルに戻ります。
user shell % aws configure
AWS Access Key ID [None]: アクセスキーを入力
AWS Secret Access Key [None]: シークレットアクセスキーを入力
Default region name [None]: ap-northeast-1
Default output format [None]: json
2-1. 認証できているか確認
user shell % aws sts get-caller-identity
SSL validation failed for https://sts.ap-northeast-1.amazonaws.com/
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:
self-signed certificate in certificate chain (_ssl.c:1032)
またエラーが発生しました。
SSL の検証エラー(CERTIFICATE_VERIFY_FAILED)が引き続き出ています。
社内のVPNを一時的に切断し、再度以下を実行しました。
user shell % aws configure
AWS Access Key ID [****************HOGE]: アクセスキーを再度入力
AWS Secret Access Key [****************TEST]: シークレットアクセスキーを再度入力
Default region name [ap-northeast-1]: ap-northeast-1
Default output format [json]: json
user shell % aws sts get-caller-identity
{
"UserId": "アクセスキー",
"Account": "何桁かの数字",
"Arn": "arn:aws:iam::000000000:user/user@test.co.jp"
}
aws sts get-caller-identity が正しく返ってきました。
AWSへのCLI接続準備は完了しました。
3. 再度シェルスクリプトの実行
再度実行したいと思います。
user ~ % cd /Users/test/db/shell
./test_file.sh
./test_file.sh: line 41: jq: command not found
3-1. エラーの原因
Macに「jq」がインストールされていないことが原因。
3-1-1. jqをインストール
ターミナルを開き、以下を実行しました。
(分かりにくくなりそうなので、新しくターミナルを開き直しました)
user ~ % brew install jq
==> Auto-updating Homebrew...
Adjust how often this is run with `$HOMEBREW_AUTO_UPDATE_SECS` or disable with
`$HOMEBREW_NO_AUTO_UPDATE=1`. Hide these hints with `$HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Downloading https://ghcr.io/v2/homebrew/core/portable-ruby/blobs/sha256:hogehoge123456789000000000
######################################################################### 100.0%
==> Pouring portable-ruby-3.4.7.arm64_big_sur.bottle.tar.gz
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
aicommit2: Reactive CLI that generates commit messages for Git and Jujutsu with AI
airtable-mcp-server: MCP Server for Airtable
aklomp-base64: Fast Base64 stream encoder/decoder in C99, with SIMD acceleration
ansible@12: Automate deployment, configuration, and upgrading
〜〜省略〜〜
You have 10 outdated formulae installed.
==> Fetching downloads for: jq
✔︎ Bottle Manifest jq (1.8.1) [Downloaded 10.8KB/ 10.8KB]
✔︎ Bottle Manifest oniguruma (6.9.10) [Downloaded 10.0KB/ 10.0KB]
✔︎ Bottle oniguruma (6.9.10) [Downloaded 460.7KB/460.7KB]
✔︎ Bottle jq (1.8.1) [Downloaded 424.6KB/424.6KB]
==> Installing jq dependency: oniguruma
==> Pouring oniguruma--6.9.10.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/oniguruma/6.9.10: 15 files, 1.5MB
==> Pouring jq--1.8.1.arm64_sonoma.bottle.tar.gz
🍺 /opt/homebrew/Cellar/jq/1.8.1: 20 files, 1.2MB
==> Running `brew cleanup jq`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
Removing: /opt/homebrew/Cellar/readline/8.2.13... (51 files, 1.8MB)
Removing: /Users/user/Library/Caches/Homebrew/portable-ruby-3.4.5.arm64_big_sur.bottle.tar.gz... (12.1MB)
Removing: /Users/user/Library/Caches/Homebrew/portable-ruby-3.4.4.arm64_big_sur.bottle.tar.gz... (12.0MB)
Removing: /Users/user/Library/Caches/Homebrew/bootsnap/hogehogehoge00000011111222222... (654 files, 5.6MB)
Removing: /Users/user/Library/Logs/Homebrew/openssl@3... (64B)
Removing: /Users/user/Library/Logs/Homebrew/ca-certificates... (64B)
Removing: /opt/homebrew/var/homebrew/tmp/.cellar/oniguruma... (14 files, 1.5MB)
4. 再度シェルスクリプトを実行
インストールができたので、再度シェルスクリプトを実行します。
user shell % cd /Users/test/db/shell
./test_file.sh
SessionManagerPlugin is not found.
Please refer to SessionManager Documentation here:
http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found
またエラーが発生しました。
4-1. エラーの原因
AWS-SSMのポートフォワードはaws ssm start-sessionを実行する際にSession Manager Pluginが必要だそうです。
4-1-1. Session Manager Pluginをインストール
user ~ % brew install --cask session-manager-plugin
✔︎ JSON API cask.jws.json [Downloaded 15.0MB/ 15.0MB]
✔︎ JSON API formula.jws.json [Downloaded 31.7MB/ 31.7MB]
Warning: session-manager-plugin has been deprecated because it does not pass the macOS Gatekeeper check! It will be disabled on 2026-09-01.
==> Downloading https://session-manager-downloads.s3.amazonaws.com/plugin/1.2.764.0/mac
################################################################################ 100.0%
==> Installing Cask session-manager-plugin
==> Running installer for session-manager-plugin with `sudo` (which may request your pa
Password:
installer: Package name is session-manager-plugin
installer: Installing at base path /
installer: The install was successful.
==> Linking Binary 'session-manager-plugin' to '/opt/homebrew/bin/session-manager-plugi
🍺 session-manager-plugin was successfully installed!
Password:
↑
ここではMacにログインするときのパスワードを入力します。
入力してもターミナルでは表示されないので注意です。
入力が終わってEnterをクリックすると、インストールできました。
インストールできたか確認する
user ~ % session-manager-plugin --version
1.2.764.0
5. 再度シェルスクリプトを実行
user shell % cd /Users/test/db/shell
./test_file.sh
注意)再び以下のエラーが発生するので、VPNは切断して実行します。
SSL validation failed for https://ecs.ap-northeast-1.amazonaws.com/ [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1032)
実行後、以下のようになりました。
user shell % cd /Users/test/db/shell
./tbw_dev_pf.sh
Starting session with SessionId: user@test.co.jp-hogehogehoge1234
Port 56432 opened for sessionId user@test.co.jp-hogehogehoge1234.
Waiting for connections...
Connection accepted for session [user@test.co.jp-hogehogehoge1234]
ポートフォワードが正常に開始され、
自分のローカルの56432ポート → AWS内部のDBにトンネルが繋がりました。👏