Access resources in a VPC
Beyond E2B, Agent Sandbox Flash Edition provides network configuration for a sandbox in a Virtual Private Cloud (VPC). Use it to reach resources in the VPC and to select the Internet egress method your business requires.
Prerequisites
Before you start, prepare the following:
-
Prepare a VPC in the same region as the sandbox. The VPC must use a Classless Inter-Domain Routing (CIDR) block within an RFC 1918 private address range: 10.0.0.0/8, 172.16.0.0/12, or 192.168.0.0/16. In this VPC, select a vSwitch that resides in a zone supported by Function Compute and has available private IP addresses.
-
Select a security group in the same VPC. The security group must not be managed by another cloud service. Its outbound rules must allow the protocol and port of the target service. If the target resource uses an allowlist, add the sandbox as an allowed source and grant the required access permissions.
Choose an Internet egress method for a template
In a template, use runtime_config.vpc_config to specify the VPC that the sandbox connects to, and use internet_access to control the Internet egress of the platform. If no VPC is specified, the sandbox uses the Internet egress of the platform by default, and the egress IP address is not fixed. After the VPC is connected, select an egress method based on the following table.
|
Access requirement |
internet_access |
Internet egress on the VPC side |
Verification from a new sandbox |
|
Access a VPC, and access the Internet through the platform |
True (default) |
An Internet NAT gateway is not required. |
Request an internal service and a public HTTPS service from the sandbox. Both are accessible, and the egress IP address provided by the platform is not fixed. |
|
Access only the VPC |
False |
The selected vSwitch has no route to the Internet. |
The internal service returns the expected result, and access to the public HTTPS service times out or is rejected. |
|
Access a VPC, and access the Internet through a specified elastic IP address (EIP) |
False |
Configure an Internet NAT gateway, an EIP, and a route and an SNAT entry that cover the selected vSwitch. If a unique source address is required, the matching SNAT entry uses only the specified EIP. |
The internal service is reachable. Query the egress IPv4 address over HTTPS from the sandbox, and the result matches the specified EIP. |
This solution creates or continuously occupies cloud resources that are billed separately. An Internet NAT gateway is billed by instance fee and capacity unit (CU) fee. An EIP is billed by bandwidth or by data transfer. A Simple Log Service (SLS) project and Logstore are billed based on the billing rules of SLS. These three types of resources are not automatically released when the sandbox instance is destroyed or the vSwitch is deleted, and billing does not stop for them either. Release each of them separately when it is no longer needed. A sandbox instance is billed based on vCPU, memory, and disk, and stops incurring fees after it is destroyed. For the fee composition, unit prices, and free quota, see Billing Overview.
The following Python example builds a complete runtime configuration for a template. Replace the VPC, vSwitch, security group, SLS project, Logstore, and sandbox image values with your own. Set internet_access to False if the sandbox accesses only the VPC or uses a self-managed NAT egress. Set it to True if the sandbox uses the Internet egress of the platform.
from alibabacloud_sandbox20260820 import models
runtime_config = models.CreateTemplateRuntimeConfig(
cpu=2,
memory_size=2048,
disk_size=15360,
internet_access=False,
vpc_config=models.CreateTemplateVPCConfig(
vpc_id="<vpc-id>",
v_switch_ids=["<vsw-id>"],
security_group_id="<sg-id>",
),
log_config=models.CreateTemplateLogConfig(
project="<sls-project>",
logstore="<sls-logstore>",
),
sandbox_config=models.CreateTemplateSandboxConfig(
image="<sandbox-image>",
),
)
Assign this object to the runtime_config field of the template creation request. After you submit the template build, wait until the status changes to ready, and then create sandboxes from the template. For the request and status query steps, see Build and manage custom templates with code.
After the configuration is complete, verify the response from the internal service first, and then check Internet access. Run the following commands in the sandbox: Replace the internal address with the address of a service that is already allowed in the same VPC. Make sure the outbound rules of the sandbox allow the targets that this verification requires.
curl --fail --max-time 5 https://api.internal.example.com/health
curl -4fsS --max-time 10 https://ifconfig.me/ip
Scope of the network configurations
-
registry_config.network_config: the network configuration used to access the source image repository during the build. It is used only to pull images. -
runtime_config.vpc_config: the VPC network configuration that is used by the sandbox instances created from the template.
|
Requirement |
Configuration location |
Items to check |
|
Pull images from ACR EE over a VPC |
|
The VPC association of the instance is in effect, the access domain name, the outbound rule for port 443 of the security group, and the pull credentials |
|
Pull images from a self-managed repository in a VPC |
|
The availability of the feature, DNS, routes, the security group, the repository credentials, and the certificate |
|
Access internal services during the sandbox runtime |
|
The connectivity and permissions of the business service |
|
Access the Internet over the platform egress during the sandbox runtime |
|
Whether the platform Internet egress is enabled |
Steps cannot access internal resources over a VPC during the build. Only the Internet is accessible. registry_config.network_config is used only to pull images. runtime_config.vpc_config is used by the sandbox runtime to access the VPC, and it cannot let steps access internal resources. Package the dependencies or files that the build requires into the image in advance, or download them over a stable public HTTPS link. The link must remain valid for later rebuilds, and the file version or the digest must be pinned.
During the sandbox runtime, internet_access=False disables the platform Internet egress. If the VPC itself provides an Internet egress such as an Internet NAT gateway, the Internet may still be accessible.
The following example configures VPC and Internet access for the sandbox runtime:
runtime_config = models.CreateTemplateRuntimeConfig(
cpu=2,
memory_size=2048,
disk_size=15360,
sandbox_config=sandbox_config,
vpc_config=models.CreateTemplateVPCConfig(
vpc_id="<vpc-id>",
v_switch_ids=["<vsw-id>"],
security_group_id="<sg-id>",
),
internet_access=True,
)
Specify a VPC for an individual sandbox
If one template is used in multiple network environments, specify the egress VPC of a single sandbox through metadata when you create it. Serialize the VPC ID, security group ID, and vSwitch ID list into a JSON string, and write the string to fc.sandbox.network.vpc.
Python
Before you run the example, configure the E2B_API_KEY, E2B_API_URL, and E2B_DOMAIN environment variables for the region of Agent Sandbox Flash Edition. Then replace vpcId, securityGroupId, and vSwitchIds in the example with resources in the same region and the same VPC.
import json
import os
from e2b import Sandbox
vpc = {"vpcId": "vpc-example", "securityGroupId": "sg-example", "vSwitchIds": ["vsw-example"]}
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"],
metadata={"fc.sandbox.network.vpc": json.dumps(vpc)},
)
try:
# Replace the address with an internal service that allows access from the CIDR block of the vSwitch.
result = sandbox.commands.run("curl --fail --max-time 5 https://api.internal.example.com/health")
print(result.stdout)
finally:
sandbox.kill()
TypeScript
Before you run the example, configure the E2B_API_KEY, E2B_API_URL, and E2B_DOMAIN environment variables for the region of Agent Sandbox Flash Edition. Then replace vpcId, securityGroupId, and vSwitchIds in the example with resources in the same region and the same VPC.
import { Sandbox } from "e2b";
const vpc = { vpcId: "vpc-example", securityGroupId: "sg-example", vSwitchIds: ["vsw-example"] };
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,
metadata: { "fc.sandbox.network.vpc": JSON.stringify(vpc) },
});
try {
// Replace the address with an internal service that allows access from the CIDR block of the vSwitch.
const result = await sandbox.commands.run("curl --fail --max-time 5 https://api.internal.example.com/health");
console.log(result.stdout);
} finally {
await sandbox.kill();
}
Verify access to VPC resources
Run the following request in the sandbox. Request an internal HTTP service that the sandbox is authorized to access, and replace the example address with the private address of the service. Make sure the response comes from the target service.
curl --max-time 5 https://api.internal.example.com/health
To access a database or a cache, use the corresponding client to connect to the private address and port. If the connection times out, check the route of the vSwitch, the outbound rules of the security group, and the allowlist of the target resource. If a fixed Internet egress is required, see Configure a fixed public IP for a sandbox instance.
FAQ
Why can the sandbox still access the Internet after the Internet egress of the platform is turned off?
After internet_access is turned off, the sandbox can still forward traffic through the VPC that it is connected to. Check the route table of the vSwitch that hosts the sandbox and the matching SNAT entry. If a route points to an Internet NAT gateway associated with an EIP, the sandbox can access the Internet through that gateway. To restrict the sandbox to internal access only, select a vSwitch that has no Internet egress, and verify the internal and Internet access results separately from a new sandbox.
Why does sandbox creation occasionally fail after a VPC is specified?
Check the number of available private IP addresses in the selected vSwitch. If the remaining addresses are insufficient, use a vSwitch with a larger address space, or specify multiple vSwitches that have available IP addresses in supported zones. Make sure that these vSwitches all belong to the specified VPC.
Obtain a fixed Internet egress IP address
To obtain a fixed egress IP address, use the Internet NAT gateway and the elastic IP address (EIP) of the VPC. For more information, see Configure a fixed public IP for a sandbox instance.