函数核算(FC)是作业驱动的全保管核算服务。运用函数核算,您无需收购与处理服务器等基础设施,只需编写并上传代码。在函数核算的基础上,为完毕主动创立与作业函数核算,以及输出函数核算的作业作用。为简略实施上述的流水先操作,此刻能够正常的运用OOS服务来完毕一系列的主动化动作。
前提条件
- 编写事务需求的脚本代码
模版阐明:
本模版首要功用为:
Description:
en: Run function (FC) and output the result.
zh-cn: 作业函数核算(FC)并输出作用。
FormatVersion: OOS-2019-06-01
Parameters:
Handler:
Type: String
Description:
en: 'Function execution entry, specific format and language related.'
zh-cn: function实施的进口,详细格局和言语相关
Runtime:
Type: String
Description:
en: Function runtime environment.
zh-cn: function的作业环境。
AllowedValues:
- nodejs6
- nodejs8
- python2.7
- python3
- java8
Timeout:
Type: Number
Description:
en: Function timeout.
zh-cn: function作业的超时时刻。
MinValue: 1
MaxValue: 300
Default: 3
SourceCode:
Type: String
Description:
en: Function code.
zh-cn: function的作业代码脚本。
ParameterValue:
Type: String
Description:
en: The value of the script code.
zh-cn: 脚本代码的传参。
OOSAssumeRole:
Description: The RAM role to be assumed by OOS.
Type: String
Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: createStack
Action: 'ACS::ExecuteAPI'
Description:
en: Create a resource stack.
zh-cn: 创立资源栈。
Properties:
Service: ROS
API: CreateStack
Parameters:
StackName: 'OOS-{{ACS::ExecutionId}}'
TimeoutInMinutes: 10
DisableRollback: false
Parameters:
- ParameterKey: ParameterKeys
ParameterValue: '{{ ParameterValue }}'
- ParameterKey: FunctionName
ParameterValue: 'OOS-{{ACS::ExecutionId}}'
- ParameterKey: ServiceName
ParameterValue: 'OOS-{{ACS::ExecutionId}}'
TemplateBody: |-
{
"ROSTemplateFormatVersion": "2015-09-01",
"Parameters": {
"ParameterKeys": {
"Type": "CommaDelimitedList"
},
"ServiceName": {
"Type": "String",
"Default": "test-fc-service"
},
"FunctionName": {
"Type": "String",
"Default": "test-fc-function"
},
"ExecuteVersion": {
"Type": "Number",
"Default": 0
}
},
"Resources": {
"Service": {
"Type": "ALIYUN::FC::Service",
"Properties": {
"ServiceName": {
"Ref": "ServiceName"
}
}
},
"Function": {
"Type": "ALIYUN::FC::Function",
"DependsOn": "Service",
"Properties": {
"ServiceName": {
"Ref": "ServiceName"
},
"FunctionName": {
"Ref": "FunctionName"
},
"Code": {
"SourceCode": "{{ SourceCode }}"
},
"Handler": "{{ Handler }}",
"Runtime": "{{ Runtime }}",
"Timeout": "{{ Timeout }}"
}
},
"FunctionInvoker": {
"Type": "ALIYUN::FC::FunctionInvoker",
"DependsOn": "Function",
"Properties": {
"ServiceName": {
"Ref": "ServiceName"
},
"FunctionName": {
"Ref": "FunctionName"
},
"Async": false,
"Event": {
"Fn::Join": [",", {"Ref": "ParameterKeys"}]
},
"ExecuteVersion": {
"Ref": "ExecuteVersion"
}
}
}
},
"Outputs": {
"Result": {
"Value": {
"Fn::Replace" : [
{
"FcResult": {
"Fn::GetAtt": [
"FunctionInvoker",
"Result"
]
}
},
"FcResult"
]
}
}
}
}
Outputs:
StackId:
Type: String
ValueSelector: StackId
- Name: untilStackReady
Action: 'ACS::WaitFor'
OnError: deleteStack
Description:
en: Wait the stack status util CREATE_COMPLETE.
zh-cn: 等候资源栈至创立成功状况。
Properties:
Service: ROS
API: GetStack
Parameters:
StackId: '{{createStack.StackId}}'
DesiredValues:
- CREATE_COMPLETE
StopRetryValues:
- CREATE_FAILED
- CHECK_FAILED
- ROLLBACK_FAILED
- ROLLBACK_COMPLETE
PropertySelector: Status
Outputs:
Result:
Type: String
ValueSelector: 'Outputs[].OutputValue'
- Name: deleteStack
Action: 'ACS::ExecuteApi'
Description:
en: Delete the stack.
zh-cn: 删去资源栈。
Properties:
Service: ROS
API: DeleteStack
Parameters:
StackId: '{{createStack.StackId}}'
Outputs:
Result:
Type: String
Value: '{{ untilStackReady.Result }}'
模版参数阐明:
参数 | 阐明 | 示例 |
---|---|---|
Handler | function实施的进口,详细格局和言语相关(注:前部分为脚本文件名,后部分为脚本内函数名) | index.lottery |
Runtime | function的作业环境,现在支撑: - nodejs6 - nodejs8 - python2.7 - python3 - java8 |
python3 |
Timeout | function作业的超时时刻,单位为秒,最小1秒,最长5分钟,默许3秒。 | 3 |
SourceCode | 脚本代码。(注:需求将换行转化为n) | import randomndef lottery(event, context):n return '祝贺%s中奖' % random.choice([name.strip() for name in event.decode('utf-8').split(',')]) |
ParameterValue | 用于脚本的参数值(注:在脚本中运用参数时,传入到脚本的参数为Bytes类型,需求在脚本代码中转译成需求的编码类型。) | a,b,c,d |
OOSAssumeRole | 可选参数。 - (默许设置)当时账号的已有权限:实施您运用的账号的权限动作。请确保您具有创立自定义镜像触及的悉数ECS API调用权限。 - 指定RAM人物,运用该人物的权限:假定指定了RAM人物称谓,OOS扮演该RAM人物实施运维使命。 |
当时账号的已有权限 |
操作过程
- 请拜访运维编列OOS的控制台点击当即注册按钮注册服务。这是一个免费服务,请定心注册。
- 单击自定义模板,单击创立模板按钮,选取空白模板。
- 将上述供给的子模板粘贴到yaml栏,输入称谓RunFcForLottery或自定义称谓,点击创立模板。
- 单击创立实施。
- 坚持基本信息的默许设置,单击下一步:设置参数。
- 完毕参数设置填写,单击下一步:招认。
- 检查参数设置是否无误,招认无误后单击创立实施。
- 在实施处理中检查创立的运维使命。
- 实施作用如下所示,作用输出栏闪现的是依据脚本中作业的作用。
附录:
此模版是运用的资源栈的方法作业的函数核算。运用函数核算能够将模版发生的数据在脚本内处理成事务需求的的格局或运用于特别的需求。当以上述的OOS模版的方法来作业函数核算只需求依据实在的状况补偿参数,及编写的脚本代码即可完毕主动创立函数核算,并输出函数核算的作用。
系列文章
主题文章
阿里云重磅发布云上主动化利器——运维编列OOS
最佳实践
玩转运维编列服务的权限:Assume Role+Pass Role
阿里云运维编列新功用:一键批量克隆ECS
批量在多台ECS内实施指令
场景系列
运维编列场景系列----更新ECS镜像
运维编列场景系列----给ECS实例主动打TAG
运维编列场景系列----从实例中拷贝文件到OSS
运维编列场景系列----给实例加到SLS机器组
运维编列场景系列----检测MFA功用状况
运维编列场景系列----每日核算多Region实例的作业状况
运维编列场景系列----怎样运用jq
运维编列场景系列----分批到机器上作业指令
运维编列场景系列----更新镜像后主动更新弹性装备镜像
运维编列场景系列----向Linux实例上传文件
运维编列场景系列----作业远端shell脚本
运维编列场景系列----在ECS实例上作业Ansible-playbook
运维编列场景系列----下载JVM仓库到OSS
运维编列系列场景----将实例的固定公网IP转换为其它新EIP
运维编列场景系列----主动守时晋级暂时带宽
运维编列场景系列----批量更新云辅佐客户端
运维编列系列场景----批量开释实例
运维编列场景系列----ECS实例体系快照下载到本地
运维编列场景系列----批量替换体系盘
运维编列场景系列----主动创立多个区域同类型存储空间
发表评论