All Products
Search
Document Center

Function parameters

Last Updated: Oct 16, 2018

This document introduces two parameters of Function Compute, namely event and context.

event

The parameter event is the input parameter.

When the trigger calls the function, the function uses the event parameter. The data structure is user-defined, and you can define the event structure in the string, JSON, or image (binary data) format. Function Compute does not explain the content of the event parameter. The event parameter is a byte stream. In Python2.7, the event parameter is string type, while in Python3, it is bytes type. For the event type of each runtime, see the runtime documentation for each language in Programming Guide.

You can convert the event type in the function according to the actual situation. If the input data is a JSON string, you can convert this string into a dictionary. For example, the event passed is:

  1. {
  2. "key": "value"
  3. }

The function code is:

Python version

  1. import json
  2. def handler(event, context):
  3. evt = json.loads(event)
  4. return evt["key"]

Nodejs version

  1. exports.handler = function(event, context, callback) {
  2. var eventObj = JSON.parse(event.toString());
  3. callback(null, eventObj['key']);
  4. };

The return result is value.

The input data is bytes from a file, such as an image passed in as the event parameter.

  1. 'use strict';
  2. var fs = require('fs');
  3. var gm = require('gm').subClass({
  4. imageMagick: true
  5. });
  6. module.exports.resize = function (event, context, callback) {
  7. // The event parameter passed in is a picture in .png format and is written to the /tmp directory.
  8. fs.writeFileSync("/tmp/serverless.png", event)
  9. fs.readFileSync("/tmp/serverless.png")
  10. new Promise(function (resolve, reject) {
  11. gm("/tmp/serverless.png").resize(128, 128, '!').write("/tmp/serverless_128.png", function (err) {
  12. if (err) {
  13. console.error('Failed to write image', err);
  14. reject('Failed to write image')
  15. callback(err)
  16. return
  17. }
  18. resolve('succ')
  19. })
  20. }).then(function () {
  21. // Read the result image from the /tmp directory
  22. callback(null, fs.readFileSync("/tmp/serverless_128.png"))
  23. })
  24. }
  1. # -*- coding: utf-8 -*-
  2. from wand.image import Image
  3. def resize(event, context):
  4. with Image(blob=event) as img:
  5. with img.clone() as i:
  6. i.resize(128, 128)
  7. return i.make_blob()

Note: Java runtime does not support the event parameter. When you use the StreamRequestHandler method, theinputStream parameter specifies the data that the function uses. When you use the PojoRequestHandler<I, O> method, you can customize the input and output types in generic mode. For more information, see Java runtime.

context

The runtime information generated when the user executes the function is stored in the context, including the user request ID, authentication information, and corresponding service and function information. The structure is as follows:

  1. {
  2. requestId: '886a2272-346f-c9ad-9db0-0dc0f35cb5b5',
  3. credentials: {
  4. accessKeyId: 'STS.abcdefghijklmn',
  5. accessKeySecret: 'abcdefghijklmnopqrstuvwxyz',
  6. securityToken: 'CAISh+********'
  7. },
  8. function: {
  9. name: 'tt',
  10. handler: 'index.handler',
  11. memory: 512,
  12. timeout: 60
  13. },
  14. service: {
  15. name: 'test',
  16. logProject: 'test-log-project',
  17. logStore: 'test-log-store'
  18. },
  19. region: 'cn-shanghai',
  20. accountId: '167*************'
  21. }

Use cases

  • The request ID can be obtained in the context and recorded in the function running log. The ID is used to track the details of the execution.

  • The user AK information can be obtained through the context and be used to access other services of Alibaba Cloud (using the access to OSS as an example) to avoid writing AK information in the code.

  • Basic information of applicable functions and services

For the context type of each runtime, see runtime documentation in the Programming Guide.

Example

The following example shows the process of accessing OSS using the personal information in context.

  1. import json
  2. import oss2
  3. def handler(event, context):
  4. evt = json.loads(event)
  5. creds = context.credentials
  6. auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
  7. bucket = oss2.Bucket(auth, 'oss-cn-hangzhou.aliyuncs.com', 'bucketName')
  8. bucket.put_object('motto.txt', 'Never give up. - Jack Ma')
  9. return 'success'