Search code examples
amazon-web-servicesload-balancingsendgridprivate-subnet

Using Sendgrid from server with no public ip address


Our servers are located in AWS in Load Balancer and private subnet. As recommended by a security company, the servers need to have no public IP address as it already uses Load Balancer and private subnet.

However, removing the public IP address from the server makes sending email via Sendgrid unable to work.

$apiKey = getenv('SENDGRID_API_KEY'); $sg = new \SendGrid($apiKey);

try {
    $response = $sg->client->_("suppression/bounces")->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

The code above returns nothing, as if the process hits an infinite loop

However, adding public IP address back makes Sendgrid works again.

Is there a workaround for this?

Placing AWS servers in Load Balancer and private subnet has become the security standard nowadays, I hope you can guide me on how to make this works with Sendgrid.

Please advise.

Thank you

Tried:

try {
    $response = $sg->client->_("suppression/bounces")->get();
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '.  $e->getMessage(). "\n";
}

Expecting: Getting response data or at least exception message.


Solution

  • You can using NAT Gateway for private subnet https://docs.aws.amazon.com/vpc/latest/userguide/nat-gateway-scenarios.html#public-nat-internet-access