(FC)是事件驅動的全托管計算服務。使用函數計算,您無需采購與管理伺服器等基礎設施,隻需編寫并上傳代碼。在函數計算的基礎上,為實作自動建立與運作函數計算,以及輸出函數計算的運作結果。為簡單執行上述的流水先操作,此時可以使用OOS服務來實作一系列的自動化動作。
前提條件
- 編寫業務需求的腳本代碼
模版說明:
本模版主要功能為:
- 建立資源棧
- 建立FC服務
- 建立FC函數
- 運作FC函數
- 等待資源棧至建立成功狀态
- 删除資源棧
- 輸出FC函數運作的結果
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模版的方式來運作函數計算隻需要根據實際情況補充參數,及編寫的腳本代碼即可實作自動建立函數計算,并輸出函數計算的結果。