Updated on 2023-03-13 GMT+08:00

How Does a Function Access Redis?

Perform the following operations:

  1. Check whether the Redis instance is deployed in a VPC.
    • If the Redis instance is deployed in a VPC, configure the same VPC and subnet as the Redis instance for the function by referring to Configuring VPC Access.
    • If the Redis instance is built on a public network, obtain its public IP address.
  2. Compile code for connecting a function to the Redis instance.

    FunctionGraph has integrated third-party library redis-py in its Python 2.7 and Python 3.6 runtimes. Therefore, you do not need to download any other Redis libraries.

    # -*- coding:utf-8 -*-
    import redis
    def handler (event, context):
        r = redis.StrictRedis(host="host_ip",password="passwd",port=6379)
        print(str(r.get("hostname")))   
        return "^_^"
    
    • If the function fails to access to the Redis instance on a public network, perform the following operations:
      • Modify the redis.conf file to allow access from any IP addresses.
      • Set a password for accessing the Redis instance in the redis.conf file.
      • Disable the firewall.
    • If the function needs to access DCS APIs, create an agency and grant required permissions.