Help Center > > Tool Guide> Application Examples> HSAM Templates

HSAM Templates

Updated at: Oct 25, 2019 GMT+08:00

Introduction

HSAM is a model that defines serverless applications on HUAWEI CLOUD.

Serverless applications are event-driven and fully hosted. A typical serverless application consists of one or more HUAWEI CLOUD functions. The cloud functions can be used independently or run in response to events of other Huawei cloud resources.

HSAM provides application administrators with a language model that describes service logic of serverless applications. Through AOS, you can orchestrate and deploy resources required for serverless applications.

FunctionGraph's HSS makes it easy to develop and test your own serverless applications locally. Through the remote deployment function of HSS, AOS can automatically deploy serverless applications on HUAWEI CLOUD.

Specifications

Format

HSAM uses YAML templates to describe serverless applications.

The serverless application resources defined by HSAM include:

  • Events
  • Functions
  • FunctionStages

Among these resources, events and functions can be defined independently, while a FunctionStage resource is associated with one function and several events. HSAM supports multiple event sources, including OBS, DMS, SMN, DIS, APIG, and timer.

Each template file contains the "SAMversion: 'v1'" part in the root node. The template files are classified into two types:

  • Resource templates
  • Event source templates

Example HSAM template:

```yaml   
SAMversion: 'v1'
Resources:
   MyFunc1: # Function resource name
    Type: 'HC::Serverless::Function'
    Properties:
      FuncName: testFunc1 
      Description: "This is my Func1" 
      Handler: index.handler 
      Runtime: Python2.7
      MemorySize: 128
      Timeout: 180
      CodeUri: ""
      Code: |
        # -*- coding:utf-8 -*-
        import json
        def handler (event, context):
            output = 'Hello message: ' + json.dumps(event)
            return output
      Xrole: allService
      Environment:
        Variables:
          key1: value1
          key2: value2
   ObsEvt1: # OBS event resource name
    Type: OBS
    Properties:
      Bucket: my-photo-bucket
      EventType:
        - ObjectCreated:*
        - ObjectRemoved:*
      Prefix: fss
      Suffix: jpg
   SmnEvt: # SMN event resource name
    Type: SMN
    Properties:
      Topic: topic1
   DmsEvt: # DMS event resource name
    Type: DMS
    Properties:
      ConsumerGroupId: g-92777f57-a42a-401e-829d-4151417c639f
      QueueId: a99e1c82-808a-4d3b-b03a-0e161e514c2c
   DisEvt: # DIS event resource name
    Type: DIS
    Properties:
      Stream: dis-test
      BatchSize: 100
      ShardIteratorType: TRIM_HORIZON
      PollingInterval: 30
   ApiEvt: # APIG event resource name
    Type: APIG
    Properties:
      ApiName: testApi
      ApiGroup: my_apigroup
      Auth: IAM
      Protocol: HTTPS
      Timeout: 5000
   TimerEvt: # TIMER event resource name
     Type: TIMER
     Properties:
       Name: my_timer
       Schedule: 3m
       ScheduleType: Rate
       UserEvent: "user data"
   MyFuncStage1: # FunctionStage resource name
    Type: 'HC::Serverless::FunctionStage'
    Properties:
      Function: MyFunc1
      Events:
        TimerEvt
        LtsEvt
        CtsEvt
        ApiEvt
```

All attribute names in HSAM templates are case-sensitive.

Resource Templates

  • HC::Serverless::Function
  • HC::Serverless::FunctionStage

HC::Serverless::Function

This template is used to create cloud function resources required for a serverless application.

Table 1 Template attributes

Attribute

Type

Description

FuncName

string

Mandatory

Function name.

Handler

string

Mandatory

Handler of the function. If you edit code in line, set a handler in the "xx.xx" format. If you upload a code file through OBS, set the handler in the format of [code file name].[handler function name]". For example, if a local code file is main.py and the handler function is handler, the handler is named as main.handler.

Runtime

string

Mandatory

