关于 huawei-cloud-functiongraph-function-create
此技能通过 Huawei Cloud FunctionGraph Python SDK 创建和调用云函数,面向部署或测试函数的开发者。它支持 VPC、环境变量配置,以及同步或异步调用。
主要功能
- 01创建 FunctionGraph 函数
- 02配置 VPC 和环境变量
- 03同步或异步调用函数
- 04指定运行时、入口和代码
- 05返回函数 URN 与调用结果
适用场景
- 01快速创建云函数
- 02批量部署带 VPC 配置的函数
- 03将函数部署接入 CI/CD
- 04调用并测试已部署函数
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ huaweicloud/ huaweicloud-skills --skill huawei-cloud-functiongraph-function-create安装后在 Claude Code 中输入/huawei-cloud-functiongraph-function-create确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/acceptance-criteria.mdiam-policies.mdsdk-installation-guide.mdverification-method.mdscripts/create_function.py详细介绍
FunctionGraph 函数创建与调用技能
概述
此技能用于管理华为云上的 FunctionGraph 函数,支持通过高级配置(VPC、环境变量)创建函数,以及调用函数(同步/异步)。
架构:使用华为云 FunctionGraph Python SDK 与 FunctionGraph 服务交互。
适用场景:
- 无需手动登录控制台即可快速创建云函数
- 批量创建多个带有 VPC 或环境变量配置的函数
- 工作流部署的基础组件
- 将函数部署集成到 CI/CD 中
- 调用和测试已部署的函数
典型用例:
- “创建一个名为 my_handler 的 Python 函数”
- “使用 VPC 配置将这段代码部署到 FunctionGraph”
- “创建一个带有环境变量的函数”
- “调用我刚创建的函数”
前提条件
1. Python 环境
- Python 3.9+
- 验证:
python --version
2. SDK 安装
pip install huaweicloudsdkfunctiongraph
3. 身份验证配置
- 有效的华为云凭证(AK/SK 模式)
- 通过环境变量进行配置:
export HUAWEI_AK="your-access-key"
export HUAWEI_SK="your-secret-key"
export HUAWEI_REGION="cn-north-4"
export HUAWEI_PROJECT_ID="your-project-id"
安全规则:
- 绝不在代码或日志中公开 AK/SK 值
- 绝不让用户在对话中直接输入 AK/SK
- 始终使用环境变量存储凭证
- 建议使用 IAM 用户,而非主账号
4. IAM 权限要求
functiongraph:function:create- 创建函数functiongraph:function:get- 查询函数详情functiongraph:function:list- 列出函数functiongraph:function:invoke- 调用函数
有关详细的权限配置,请参阅 IAM Policies。
工作流
User Request → Parse Parameters → Validate Input
→ Create Function (with optional VPC/Env Vars) → Return Function URN
→ (Optional) Invoke Function → Return Invocation Result
用法
创建函数
cd scripts
# Basic creation
python create_function.py create \
--name my_function \
--runtime Python3.9 \
--handler index.handler \
--code main.py
# With VPC configuration
python create_function.py create \
--name my_function \
--runtime Python3.9 \
--handler index.handler \
--code main.py \
--vpc-id vpc-xxx \
--subnet-id subnet-xxx \
--security-group-id sg-xxx
# With environment variables
python create_function.py create \
--name my_function \
--runtime Python3.9 \
--handler index.handler \
--code main.py \
--env-vars '{"DB_HOST":"localhost","DB_PORT":"3306"}'
# Full configuration
python create_function.py create \
--name my_function \
--runtime Python3.9 \
--handler index.handler \
--code main.py \
--memory 256 \
--timeout 30 \
--description "My production function" \
--vpc-id vpc-xxx \
--subnet-id subnet-xxx \
--security-group-id sg-xxx \
--env-vars '{"DEBUG":"true","LOG_LEVEL":"info"}'
调用函数
注意:函数 URN 使用 FunctionGraph 实际的 URN 前缀
urn:fss:。 请将下方示例中的PROJECT_ID替换为你的实际项目 ID (与HUAWEI_PROJECT_ID环境变量的值相同)。函数包为default,版本为latest(或特定版本/别名)。你可以复制create命令返回的 URN,并在末尾添加:latest以进行调用。
# Synchronous invocation (default)
python create_function.py invoke \
--function-urn urn:fss:cn-north-4:PROJECT_ID:function:default:my_function:latest \
--body '{"key1":"value1","source":"test"}'
# Asynchronous invocation
python create_function.py invoke \
--function-urn urn:fss:cn-north-4:PROJECT_ID:function:default:my_function:latest \
--body '{"async":true}' \
--invocation-type async
# Invoke with log tail (returns last 4KB of log)
python create_function.py invoke \
--function-urn urn:fss:cn-north-4:PROJECT_ID:function:default:my_function:latest \
--body '{"test":"data"}' \
--log-type tail
参数确认
创建参数
| 参数 | 必填/可选 | 说明 | 默认值 |
|---|---|---|---|
--name | 必填 | 函数名称,必须符合命名规则 | - |
--runtime | 必填 | 运行环境(Python3.9 等) | - |
--handler | 必填 | 函数入口点(例如 index.handler) | - |
--code | 必填 | 代码文件路径或内联代码内容 | - |
--memory | 可选 | 内存大小,单位为 MB | 128 |
--timeout | 可选 | 超时时间,单位为秒 | 3 |
--description | 可选 | 函数描述 | - |
--vpc-id | 可选 | VPC 配置的 VPC ID(必须与 --subnet-id 和 --security-group-id 一起设置) | - |
--subnet-id | 可选 | VPC 配置的子网 ID(必须与 --vpc-id 和 --security-group-id 一起设置) | - |
--security-group-id | 可选 | VPC 配置的安全组 ID(必须与 --vpc-id 和 --subnet-id 一起设置) | - |
--env-vars | 可选 | 以 JSON 字符串形式提供的环境变量 | - |
调用参数
| 参数 | 必填/可选 | 说明 | 默认值 |
|---|---|---|---|
--function-urn | 必填 | 要调用的函数 URN | - |
--body | 可选 | 以 JSON 字符串形式提供的事件负载 | {} |
--invocation-type | 可选 | sync 或 async | sync |
--log-type | 可选 | tail:返回最后 4KB 日志 | - |
运行时选项
| 运行时 | 处理程序格式 | 示例 |
|---|---|---|
| Python3.9 | index.handler | def handler(event, context): |
| Node.js14.18 | index.handler | exports.handler = (event, context) => {} |
| Java8 | com.example.Handler::handleRequest | Java 类方法 |
| Go1.x | handler | Go 函数名 |
验证方法
详细步骤请参阅验证方法。
快速验证
# Verify function exists using SDK
from huaweicloudsdkfunctiongraph.v2.functiongraph_client import FunctionGraphClient
from huaweicloudsdkfunctiongraph.v2.model import ListFunctionsRequest
# List functions to verify creation
client = FunctionGraphClient.new_builder().build()
request = ListFunctionsRequest()
response = client.list_functions(request)
print(f"Total functions: {len(response.functions)}")
输出格式
创建输出
脚本会返回一个结构化的 JSON 对象:
{
"status": "success",
"function_urn": "urn:fss:cn-north-4:project_id:function:default:my_function",
"function_name": "my_function",
"runtime": "Python3.9",
"memory_size": 128,
"timeout": 3,
"handler": "index.handler",
"code_size": 1234,
"message": "Function created successfully"
}
调用输出
{
"status": "success",
"function_urn": "urn:fss:cn-north-4:project_id:function:default:my_function",
"invocation_type": "sync",
"result": {
"output": "..."
},
"request_id": "a1b2c3d4-...",
"message": "Function invoked successfully"
}
最佳实践
- 在生产环境使用前进行测试:始终先在开发环境中测试
- 监控资源:为函数调用和错误设置监控
- 使用环境变量:在环境变量中存储敏感数据
- 实现错误处理:在函数代码中添加适当的错误处理
- 设置适当的超时时间:根据函数逻辑配置超时时间
- 使用 VPC 访问私有资源:当函数需要访问私有网络资源时,使用 VPC 配置
错误处理
| 错误代码 | 描述 | 解决方法 |
|---|---|---|
| InvalidParameter | 输入参数无效 | 检查参数格式和值 |
| InsufficientPermission | 权限不足 | 检查 IAM 权限 |
| QuotaExceeded | 超出资源配额 | 申请提高配额或删除未使用的函数 |
| FunctionAlreadyExists | 存在同名函数 | 使用其他函数名或删除现有函数 |
| FunctionNotFound | 未找到函数 URN(调用时) | 确认函数 URN 正确 |
参考资料
评论
支持 Markdown