LoginSignup
5
2

More than 3 years have passed since last update.

秘密鍵を設定したcircleCIでEC2にSSH接続できない原因は、EC2のセキュリティグループでローカルIP以外のSSH接続を拒否していたからだった

Posted at

ローカルからSSH接続もcapistranoデプロイも正常に可能だから、
EC2側は問題ないという灯台下暗し

# 問題

circleCIのDeployフェーズで毎回SSH接続がTimeoutする

bundle exec cap production deploy 

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as <user名>@xx.xxx.xx.249: Net::SSH::ConnectionTimeout

Caused by:
Net::SSH::ConnectionTimeout: Net::SSH::ConnectionTimeout

Caused by:
Errno::ETIMEDOUT: Connection timed out - connect(2) for xx.xxx.xx.249:22

Tasks: TOP => rbenv:validate 
(See full trace by running task with --trace)

解決

VPCにアタッチしているセキュリティグループの
インバウンドルールを後述のように変更した

SSH     TCP     22      <ローカルIP>
SSH     TCP     22      0.0.0.0/0

検証

@xx.xxx.xx.249(EC2のElastic IP)は適切
Terminalから同じ秘密鍵を使用してEC2ログイン可能
ローカル環境ではbundle exec cap production deployは成功する

原因

AWSのVPCに割り当てられたセキュリティグループが、
SSH接続(port: 22)でローカル環境のIPアドレスのみを許可する設定になっていた

Qiitaの記事などを参考に手順どおりEC2へのデプロイ環境を構築してきた場合、同様に遭遇する可能性が高いと考えます

5
2
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
5
2