Configuring VPC Access

Functions can be configured to access resources in your Virtual Private Clouds (VPCs) or to access the Internet using elastic IP addresses bound through source network address translation (SNAT).

Configuring VPC Access

To configure VPC access for a function, perform the following steps:

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Create a function based on Creating and Initializing a Function.
  3. Click the name of the created function.
  4. On the displayed function details page, click the Configuration tab, enable VPC Access, and select a VPC and subnet.

    1. For details on how to create a VPC and subnet, see Creating a VPC.
    2. Specify an agency with VPC administrator permissions for the function. For details, see Creating an Agency.
    3. Each account can bind functions in a project to up to four different subnets in whatever VPCs.

  5. Click Save.

Example

The following is an example of configuring VPC access to connect to a Distributed Cache Service (DCS) Redis instance.

  1. On the DCS console, view the VPC and subnet of the DCS Redis instance. If no Redis instances are available, create one.
  2. On the FunctionGraph console, configure VPC access for the function as follows:

    • VPC Access: Enable VPC access.
    • VPC: Select the VPC queried in 1.
    • Subnet: Select the subnet queried in 1.

  3. After the configuration is complete, use the following code to access the Redis instance:

    # -*- coding:utf-8 -*-
    import redis
    
    def handler (event, context):
        r = redis.StrictRedis(host='192.168.1.143', port=6379, db=0)
        r.set('product', 'FunctionGraph')
        print r.keys('*')
        print r.get('product')

Accessing the Internet from a VPC

By default, functions deployed in a VPC are isolated from the Internet. If a function needs to access both internal and external networks, add a public NAT gateway for the VPC.

Prerequisites

  1. You have created a VPC and subnet according to Creating a VPC.
  2. You have obtained an elastic IP address according to Assigning an EIP.

Procedure of Creating a Public NAT Gateway

  1. Log in to the NAT Gateway console, and click Buy Public NAT Gateway.
  2. On the displayed page, enter gateway information, select a VPC and subnet (for example, vpc-01), and confirm and submit the settings to buy a NAT gateway. For details, see Buying a Public NAT Gateway.
  3. On the NAT gateway details page, click Add SNAT Rule, specify rule information, and click OK.