Files
doc-exports/docs/fg/umn/functiongraph_03_0300.html
chenjunjie da8877ec76 FG UMN 20241021 version
Reviewed-by: Mützel, Andrea <andrea.muetzel@t-systems.com>
Co-authored-by: chenjunjie <chenjunjie@huawei.com>
Co-committed-by: chenjunjie <chenjunjie@huawei.com>
2025-05-20 13:39:19 +00:00

2.8 KiB

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.