文档首页/ 函数工作流 FunctionGraph/ 开发指南/ 开发工具/ FunctionGraph与基础设施即代码(IaC)
更新时间:2025-03-13 GMT+08:00

FunctionGraph与基础设施即代码(IaC)

将FunctionGraph与基础设施即代码(IaC)结合使用

FunctionGraph函数不常单独运行。它与存储、网关、数据库和消息队列等其他资源结合,组成无服务应用程序。借助基础设施即代码(IaC),可以自动化部署流程,从而快速且重复地部署与更新函数及触发器配置。该方法加快了开发周期,简化了配置管理,确保每次都一致地部署资源。

适用于FunctionGraph的IaC工具

资源编排服务(Resource Formation Service,简称RFS)是完全支持业界事实标准Terraform(HCL + Provider)的新一代云服务资源终态编排引擎。基于业界开放生态HCL语法模板,实现云服务资源的自动化批量构建,帮助用户高效、安全、一致创建、管理和升级云服务资源(例如FunctionGraph,APIG实例,数据库实例等),能有效提升资源管理效率,并降低资源管理变更带来的安全风险。

RFS for FunctionGraph入门

  1. 编写Python脚本代码index.py文件,内容如下:

    # -*- coding:utf-8 -*-
    import json
    def handler (event, context):
        return {
            "statusCode": 200,
            "isBase64Encoded": False,
            "body": json.dumps(event),
            "headers": {
                "Content-Type": "application/json"
            }
        }

  2. 将index.py代码文件打包为index.zip文件,把zip文件上传至OBS桶,获取OBS桶中的代码文件对象链接
  3. 编写参数模板文件variables.tf,用于定义RFS模板用到的参数,内容如下:

    variable "enterprise_project_id" {
      type        = string
      description = " Specifies enterprise_project_id"
      default     = "0"
    }
    variable "agency_name" {
      type        = string
      description = " Specifies the agency to which the function belongs."
      default     = ""
    }
    variable "region" {
      type        = string
      description = " Specifies the region."
      default     = "cn-north-7"
    }
    variable "code_url" {
      type        = string
      description = "code_url"
    }
    variable "apig_id" {
      type        = string
      description = "apig_id"
      default     = ""
    }

  4. 编写RFS模板文件main.tf,用于定义函数及触发器,内容如下:

    variable "enterprise_project_id" {
      type        = string
      description = " Specifies enterprise_project_id"
      default     = "0"
    }
    variable "agency_name" {
      type        = string
      description = " Specifies the agency to which the function belongs."
      default     = ""
    }
    variable "region" {
      type        = string
      description = " Specifies the region."
      default     = "cn-north-7"
    }
    variable "code_url" {
      type        = string
      description = "code_url"
    }
    variable "apig_id" {
      type        = string
      description = "apig_id"
      default     = ""
    }

  5. 将上述main.tf和variables.tf文件打包为一个components.zip文件。
  6. 登录资源编排服务RFS控制台,如图1所示选择“资源栈 > 立即创建资源栈”。可参考创建资源栈同步操作。

    图1 RFS控制台

  7. 图2所示填写参数,并上传components.zip文件。完成后单击“下一步”

    图2 配置参数

  8. 进入“参数配置”页面,填写配置参数。完成后单击“下一步”

    • enterprise_project_id:企业项目ID。
    • agency_name:函数委托名称。
    • region:局点ID,请参考地区和终端节点填写。
    • code_url:步骤2中获取的代码OBS链接。
    • apig_id:APIG实例ID。

  9. 进入“资源栈设置”页面,配置IAM权限委托。其他参数保持默认即可,单击“下一步”确认部署资源栈。将自动在函数工作流控制台创建相关的函数与资源。
  10. 登录函数工作流控制台,在函数列表中单击函数名称进入函数详情页,单击“设置 > 触发器”,找到APIG触发器并复制调用URL,将其粘贴到浏览器中或运行以下curl命令。

    curl -s <Trigger_Invoke_URL>  # <Trigger_Invoke_URL>处填写APIG触发器的调用URL

    响应是原始事件对象中选定属性的 JSON 表示,其中包含向 API Gateway 端点发出请求的相关信息。示例:

    HTTP/1.1 200 OK
    Content-Length: 658
    Connection: keep-alive
    Content-Type: application/json
    Date: Wed, 12 Mar 2025 08:59:18 GMT
    Server: api-gateway
    Strict-Transport-Security: max-age=31536000; includeSubdomains;
    X-Apig-Latency: 52
    X-Apig-Ratelimit-Api: remain:97,limit:100,time:1 minute
    X-Apig-Ratelimit-Api: remain:29973,limit:30000,time:1 second
    X-Apig-Ratelimit-Api-Allenv: remain:199,limit:200,time:1 second
    X-Apig-Ratelimit-Api-Allenv: remain:29973,limit:30000,time:1 second
    X-Apig-Ratelimit-User: remain:3995,limit:4000,time:1 second
    X-Apig-Upstream-Latency: 14
    X-Cff-Billing-Duration: 1
    X-Cff-Invoke-Summary: {"funcDigest":"e6e9c99b8f5b9d6f9408d5210263330","duration":0.756,"billingDuration":1,"memorySize":128,"memoryUsed":33.207,"podName":"pool22-300-128-fusion-844bdc7755-bh55w","gpuMemorySize":0,"ephemeralStorage":512}
    X-Cff-Request-Id: b43781ee-49f3-4762-8c24-236c718d3391
    X-Content-Type-Options: nosniff
    X-Download-Options: noopen
    X-Frame-Options: SAMEORIGIN
    X-Func-Err-Code: 0
    X-Is-Func-Err: false
    X-Request-Id: 90a48d7a4c699780579f4edc8983cdaf
    X-Xss-Protection: 1; mode=block;
     
    {"requestContext": {"requestId": "90a48d7a4c699780579f4edc8983cdaf", "apiId": "01127600bb9f4d2ca8e532d1c378d8c8", "stage": ":DEBUG"}, "queryStringParameters": {}, "path": "/nxy-sasa", "httpMethod": "GET", "isBase64Encoded": true, "headers": {"host": "47f32d1efa1742f5a7ee5b720ca9c4a5.apig.cn-east-3.huaweicloudapis.com", "content-type": "application/json", "x-forwarded-host": "47f32d1efa1742f5a7ee5b720ca9c4a5.apig.cn-east-3.huaweicloudapis.com", "user-agent": "APIGatewayDebugClient/1.0", "x-forwarded-port": "443", "x-forwarded-proto": "https", "x-request-id": "90a48d7a4c699780579f4edc8983cdaf", "x-apig-mode": "debug"}, "body": "", "pathParameters": {}}