title: How to make my app on EC2 access Redis on ECS Fargate?
body: <p>I have a software infra on AWS where part of the app runs on an EC2 instance and another part on ECS Fargate cluster.</p>
  <p>I'm having trouble getting my application, which runs on the EC2 instance, to access a specific container that runs on ECS. More clearly, I need to make my application on EC2 access Redis on ECS. Is possible to do this kind of integration?</p>
  <p>Both parts of the application are in the same VPC and have Security Groups that authorize access to each other.</p>
  <p><strong>More context:</strong></p>
  <p>Every time the Task Definition is provisioned in ECS Fargate, my public/private network IP changes on cluster service. To be able to access the services, Amazon provides the Service Discovery configuration, which updates a DNS hosted zone whenever there is a change in the ECS network.</p>
  <p>My problem is using this hosted zone within my EC2 instances. I can access ECS containers directly via IP, but this is not the ideal scenario. The IP address constantly changes whenever the service is provisioned. On the other hand, on my EC2 instances, the Route 53 nameserver always appears as unknown.</p>
  <p>My cloud is configured as follows:</p>
  <p><strong>Task Definition</strong></p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;family&quot;: &quot;******&quot;,
    &quot;requiresCompatibilities&quot;: [&quot;FARGATE&quot;],
    &quot;executionRoleArn&quot;: &quot;arn:aws:iam::******&quot;,
    &quot;networkMode&quot;: &quot;awsvpc&quot;,
    &quot;runtimePlatform&quot;: {
      &quot;operatingSystemFamily&quot;: &quot;LINUX&quot;
    },
    &quot;cpu&quot;: &quot;2048&quot;,
    &quot;memory&quot;: &quot;4096&quot;,
    &quot;containerDefinitions&quot;: [
      {
        &quot;name&quot;: &quot;redis&quot;,
        &quot;image&quot;: &quot;redis:alpine&quot;,
        &quot;portMappings&quot;: [
          {
            &quot;containerPort&quot;: 6379,
            &quot;hostPort&quot;: 6379
          }
        ]
      }
    ]
  }
  </code></pre>
  <p><strong>ECS Service config</strong><br />
  ECS service network settings:</p>
  <p><a href="https://i.stack.imgur.com/Gsmi8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gsmi8.png" alt="ECS Service config" /></a></p>
  <p><strong>Route 53</strong><br />
  Container auto discovery is enabled for the service in ECS:</p>
  <p><a href="https://i.stack.imgur.com/htCYP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/htCYP.png" alt="Route 53 Container auto discovery" /></a></p>
  <p>The A record IP is updated whenever the ECS service is provisioned.</p>
  <p><strong>ECS Security Group Inbound Rules for my EC2 instance:</strong></p>
  <p>My ECS container is open for connections from my EC2 instance. Connections to Redis via ECS service IP are successful, but connections via generated nameserver aren't.</p>
  <p><a href="https://i.stack.imgur.com/4Ww2V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Ww2V.png" alt="Security Group" /></a></p>