Skip to content
Published on

Static IPs for your Next.js functions

Authors
  • Name
    Mark Alexis

Serverless platforms, like Vercel, greatly simplify the process of deploying applications online. Not having to worry about servers, daemons, reverse proxies or even TLS greatly speeds up development cycles. However there are pitfalls, such as needing a static IP. Most commonly you may need a static IP to whitelist so that you can access an internal api or connect to a database. Noble IP is a service designed to easily solve this problem.


One limitation of serverless platforms is that typically you cannot control the server where your application runs. In most cases this is a win - you do not typically want to care about server details. As your product grows, and as you interface with larger clients, you may have needs that require you to provide a static IP address. For example, you might want to connect to a Postgres database, but the operator of that database requires a whitelisted IP. Vercel, and other serverless platforms, are unable to guarantee a single IP for your functions, or do so with the addition of a costly enterprise license.

Noble IP is a cheaper alternative that is easy to setup and use; and is built with serverless platforms in mind. Typically this is solved with the use of a proxy, and it is the preferred way of connecting to HTTP resources such as APIs. Alternatively, Noble IP provides a solution for when a proxy might not work, such as connecting to a database, called port leases.

HTTP Proxies with Vercel Serverless Functions

First, Sign Up for a Noble IP account. It's free to get started and for projects that do under 75MB/month in bandwidth. This tends to be just enough for light usage for one-off clients.

Start by creating an outbound proxy in the Noble IP dashboard.

Create Outbound Proxy

Once created, you will be shown a screen with the proxy credentials you need.

Credentials

Next add the node-fetch and hpagent packages:

shell
npm install node-fetch
npm install hpagent

Finally, you can create your handler like so:

js
import fetch from "node-fetch";
import { HttpProxyAgent } from "hpagent";

export default async function handler(req, res) {
  try {
    const response = await fetch("https://ifconfig.me/ip", {
      agent: new HttpProxyAgent({
        keepAlive: true,
        proxy: "https://noble:fp_NOBLETOKEN@p-12345.noble-ip.com:3129",
      }),
    });
    const ip = await response.text();

    res.status(200).json({ ip: ip });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: "Failed to fetch IP address" });
  }
}

If all is successful, you should see your assigned IP from Noble IP.

Connect to a database with a static IP

HTTP and SOCKS proxies are broadly useful, however, sometimes the resource you are trying to access does not support use proxies. For example, if you want to connect to postgres database using a static IP your driver may not support adding a proxy. For this, Noble IP provides port leases. This is an API that will allocate a port for you that will forward the connection to the destination server.

This is useful for when you need to connect to a service, such as a database.

js
const postgres = require("postgres");

async function createLease() {
  const leaseParams = {
    target_host: "my.postgres.server.corp",
    target_port: 5432,
    proxy_token: "fp_xxxxxxxx",
  };

  const response = await fetch("https://api.noble-ip.com/v1/leases", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(leaseParams),
  });

  const lease = await response.json();
  return lease;
}

async function runQuery() {
  const lease = await createLease();

  // Replace these values with your actual database connection details
  const sql = postgres(
    `postgres://username:password@${lease.hostname}:${lease.port}/database`,
    {
      ssl: { rejectUnauthorized: false, servername: "my.postgres.server.corp" },
    }
  );

  try {
    const result = await sql`SELECT 1;`;
    console.log(result);
    // Properly terminate the connection
    sql.end();
  } catch (error) {
    console.error("Error running query:", error);
    sql.end();
  }
}

Many users use this functionality to bridge their internal databases with Vercel or Koyeb without whitelisting large swaths of IP ranges.

If you need further help in cloud architecture or deploying your application securely, do not hesitate to Contact Us.

Sign Up for a Noble IP account.