访问Redis数据库是指在函数计算中通过函数调用数据库应用编程接口,对数据库执行数据插入、查询等操作。通常函数计算中不同的执行环境实例之间的状态是不共享的,通过数据库的形式可以将结构化的数据持久化以实现状态共享。本文介绍如何通过Funcraft部署函数来访问Redis数据库。
前提条件
- 创建专有网络和交换机
创建的交换机需要在函数计算支持的可用区内,函数计算支持的可用区信息,请参见配置函数访问VPC内资源。
- 创建Redis实例
创建数据库实例需要选择与函数计算相同的VPC实例。由于相同的VPC实例下不同可用区交换机内网是互通的,因此您可以配置和函数计算不同可用区的交换机。
- 设置Redis白名单
如果您通过设置白名单分组的方法来设置数据库白名单,则需要在设置过程中的组内白名单文本框中输入相应的VPC的IP网段。您可以登录VPC控制台,进入目标VPC的专有网络详情页查看VPC的IP网段。
- 创建安全组
安全组出口方向必须放行数据库的端口和内网IP段。
编写函数
本文以Python 3开发语言的函数示例为例,介绍如何通过Funcraft工具编写函数访问数据库。
- 在本地建立一个目录,用于存放代码和依赖模块,在该目录下新建template.yml文件,例如/tmp/code/template.yml,示例内容如下。
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
service:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: This is Redis service
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: vpc-xxxx
VSwitchIds:
- vsw-xxxx
SecurityGroupId: sg-xxxx
InternetAccess: true
function:
Type: 'Aliyun::Serverless::Function'
Properties:
Initializer: 'index.initializer'
Handler: 'index.handler'
Runtime: python3
CodeUri: './'
EnvironmentVariables:
REDIS_HOST: r-xxxx.redis.rds.aliyuncs.com
REDIS_PASSWORD: Txd1xxxx
REDIS_PORT: '6379'
代码中的关键参数含义如下:
- 声明了一个名为service的服务。
- Policies:授予函数计算管理ECS弹性网卡的权限,实现函数服务访问已创建的VPC内的资源。
- VpcConfig:为服务绑定已创建的VPC。您需要将以下参数值替换成自己的VPC相关信息:
- VpcId:专有网络ID。
- VSwitchIds:交换机ID。
- SecurityGroupId:安全组ID。
- 声明了一个名为function的函数。
更多配置规则,请参见Serverless Application Model。
- 在template.yml文件的同级目录下创建Funfile文件,示例内容如下。
RUNTIME python3
RUN fun-install pip install redis
- 执行fun install命令安装依赖。
fun install
using template: template.yml
start installing function dependencies without docker
building service/function
Funfile exist, Fun will use container to build forcely
Step 1/2 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.7.7
---> 373f5819463b
Step 2/2 : RUN fun-install pip install redis
---> Running in f26aef48f9e5
Task => PipTask
=> PYTHONUSERBASE=/code/.fun/python pip install --user redis
Removing intermediate container f26aef48f9e5
---> 809c6655f9e9
sha256:809c6655f9e93d137840b1446f46572fbab7548c5c36b6ae66599dfc2e27555b
Successfully built 809c6655f9e9
Successfully tagged fun-cache-78c74899-5497-4205-a670-24e4daf88284:latest
copying function artifact to /Users/txd123/Desktop/Redis/Python
Install Success
- 在template.yml文件的同级目录下新建代码文件,例如/tmp/code/index.py,示例内容如下。
# -*- coding: utf-8 -*-
import os,sys
import redis
def initializer(context):
global conn_pool
conn_pool=redis.ConnectionPool(host=os.environ['REDIS_HOST'],password=os.environ['REDIS_PASSWORD'],port=os.environ['REDIS_PORT'],db=1,decode_responses=True)
def handler(event, context):
r = redis.Redis(connection_pool=conn_pool)
r.set('test','89898')
r.set('zyh_info','{"name":"Tanya","password":"123456","account":11234}')
print(r.get('test'))
return r.get('zyh_info')
- 执行以下命令,使用Funcraft工具部署。
fun deploy
using template: template.yml
using region: cn-hangzhou
using accountId: ***********3743
using accessKeyId: ***********Ptgk
using timeout: 60
Waiting for service service to be deployed...
Waiting for function function to be deployed...
Waiting for packaging function function code...
The function function has been packaged. A total of 25 files files were compressed and the final size was 138.78 KB
function function deploy success
service service deploy success
调试函数
- 登录函数计算控制台。
- 在顶部菜单栏,选择地域。
- 在左侧导航栏,单击服务及函数。
- 在服务列表区域,单击目标服务。然后在函数列表页签,单击目标函数名称。
- 单击代码执行页签,然后单击执行。
执行完成后,您可以查看执行结果和执行日志。