1
0

More than 1 year has passed since last update.

vscodeからグローバルIPのないインスタンスにgcloud compute sshする方法

Last updated at Posted at 2022-05-21

俺的Chromebook普段使いプロジェクトの一環で

GCPには Identity Aware Proxy(IAP) という Google 謹製のFirewall件Proxyがあり、グローバルIPアドレスを持たないインスタンスへの接続をいい感じに中継してくれる。もちろん通過するときにはGoogleアカウントで認証されていないとできないのでセキュリティもバッチリ。これが(インスタンスに接続する分には)無料で使える。Google太っ腹!

IAPを使うことで、グローバルIPアドレスを持たないインスタンスに手元の端末から安全にsshすることができる。これでいままで踏み台ホストとか使って、二段sshして・・・といった死ぬほど面倒くさい作業をなくすことができる。

で、vscodeと組み合わせるとグローバルIPアドレスを持たない開発用インスタンスに2クリックで接続していい感じに開発できる。超便利!

別に Cloud Shell でいいじゃんという人もいると思うけど、Cloud Shell はほかっておくとサスペンドしていちいち再接続するのがダルいので私はこの方法のほうが好み。

手順

1. インスタンスの作成

事前にインスタンスを作成する。グローバルIPアドレスはつけない

2. IAPの有効化

VPCファイアウォールで 35.235.240.0/20 からの 22/tcp をすべてのインスタンスで許可する(公式ドキュメント)

[IAM と管理] - [Identity-Aware Proxy] で IAP を有効化する

3. まずは普通に gcloud compute ssh する

まず gcloud compute ssh でインスタンスに接続できることを確認する

daredeshow@desktop:~/Desktop$ gcloud compute ssh instance-1 --zone=asia-northeast1-c --tunnel-through-iap
WARNING: 

To increase the performance of the tunnel, consider installing NumPy. For instructions,
please see https://cloud.google.com/iap/docs/using-tcp-forwarding#increasing_the_tcp_upload_bandwidth

Linux instance-1 5.10.0-13-cloud-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat May 21 11:04:39 2022 from 123.123.123.123
daredeshow@instance-1:~$ 

4. sshの設定を生成して修正する

つぎに --dry-run オプションをつけて同じコマンドを実行すると、sshでどのように実行されるのかが生成される

daredeshow@desktop:~/Desktop$ gcloud compute ssh instance-1 --zone=asia-northeast1-c --tunnel-through-iap --dry-run
/usr/bin/ssh -t -i /home/daredeshow/.ssh/google_compute_engine -o CheckHostIP=no -o HashKnownHosts=no -o HostKeyAlias=compute.4837468964821899300 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/daredeshow/.ssh/google_compute_known_hosts -o ProxyCommand /usr/bin/python3 -S /home/daredeshow/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel instance-1 %p --listen-on-stdin --project=vscode-123456 --zone=asia-northeast1-c --verbosity=warning -o ProxyUseFdpass=no daredeshow@compute.4837468964821899300
daredeshow@desktop:~/Desktop$ 

エディタで上記のコマンドを修正する。ProxyCommand のあとに =" を入れ、-o ProxyUseFdpass=no の前に " を入れる

修正前
/usr/bin/ssh -t -i /home/daredeshow/.ssh/google_compute_engine -o CheckHostIP=no -o HashKnownHosts=no -o HostKeyAlias=compute.4837468964821899300 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/daredeshow/.ssh/google_compute_known_hosts -o ProxyCommand /usr/bin/python3 -S /home/daredeshow/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel instance-1 %p --listen-on-stdin --project=vscode-123456 --zone=asia-northeast1-c --verbosity=warning -o ProxyUseFdpass=no daredeshow@compute.4837468964821899300
修正後
/usr/bin/ssh -t -i /home/daredeshow/.ssh/google_compute_engine -o CheckHostIP=no -o HashKnownHosts=no -o HostKeyAlias=compute.4837468964821899300 -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/home/daredeshow/.ssh/google_compute_known_hosts -o ProxyCommand="/usr/bin/python3 -S /home/daredeshow/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel instance-1 %p --listen-on-stdin --project=vscode-123456 --zone=asia-northeast1-c --verbosity=warning" -o ProxyUseFdpass=no daredeshow@compute.4837468964821899300

5. vscode に投入して修正する

vscode を開き、コマンドパレットから Remote-SSH: Add New SSH Host... を選ぶ

SSHコマンドの部分に上記の修正したsshコマンドをコピペする。保存先は自分の ~/.ssh/config に保存する。

上で保存した ~/.ssh/config を開いて修正する

~/.ssh/config(修正前)
Host /usr/bin/ssh
   HostName /usr/bin/ssh
   IdentityFile /home/daredeshow/.ssh/google_compute_engine
   CheckHostIP no
   HashKnownHosts no
   HostKeyAlias compute.4837468964821899300
   IdentitiesOnly yes
   StrictHostKeyChecking yes
   UserKnownHostsFile /home/daredeshow/.ssh/google_compute_known_hosts
   ProxyCommand /usr/bin/python3 -S /home/daredeshow/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel instance-1 %p --listen-on-stdin --project=vscode-123456 --zone=asia-northeast1-c --verbosity=warning
   ProxyUseFdpass no
~/.ssh/config(修正後)
Host instance-1_gcp
   HostName compute.4837468964821899300
   User daredeshow
   IdentityFile /home/daredeshow/.ssh/google_compute_engine
   CheckHostIP no
   HashKnownHosts no
   HostKeyAlias compute.4837468964821899300
   IdentitiesOnly yes
   StrictHostKeyChecking yes
   UserKnownHostsFile /home/daredeshow/.ssh/google_compute_known_hosts
   ProxyCommand /usr/bin/python3 -S /home/daredeshow/google-cloud-sdk/lib/gcloud.py compute start-iap-tunnel instance-1 %p --listen-on-stdin --project=vscode-123456 --zone=asia-northeast1-c --verbosity=warning
   ProxyUseFdpass no

これで設定完了。

つかいかた

使うときは instance-1_gcp でリモートウィンドウを開くと IAP 経由でインスタンスにssh接続して中のファイルが編集できるようになる。接続後vscodeの中でterminalを開くとインスタンスのターミナルが開く。

以上です!

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