http.get from async lambda timeout trying to reach public EC2

I'm trying to figure out what I'm missing in this context.

I have an EC2 running two docker container. One is a python REST api, the other contains nginx which redirect to the python server.

Sending a GET request to my /test endpoint from the browser or postman returns the expected value.

I tried accessing the same public url from an AWS Lambda but the request timeout.

Here's my current situation

  • The EC2 url is public and accessible from my browser.
  • The Lambda can reach www.google.com sucessfully.
  • The Lambda can't reach the EC2 url.
  • The Lambda doesn't have a VPC.
  • The Lambda is behind an API Gateway (websocket).
  • Tried a high timeout value (30sec+).

The issue probably comes from my lack of knowledge of the AWS environment. Can anyone see the problem in the following code?

const AWS = require('aws-sdk');
require('./patch.js');
var http = require('http');

function test(url){
  return new Promise((resolve, reject) => {
    const req = http.request(url,
      (res) => {
        let body = '';
        res.on('data', (chunk) => (body += chunk.toString()));
        res.on('error', reject);
        res.on('end', () => {
          if (res.statusCode >= 200 && res.statusCode <= 299) {

            resolve({statusCode: res.statusCode, headers: res.headers, body: body});
          } else {
            reject('Request failed. status: ' + res.statusCode + ', body: ' + body);
          }
        });
      });
    req.on('error', reject);
    req.write('', 'binary');
    req.end();
  });
}


exports.handler = async(event) => {  
  let googleUrl = "http://www.google.com";
  let ec2Url = "XXXXX"; // Public EC2 url

  await test(googleUrl).then(function(res){
        console.log("Response",res); // This is logged 
    });

  await test(ec2Url).then(function(res){
        console.log("Response",res); // This is never logged. Timeout kicked in.
    });
  return {};
};

Answers:

Answer

Turns out I didn't save the security group after editing it. Check you security group inbound rules if you run into a similar problem.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.