Lambda

Table of Contents

Client

class Lambda.Client

A low-level client to interact locally with the AWS Greengrass Core in order to invoke other deployed Lambda functions.

import greengrasssdk

client = greengrasssdk.client('lambda')

These are the available methods:

invoke(**kwargs)

Invokes a specific Lambda function.

In AWS Greengrass, you will need to specify the version that is deployed to Greengrass Core when invoking them by using the Qualifier field.

Request Syntax

response = client.invoke(
    FunctionName='string',
    InvocationType='Event'|'RequestResponse',
    ClientContext='string',
    Payload=b'bytes',
    Qualifier='string'
)
Parameters
  • FunctionName (string) --

    [REQUIRED]

    The Lambda function name.

    You can specify Amazon Resource Name (ARN) of the function (for example, arn:aws:lambda:us-west-2:account-id:function:ThumbNail:1 ).

  • InvocationType (string) -- With AWS Greengrass, by default, the Invoke API assumes RequestResponse invocation type. You can optionally request asynchronous execution by specifying Event as the InvocationType.
  • ClientContext (string) --

    Using the ClientContext you can pass client-specific information to the Lambda function you are invoking. You can then process the client information in your Lambda function as you choose through the context variable.

    The ClientContext JSON must be base64-encoded.

  • Payload (bytes) -- JSON that you want to provide to your Lambda function as input. This can also be in binary if the target lambda is configured to accept the binary format. If the target lambda is not configured to receive binary payload and you send a binary payload as a RequestResponse invocation, you will receive an error message stating that the target lambda could not parse the payload as JSON. If the target lambda is configured to receive binary data type and invoker sends JSON data, it is converted into binary.
  • Qualifier (string) --

    You can use this parameter to specify a Lambda function version that you are trying to invoke. The function version needs to match the one that was downloaded to GGC.

Return type

dict

Returns

Response Syntax

{
    'Payload': StreamingBody(),
    'FunctionError': 'string'
}

Response Structure

  • (dict) --

    Upon success, returns an empty response. Otherwise, throws an exception.

    • Payload (StreamingBody) --

      It is the object returned by the Lambda function. This is present only if the invocation type is RequestResponse .

      In the event of a function error this field contains a message describing the error.

    • FunctionError (string) --

      Indicates whether an error occurred while executing the Lambda function. If an error occurred this field will have Handled. Handled errors are errors that are reported by the function.

Examples

This operation invokes a Lambda function with client context


msg = json.dumps({
    'message':'foobar'
})

client_context = json.dumps({
    'custom':'custom text'
})

response = client.invoke(
    ClientContext=base64.b64encode(bytes(client_context)),
    FunctionName='arn:aws:lambda:us-west-2:123451234510:function:MyFunction',
    InvocationType='RequestResponse',
    Payload=msg,
    Qualifier='1'
)

logger.info(response)

Expected Output:

{
    'FunctionError': '',
    'Payload': '?'
}

This operation invokes a Lambda function with client context and non-JSON payload. It only works if the target lambda is configured to receive binary data type.



client_context = json.dumps({
    'custom':'custom text'
})

response = client.invoke(
    ClientContext=base64.b64encode(bytes(client_context)),
    FunctionName='arn:aws:lambda:us-west-2:123451234510:function:MyFunction',
    InvocationType='RequestResponse',
    Payload="Non-JSON Data",
    Qualifier='1'
)

logger.info(response)

Expected Output:

{
    'FunctionError': '',
    'Payload': '?'
}