Runtime environment. Options: Node.js 6.10, Node.js 8.10, Python 2.7, Python 3.6, Java 8, Go 1.8, C# 2.0, and C# 2.1.

CodeUri

string

This parameter is required if you upload a code file through OBS.

Address of the code file in OBS.

Code

string

This parameter is required if you edit code inline.

Source code of your function.

Description

string

Optional

Description of the function.

Xrole

string

Optional

Configure an agency for the function. For details, see Creating an Agency.

MemorySize

integer

Optional

The options include 128, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2560, 3072, 3584, and 4096. The default value is 128 MB.

Timeout

integer

Optional

Maximum time that the function can run before being terminated. The range is 3s to 900s. The default value is 3s.

Environment

object

Optional

Configure environment variables for the function. For details, see Environment Variables.

Example: HC::Serverless::Function

```yaml
MyFunc1: # Function resource name
    Type: 'HC::Serverless::Function'
    Properties:
      FuncName: testFunc1
      Description: "This is my Func1"
      Handler: index.handler
      Runtime: Python2.7
      MemorySize: 128
      Timeout: 180
      Code: |
        # -*- coding:utf-8 -*-
        import json
        def handler (event, context):
            output = 'Hello message: ' + json.dumps(event)
            return output
      Xrole: allService
      Environment:
        Variables:
          key1: value1
          key2: value2
```

HC::Serverless::FunctionStage

This template is used to create a FunctionStage resource, which contains a function and several triggers. Triggers are not required. This resource template describes the orchestration relationship between a function and triggers.

Table 2 Template attributes

Attribute

Type

Description

Function

string

Mandatory

Function resource name, which is associated with a function.

Events

Events

Optional

Event resource list, which is associated with the triggers under the function. If this attribute is not specified, no triggers have been configured for the function.

Example: HC::Serverless::FunctionStage

```yaml
MyFunc1: # Function resource name
    Type: 'HC::Serverless::Function'
    Properties:
      FuncName: testFunc1
      Description: "This is my Func1"
      Handler: index.handler
      Runtime: Python2.7
      MemorySize: 128
      Timeout: 180
      Code: |
        # -*- coding:utf-8 -*-
        import json
        def handler (event, context):
            output = 'Hello message: ' + json.dumps(event)
            return output
      Xrole: allService
      Environment:
        Variables:
          key1: value1
          key2: value2
SmnEvt: # SMN event resource name
    Type: SMN
    Properties:
      Topic: topic1
MyFuncStage1: # FunctionStage resource name
    Type: 'HC::Serverless::FunctionStage'
    Properties:
      Function: MyFunc1
      Events:
        SmnEvt
```

