Private Access

Updated at:

PrivateLink enables you to establish secure and stable private connections between your VPC and services on Alibaba Cloud. It simplifies network architecture, enables private access to services, and avoids potential security risks associated with public network access. We have enabled endpoint services in all supported regions. You can connect through endpoints to access your services over the internal network.

If this is your first time, please activate the PrivateLink service first.

Create an Endpoint

Log in to the PrivateLink console and select Interface Endpoint to create an endpoint.

In Basic Settings, fill in the Region and Endpoint Name, set Type to Alibaba Cloud Service, and select com.aliyuncs.privatelink.<region>.fc.e2b for Alibaba Cloud Service.

Configure the VPC, Zone and vSwitch, and Security Group (create the corresponding resources if you don't have them).

In Advanced Settings, set Enable Custom Domain Name? to Enable.

Click the OK button to create, then check the status. If the Custom Domain Name is not enabled, enable it manually.

You can then access the service through the Custom Domain Name within your VPC.

Start a Service and Access via Private Network

Set the environment variables E2B_API_URL and E2B_DOMAIN to the private network endpoints.

export E2B_API_KEY="<your-api-key>"
export E2B_API_URL="https://api.<region>-vpc.e2b.fc.aliyuncs.com"
export E2B_DOMAIN="<region>-vpc.e2b.fc.aliyuncs.com"

The following code must be run within your VPC.

TypeScript example:

import { Sandbox } from "e2b";

const sandbox = await Sandbox.create("code-interpreter-v1", {
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
});

try {
  const process = await sandbox.commands.run("python3 -m http.server 8000", {
    background: true,
  });

  const host = sandbox.getHost(8000);
  const url = `https://${host}`;
  console.log(url);

  const response = await fetch(url);
  console.log(await response.text());

  await process.kill();
} finally {
  await sandbox.kill();
}

Python example:

import os
import urllib.request
import time

from e2b import Sandbox

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
)

try:
    process = sandbox.commands.run(
        "python3 -m http.server 8000",
        background=True,
    )
    time.sleep(2)  # Wait for http.server to start
    host = sandbox.get_host(8000)
    url = f"https://{host}"
    print(url)

    with urllib.request.urlopen(url, timeout=10) as response:
        print(response.read().decode())

    process.kill()
finally:
    sandbox.kill()