Menu
AWS Greengrass
Developer Guide

Create a Lambda Function Definition

Now create a Lambda function definition. The Lambda function definition contains a list of functions that will be deployed to your AWS Greengrass core. In our scenario, there are two simple Lambda functions: uptime and message.

To make the Lambda functions available for Greengrass, you need to upload these Lambda functions to the Lambda console. Make sure that you are accessing the Lambda console in the same region as the Greengrass endpoint you are using. (If you are accessing Greengrass in the us-west-2 region, use the Lambda console in the us-west-2 region.) Create two Lambda functions: uptimeLambda and messageLambda.

Note

The source code for these Lambda functions are part of the AWS Greengrass Core SDK. You can download it from the AWS IoT console. In the navigation pane, choose Software, and then choose Download SDK for the AWS Greengrass Core SDK. Each Lambda function is contained in a zip file located in the examples/Storyline directory of the AWS Greengrass Core SDK.

Using the AWS Lambda console, create a Lambda function and choose Blank Function. Do not configure any triggers. Name the function uptimeLambda. For runtime, choose python 2.7. For Code entry type, choose Upload a .ZIP file, and then browse to the storyline_uptimeLambda.zip file.

For Handler, type uptimeLambda.uptime_handler. Choose any existing role. The role is not important here because AWS Greengrass doesn't use it. Review and then publish the function. Choose Actions, choose Publish new version, and then choose Publish. The ARN for the published Lambda looks like this: arn:aws:lambda:us-west-2:123451234510:function:uptimeLambda:1.

Associate a Lambda alias with this published version of your Lambda function. A Lambda alias is a pointer to a specific Lambda function version. Aliases are mutable (that is, they can be changed) and can be updated to point to different versions. For more information about Lambda aliases, see AWS Lambda Developer Guideversioning-aliases.html. Using an alias allows you to update a Lambda function without having to update subscriptions for each Greengrass group that references the Lambda function.

Note

The $LATEST alias is not supported for use with Greengrass.

From the Actions menu, choose Create alias. In Name, type "storyLineUptime", and choose the version that was just published.

Note

A Lambda alias is a name given to a specific version of a Lambda function. Using an alias allows the user to update the Lambda function without having to update the subscription table later. You can use any alias except $LATEST which is used internally for work-in-progress deployments.

Make sure the alias is pointing to the correct version. It will be automatically resolved during Greengrass deployment later.

Using the AWS Lambda console, create a Lambda function and choose Blank Function. Do not configure any triggers. Name the function messageLambda. For runtime, choose python 2.7. For Code entry type, choose Upload a .ZIP file, and then browse to the storyline_messageLambda.zip file that came with the release. For Handler, type messageLambda.message_handler. Choose any existing role. The role is not important because AWS Greengrass doesn't use it. Review the Lambda function and then publish it. From the Actions menu, choose Publish new version, and then choose Publish. The ARN for the published Lambda function looks like this: arn:aws:lambda:us-west-2:123451234510:function:messageLambda:1.

Associate an alias with the published version. From the Actions menu, choose Create alias. In Name, type "storyLineMessage", and choose the version that was just published. Using an alias allows the user to update the Lambda function without having to update the subscription table later. Make sure the alias is pointing to the correct version. It will be automatically resolved during Greengrass deployment later.

Note

You will need the versioned ARN with either a version or an alias in order to build your function list. Make sure you have published a version of your Lambda function and associated an alias with it.

Use the create-function-definition CLI command to create a Lambda function definition:

Copy
aws greengrass create-function-definition --name "MyFunctionDefinition"

Arguments for the create-function-definition CLI command:

--name

A name for the Lambda function definition.

The output of the create-function-definition CLI command looks like:

Copy
{ "LastUpdatedTimestamp": "2017-05-17T18:26:14.235Z", "CreationTimestamp": "2017-05-17T18:26:14.235Z", "Id": "ff44a4cf-a619-47c2-95b4-13c6daf8cf47", "Arn":"arn:aws:greengrass:us-west-2:123451234510:/greengrass/definition/functions/ff44a4cf-a619-47c2-95b4-13c6daf8cf47", "Name": "<MyFunctionDefinition>" }

