ACS-FC-RunScript

更新时间:
复制 MD 格式

Template name

ACS-FC-RunScript

Execute Now

Template description

Runs a Python script in Function Compute (FC). You must activate Function Compute before you use this template.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Limit

Runtime

Runtime environment for the script.

String

No

python3.10

Script

Python script code to run.

String

No

import datetime
import os
import sys

def _now(format="%Y-%m-%d %H:%M:%S"):
  return datetime.datetime.now().strftime(format)

def job_start():
  return ("[%s][%s] job_start" % (_now(), os.getpid()))

def handler(event, context):
  return job_start() 

FCAssumeRole

RAM role assumed by Function Compute.

String

No

""

OOSAssumeRole

RAM role assumed by CloudOps Orchestration Service (OOS).

String

No

""

Output parameters

Parameter

Description

Type

ScriptOutput

String

Required permission policy

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ros:CreateStack",
                "ros:DeleteStack",
                "ros:GetStack"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

Details

For more information, see ACS-FC-RunScript.yml at GitHub.

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: FC runs script(Only support python), To use this template, you must first <a href='https://help.aliyun.com/zh/functioncompute/getting-started/quickly-create-a-function#p-t79-y7o-68z' target="_blank">activate the function computing service< /a>
  name-en: ACS-FC-RunScript
Parameters:
  Runtime:
    Type: String
    Label:
      en: Runtime
    AllowedValues:
      - python3.9
      - python3.10
    Default: python3.10
  Script:
    Type: String
    Label:
      en: Python Script
    AssociationProperty: ALIYUN::OOS::Command::CommandContent
    AssociationPropertyMetadata:
      CommandType: RunPythonScript
    Default: |-
      import datetime
      import os
      import sys

      def _now(format="%Y-%m-%d %H:%M:%S"):
        return datetime.datetime.now().strftime(format)

      def job_start():
        return ("[%s][%s] job_start" % (_now(), os.getpid()))

      def handler(event, context):
        return job_start()
  FCAssumeRole:
    Label:
      en: FCAssumeRole
    Description:
      en: The Function Compute platform will use this RAM role to generate a temporary key for accessing your Alibaba Cloud resources and pass it to your code. For details, please see <a href="https://help.aliyun.com/zh/functioncompute/user-guide/grant-function-compute-permissions-to-access-other-alibaba-cloud-services" target="_blank ">Grant Function Compute permissions to access other cloud services</a>
    Type: String
    AssociationProperty: ALIYUN::RAM::Service::Role
    AssociationPropertyMetadata:
      Service: fc.aliyuncs.com
    Default: ''
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: ExecuteScript
    Action: ACS::FC::ExecuteScript
    Description:
      en: Run the python script
    Properties:
      runtime: '{{ Runtime }}'
      script: '{{ Script }}'
      role: '{{ FCAssumeRole }}'
    Outputs:
      Result:
        Type: String
        ValueSelector: Result
Outputs:
  ScriptOutput:
    Type: String
    Value: '{{ ExecuteScript.Result }}'