j9九游会登录/ 函数工作流 functiongraph/ 开发指南/ / functiongraph与基础设施即代码(iac)
更新时间:2025-04-21 gmt 08:00

functiongraph与基础设施即代码(iac)-j9九游会登录

将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   # 处填写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": {}}

相关文档

网站地图