mcpField
H
huawei-cloud-functiongraph-function-create

huawei-cloud-functiongraph-function-create

github.com/huaweicloud云与 DevOps5917.1k 次安装2 天前更新
代码仓库

关于 huawei-cloud-functiongraph-function-create

此技能通过 Huawei Cloud FunctionGraph Python SDK 创建和调用云函数,面向部署或测试函数的开发者。它支持 VPC、环境变量配置,以及同步或异步调用。

主要功能

  1. 01创建 FunctionGraph 函数
  2. 02配置 VPC 和环境变量
  3. 03同步或异步调用函数
  4. 04指定运行时、入口和代码
  5. 05返回函数 URN 与调用结果

适用场景

  1. 01快速创建云函数
  2. 02批量部署带 VPC 配置的函数
  3. 03将函数部署接入 CI/CD
  4. 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.md
iam-policies.md
sdk-installation-guide.md
verification-method.md
scripts/
create_function.py

详细介绍

FunctionGraph 函数创建与调用技能

概述

此技能用于管理华为云上的 FunctionGraph 函数,支持通过高级配置(VPC、环境变量)创建函数,以及调用函数(同步/异步)。

架构:使用华为云 FunctionGraph Python SDK 与 FunctionGraph 服务交互。

适用场景:

  • 无需手动登录控制台即可快速创建云函数
  • 批量创建多个带有 VPC 或环境变量配置的函数
  • 工作流部署的基础组件
  • 将函数部署集成到 CI/CD 中
  • 调用和测试已部署的函数

典型用例:

  1. “创建一个名为 my_handler 的 Python 函数”
  2. “使用 VPC 配置将这段代码部署到 FunctionGraph”
  3. “创建一个带有环境变量的函数”
  4. “调用我刚创建的函数”

前提条件

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可选内存大小,单位为 MB128
--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 或 asyncsync
--log-type可选tail:返回最后 4KB 日志-
运行时选项
运行时处理程序格式示例
Python3.9index.handlerdef handler(event, context):
Node.js14.18index.handlerexports.handler = (event, context) => {}
Java8com.example.Handler::handleRequestJava 类方法
Go1.xhandlerGo 函数名

验证方法

详细步骤请参阅验证方法。

快速验证
# 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"
}

最佳实践

  1. 在生产环境使用前进行测试:始终先在开发环境中测试
  2. 监控资源:为函数调用和错误设置监控
  3. 使用环境变量:在环境变量中存储敏感数据
  4. 实现错误处理:在函数代码中添加适当的错误处理
  5. 设置适当的超时时间:根据函数逻辑配置超时时间
  6. 使用 VPC 访问私有资源:当函数需要访问私有网络资源时,使用 VPC 配置

错误处理

错误代码描述解决方法
InvalidParameter输入参数无效检查参数格式和值
InsufficientPermission权限不足检查 IAM 权限
QuotaExceeded超出资源配额申请提高配额或删除未使用的函数
FunctionAlreadyExists存在同名函数使用其他函数名或删除现有函数
FunctionNotFound未找到函数 URN(调用时)确认函数 URN 正确

参考资料

评论

支持 Markdown