Now, create a Lambda definition version by adding Lambda functions to the definition:

Copy
aws greengrass create-function-definition-version --function-definition-id "<FunctionDefinitionId>" --functions '[ { "Id": "An arbitrary element ID.", "FunctionArn": "<uptimeLambda-version-arn>", "FunctionConfiguration": { "Executable": "uptimeLambda.uptime_handler", "MemorySize": 128000, "Timeout": 3 } }, { "Id": "An arbitrary element ID.", "FunctionArn": "<messageLambda-version-arn>", "FunctionConfiguration": { "Executable": "messageLambda.message_handler", "MemorySize": 128000, "Timeout": 3 } }]'

Arguments to the create-function-definition-version CLI Command:

--function-definition-id

The ID of the function definition, returned from the create-function-definition CLI command. In the preceding example, it would be ff44a4cf-a619-47c2-95b4-13c6daf8cf47 for this entry in the list.

--functions

A list of JSON objects describing the functions that you are adding to the function definition.

Id

The element ID for this entry in the list.

FunctionArn

The ARN of the function version you are adding to the definition.

Note

The function version ARN must end in a version. For example "arn:aws:lambda:us-west-2:$ACCOUNT:function:UptimeLambda:1" is a valid function version ARN.

FunctionConfiguration

Configuration information for the function you are adding to the definition.

Executable

The name of the Lambda function handler.

MemorySize

The amount of memory to allocate for execution of the Lambda function.

Timeout

The timeout, in seconds, for the Lambda function.

The output from the create-function-definition-version CLI command looks like this:

Copy
{ "Arn" : "arn:aws:greengrass:us-west-2:123451234510:/greengrass/definition/lambdas/ff44a4cf-a619-47c2-95b4-13c6daf8cf47/versions/8bdd9e9f-c2be-41a0-8cb2-2b8e952013c3", "Id" : "ff44a4cf-a619-47c2-95b4-13c6daf8cf47", "Version" : "8bdd9e9f-c2be-41a0-8cb2-2b8e952013c3", "CreationTimestamp" : "2017-05-17T18:54:17.227Z" }

Make a note the ARN of the Lambda definition version. You will need it later.

You can view the Lambda function list using the get-function-version CLI command.

Copy
aws greengrass get-function-definition-version --function-definition-id "<FunctionDefinitionId>" --function-definition-version-id "<FunctionDefinitionVersion>"

Arguments for the get-function-definition-version CLI command:

--function-definition-id

The ID of the Lambda definition.

--function-definition-version-id

The ID of the function definition version.

The output of the get-function-definition-version CLI command will look like this:

Copy
{ "Definition": { "Functions": [ { "FunctionConfiguration": { "Executable": "messageLambda.message_handler", "MemorySize": 128000, "Timeout": 3 }, "Id": "message-lambda", "FunctionArn": "arn:aws:lambda:us-west-2:123451234510:function:messageLambda:storyLineMessage" }, { "FunctionConfiguration": { "Executable": "uptimeLambda.uptime_handler", "MemorySize": 128000, "Timeout": 3 }, "Id": "uptime-lambda", "FunctionArn": "arn:aws:lambda:us-west-2:123451234510:function:uptimeLambda:storyLineUptime" } ] }, "Version": "8bdd9e9f-c2be-41a0-8cb2-2b8e952013c3", "CreationTimestamp": "2017-05-17T18:54:17.227Z", "Id": "ff44a4cf-a619-47c2-95b4-13c6daf8cf47", "Arn": "arn:aws:greengrass:us-west-2:123451234510:/greengrass/definition/lambdas/ff44a4cf-a619-47c2-95b4-13c6daf8cf47/versions/8bdd9e9f-c2be-41a0-8cb2-2b8e952013c3" }