官术网_书友最值得收藏!

Functions and events

Functions are the properties that are defined within the service, and they are defined within the serverless.yml, so we name the function and provide the handler property to the function, and this property points to the function file, which could be Node.js or Python. We can add multiple functions within the property. The functions can inherit the properties from the provider or we can define the properties at function level. These function properties vary as per the cloud provider, as shown in the following code:

# serverless.yml
service: myService
provider:
name: aws
runtime: nodejs6.10
memorySize: 512 # will be inherited by all functions
functions:
usersAdd:
handler: handler.userAdd
description: optional description for your function
userModify:
handler: handler.userModify
userDelete:
handler: handler.userDelete
memorySize: 256 # function specific

The functions can be listed as arrays if we create a separate file for each function: 

 # serverless.yml

functions:
- ${file(../user-functions.yml)}
- ${file(../post-functions.yml)}
# user-functions.yml
addUser:
handler: handler.user
deleteUser:
handler: handler.user

An environment object property can also be added to the function within the service, and it should be a key–pair value. Also, the function-specific environment variable overrides the provider-specific environment variable: 

 # serverless.yml 
service: service-name
provider: aws

functions:
hello:
handler: handler.hello
environment:
TABLE_NAME: tableName

Events are things that trigger the function, such as the S3 bucket upload. There are multiple events supported by Serverless Framework, but they vary as per the cloud provider. We can define multiple events for a single function, as shown in the following code: 

events:
- http:
path: handler
method: get
The types of events provided for AWS Lambda by Serverless Framework are shown at the following link:
https://serverless.com/framework/docs/providers/aws/events/
主站蜘蛛池模板: 行唐县| 横峰县| 连江县| 平江县| 历史| 理塘县| 周至县| 乌什县| 梓潼县| 甘谷县| 高淳县| 柳河县| 庄河市| 昌都县| 政和县| 湛江市| 敖汉旗| 肃北| 思南县| 英德市| 保定市| 云浮市| 安丘市| 姚安县| 太原市| 定襄县| 泸水县| 延长县| 忻州市| 江油市| 云阳县| 玉林市| 望城县| 上高县| 卢湾区| 岳普湖县| 武义县| 阿巴嘎旗| 绩溪县| 江源县| 枣庄市|