title: How to configure security groups when connecting to mongodb between AWS ec2 instances?
body: <p>I have one ec2 instance with mongodb (called ec2-mongo) with the security group, sg-mongodb-server. And I am trying to connect to mongodb via another ec2 instance (called ec2-connect) with the security group, sg-connect. I want to set the rules to allow for traffic between the two security groups without referring to specific IP Addresses. How can I do this?</p>
  
  <p>I can connect to mongo when my security groups are set up as follows (so there is nothing wrong with the servers as far as I know). Outbound is set to all:</p>
  
  <p><strong>sg-mongodb-server</strong>:</p>
  
  <pre><code>Inbound:
  Port    Source
  22      my_ip
  27017   ip_of_ec2-connect
  </code></pre>
  
  <p><strong>sg-connect</strong>:</p>
  
  <pre><code>Inbound:
  Port    Source
  22      my_ip
  27017   ip_of_ec2-mongo
  </code></pre>
  
  <p>but when I try to use security groups instead of IP Addresses I can't connect to mongodb.</p>
  
  <p><strong>sg-mongodb-server</strong>:</p>
  
  <pre><code>Inbound:
  Port    Source
  22      my_ip
  27017   sg-connect
  </code></pre>
  
  <p><strong>sg-connect</strong>:</p>
  
  <pre><code>Inbound:
  Port    Source
  22      my_ip
  27017   sg-mongodb-server
  </code></pre>
  
  <p>Error message when trying to connect:</p>
  
  <pre><code>MongoDB shell version: 3.2.3
  connecting to: XXX.XXX.XXX.XXX/test
  2016-02-19T18:24:09.245+0000 W NETWORK  [thread1] Failed to connect to XXX.XXX.XXX.XXX:27017 after 5000 milliseconds, giving up.
  2016-02-19T18:24:09.245+0000 E QUERY    [thread1] Error: couldn't connect to server XXX.XXX.XXX.XXX:27017, connection attempt failed :
  connect@src/mongo/shell/mongo.js:226:14
  @(connect):1:6
  
  exception: connect failed
  </code></pre>