0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC2のセキュリティグループでPortを開いたのに接続できない問題

Posted at

この記事のタイトルを悩みましたが、根本原因はEC2でのポート設定問題でしたので、このタイトルにした。
この問題に辿り着いたのは下記の問題からである。

-EC2のUbuntuサーバでMongoDBを設定したが、Lambdaから繋がらない
-ローカルからUbuntuにあるMongoDBへ接続を試したが、繋がらない
-セキュリティグループなど全部正確に設定したのに、Telnet IP portしても繋がらない(Port 80は繋がる)

この記事を参考にして、ローカルからMongoDBへ接続しようとしたが失敗。
https://ianlondon.github.io/blog/mongodb-auth/
ローカルからEC2上のMongoDBへ接続するガイドとしてはほぼ完全だと思う。但し、この通りに
設定内容を何度も確認しても繋がらないですね。。。

エラーメッセージは
Error: couldn't connect to server (IP):27017, connection attempt failed: SocketException: Error connecting to (IP):27017 :: caused by :: Operation timed out :
タイムアウトですね。Firewall関連だとは分かったが、Security Groupもちゃんと設定しているし、ネットワークACLも全てAllowしているし、何が悪いの?で何時間もイライラしていました。

# telnet IP 80
Connected to ec2-IP.ap-northeast-1.compute.amazonaws.com.
Escape character is '^]'.

# telnet IP 27017
Trying IP...

上記のようにローカルからポート確認をしたら、どうやら27017は開いてないようですね。
普段、毎日サーバ関連を弄っている訳ではないので、正直ネットワーク関連は苦手なんですね。EC2のセキュリティグループでちゃんと設定したのに、80はよくて他のポートがダメな理由が分かりませんでした。
さらに調べて、どうやらサーバ上でiptableを弄る必要があるみたいなので、下記の記事を参考した。

https://qiita.com/hana_shin/items/a630871dce209cff04f3
Ubuntuでのiptableの設定方法、utwのコマンドを分かりやすく説明しているのです。

まず、ufwで既存のルールを確認するとやはり27017は開いてない状態ですね。

# sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp (OpenSSH)           ALLOW IN    Anywhere                  
80,443/tcp (Nginx Full)    ALLOW IN    Anywhere                  
22/tcp (OpenSSH (v6))      ALLOW IN    Anywhere (v6)             
80,443/tcp (Nginx Full (v6)) ALLOW IN    Anywhere (v6)

それで、27017を設定すると正確にルールが追加されていることが分かる。

# sudo ufw allow 27017
Rule added
Rule added (v6)

# sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] OpenSSH                    ALLOW IN    Anywhere                  
[ 2] Nginx Full                 ALLOW IN    Anywhere                  
[ 3] 27017                      ALLOW IN    Anywhere                  
[ 4] OpenSSH (v6)               ALLOW IN    Anywhere (v6)             
[ 5] Nginx Full (v6)            ALLOW IN    Anywhere (v6)             
[ 6] 27017 (v6)                 ALLOW IN    Anywhere (v6)      

これでTelnet IP 27017が繋がるようになり、MongoDBへのアクセスも可能となった!長い旅だった!ネットワークに詳しい友達が欲しいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?