Event Source Templates

  • TIMER

    This template type describes timer trigger objects. For details, see Using a Timer Trigger.

    Table 3 Template attributes

    Attribute

    Type

    Description

    Name

    string

    Mandatory

    Timer trigger name, which consists of a maximum of 64 characters, starting with a letter. Only letters, digits, and underscores (_) are allowed.

    Schedule

    string

    Mandatory

    Triggering rule. For details, see Using a Timer Trigger.

    ScheduleType

    string

    Mandatory

    Type of the triggering rule. Options: Rate and Cron.

    Status

    Status

    Optional

    Configure whether to enable the timer trigger. The options include DISABLED and ACTIVE. By default, the timer trigger is enabled.

    UserEvent

    string

    Optional

    Configure additional information for the trigger. The additional information must be smaller than 2 KB and will be included in the event when the timer triggers the function.

    Example timer event source object:

    ```yaml
    TimerEvt:
      Type: TIMER
      Properties:
        Name: my_timer
        Schedule: 3m
        ScheduleType: Rate
        UserEvent: "user data"
    ```
  • APIG

    This template type describes API Gateway trigger objects. For details, see Using an APIG Trigger.

    Table 4 Template attributes

    Attribute

    Type

    Description

    ApiName

    string

    Mandatory

    API name.

    APIGroup

    string

    Mandatory

    API group name.

    Auth

    string

    Optional

    Security authentication type. The options include IAM, App, and None. IAM authentication is used by default if this attribute is not set.

    Protocol

    string

    Optional

    Request protocol, which can be HTTP or HTTPS. HTTPS is used by default if this attribute is not set.

    Timeout

    integer

    Optional

    Backend timeout (ms). The range is 1–60000 ms. The timeout is 5000 ms by default if this attribute is not set.

    Example API Gateway event source object:

    ```yaml
    ApiEvt:
      Type: APIG # trigger type
      Properties:
        ApiName: testApi
        ApiGroup: my_apigroup
        Auth: IAM
        Protocol: HTTPS
        Timeout: 5000
    ```
  • OBS

    This template type describes OBS trigger objects. For details, see Using an OBS Trigger.

    Table 5 Template attributes

    Attribute

    Type

    Description

    Bucket

    string

    Mandatory

    Bucket name.

    EventType

    string

    Mandatory

    Event list. Options:

    • ObjectCreated:
    • ObjectCreated:Put
    • ObjectCreated:Post
    • ObjectCreated:Copy
    • ObjectCreated:CompleteMultipartUpload
    • ObjectRemoved:
    • ObjectRemoved:Delete
    • ObjectRemoved:DeleteMarkerCreated

    Prefix

    string

    Mandatory

    Prefix for limiting notifications to those about objects whose names start with the matching characters.

    Suffix

    string

    Mandatory

    Prefix for limiting notifications to those about objects whose names end with the matching characters.

    Example OBS event source object:

    ```yaml
    ObsEvt:
      Type: OBS
      Properties:
        Bucket: my-photo-bucket
        EventType:
          - ObjectCreated:*
          - ObjectRemoved:*
        Prefix: fss
        Suffix: jpg
    ```
  • SMN

    This template type describes SMN trigger objects. For details, see Using an SMN Trigger.

    Table 6 Template attributes

    Attribute

    Type

    Description

    Topic

    string

    Mandatory

    SMN topic. Enter the name of a topic that you have created.

    Remark

    string

    Optional

    Remark about the topic. The default value APItest is used if this attribute is not set.

    Example SMN event source object:

    ```yaml
    SmnEvt:
      Type: SMN
      Properties:
        Topic: topic1
    ```
  • DMS

    This template type describes DMS trigger objects. For details, see Using a DMS Trigger.

    Table 7 Template attributes

    Attribute

    Type

    Description

    ConsumerGroupId

    ConsumerGroupId

    Mandatory

    ConsumerGroupId

    QueueId

    string

    Mandatory

    Queue ID. Enter the ID of a queue that you have created.

    PollingInterval

    integer

    Optional

    Interval at which the DMS trigger polls for messages. The default value 30 is used if this attribute is not set.

    Example DMS event source object:

    ```yaml
    DmsEvt:
      Type: DMS
      Properties:
        ConsumerGroupId: g-92777f57-a42a-401e-829d-4151417c639f
        QueueId: a99e1c82-808a-4d3b-b03a-0e161e514c2c
        PollingInterval: 30
    ```
  • DIS

    This template type describes DIS trigger objects. For details, see Using a DIS Trigger.

    Table 8 Template attributes

    Attribute

    Type

    Description

    Stream

    string

    Mandatory

    Stream name. Enter the name of a stream that you have created.

    BatchSize

    integer

    Optional

    Maximum number of records that your function will read from the stream, which ranges from 1 to 10000. The default value 100 is used if this attribute is not set.

    ShardIteratorType

    string

    Optional

    Select a position in the stream to start reading data. The options include TRIM_HORIZON and LATEST. The default value TRIM_HORIZON is used if this attribute is not set.

    PollingInterval

    integer

    Optional

    Interval at which data is pulled from the stream, which ranges from 1s to 60s. The default value 30 is used if this attribute is not set.

    Example DIS event source object:

    ```yaml
    DisEvt:
      Type: DIS
      Properties:
        Stream: dis-test
        BatchSize: 100
        ShardIteratorType: TRIM_HORIZON
        PollingInterval: 30
    ```

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel