How do I access private VPC resources in a function?

Updated at:

Function Compute allows functions to access specific virtual private clouds (VPCs), giving them direct access to private resources such as databases, caches, and internal services hosted within that VPC.

How it works

When you enable VPC access for a function, Function Compute connects the function to the specified VPC. The function can then reach any resource in the VPC using its private IP address — no public internet required.

Prerequisites

Before you begin, ensure that you have:

  • A VPC with at least one vSwitch

  • A security group in the same VPC

  • Enough available IP addresses in the vSwitch (see Usage notes)

Configure VPC access for a function

Configure the following network settings for your function. For step-by-step instructions, see Configure network settings.

ParameterDescription
VPC IDThe ID of the VPC that contains the private resources your function needs to reach
vSwitch IDThe ID of the vSwitch (subnet) within the VPC where the function runtime is placed
Security group IDThe ID of the security group that controls inbound and outbound traffic for the function

Access a VPC resource from a function

After VPC access is configured, connect to private resources using their VPC-internal IP addresses. The following example connects to a Redis instance at the private IP address 10.0.0.100.

import redis

def handler(event, context):
    # Connect to the Redis instance using its private IP address within the VPC
    client = redis.StrictRedis(host='10.0.0.100', port=6379, db=0)
    client.set('key', 'value')
    return client.get('key')

Replace 10.0.0.100 with the actual private IP address of your resource, and adjust the connection parameters for your specific service (RDS, Redis, or other VPC-hosted services).

Usage notes

  • Provision enough IP addresses in the vSwitch. The vSwitch must have more available IP addresses than the maximum number of concurrent function instances you expect to run. If IP addresses run out, Function Compute cannot create new containers and function invocations fail.

What's next