https://www.wakuwakubank.com/posts/681-ssh-portforward-multistage/
Client -> EC2 -> Redshift
リージョン: オレゴン us-west-2
-- 1. キーペア作成
aws ec2 create-key-pair \
--key-name key123 \
--query "KeyMaterial" \
--output text > key123.pem
cat key123.pem
aws ec2 describe-key-pairs
chmod 400 key123.pem
-- 2. EC2インスタンス作成
aws ec2 run-instances \
--image-id ami-07f3ef11ec14a1ea3 \
--instance-type t3a.nano \
--key-name key123 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=work}]' \
--instance-market-options '{"MarketType": "spot","SpotOptions": {"SpotInstanceType": "one-time"}}' \
--subnet-id subnet-22222222222222222
aws ec2 describe-instances
-- 3. EC2インスタンス接続確認
ssh -i key123.pem ec2-user@192.0.2.1
-- 4. クラスターの作成
aws redshift create-cluster \
--db-name testdb \
--cluster-identifier redshift01 \
--cluster-type single-node \
--node-type dc2.large \
--master-username admin \
--master-user-password 'password' \
--no-allow-version-upgrade \
--no-publicly-accessible \
--automated-snapshot-retention-period 0
aws redshift describe-clusters
aws redshift describe-clusters --cluster-identifier redshift01
amazon-linux-extras list | grep postgresql
sudo amazon-linux-extras install postgresql14 -y
psql --version
PGPASSWORD='password' psql -h redshift01.xxxxxxxxxxxx.us-west-2.redshift.amazonaws.com -p 5439 -d testdb -U admin
※Redshifのポートは5432ではなく5439
-- 6. ポートフォワーディング動作確認
ssh -fNC \
-L [ローカルホストのポート]:[リモートホスト]:[リモートホストのポート] \
-i [EC2(public)の鍵] \
[ユーザー名]@[EC2(public)のアドレス]
ssh -fNC \
-L 15439:redshift01.xxxxxxxxxxxx.us-west-2.redshift.amazonaws.com:5439 \
-i key123.pem \
ec2-user@192.0.2.1
PGPASSWORD='password' psql -h 127.0.0.1 -p 15439 -d testdb -U admin
-- 7. クリーンアップ
-- クラスターの削除
aws redshift delete-cluster \
--cluster-identifier redshift01 \
--skip-final-cluster-snapshot
-- EC2インスタンスの削除
aws ec2 describe-instances
aws ec2 terminate-instances \
--instance-ids i-11111111111111111
-- キーペアの削除
aws ec2 describe-key-pairs
aws ec2 delete-key-pair \
--key-name key123
rm -rf key123.pem