Skip to content

Commit 71d3565

Browse files
author
aws
committed
Release of Version 1.4.0
1 parent 4fd6cd8 commit 71d3565

File tree

8 files changed

+580
-1
lines changed

8 files changed

+580
-1
lines changed

README

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<div id="content">
2+
3+
<div id="filecontents">
4+
5+
# AWS Greengrass Core SDK for JavaScript
6+
7+
The **AWS Greengrass Core SDK for JavaScript** allows developers to write JavaScript Lambda functions which will run within Greengrass.
8+
9+
## Overview
10+
11+
This document provides instructions for preparing your Greengrass Core environment to run Lambda functions written in JavaScript. It also includes examples on how to develop a Lambda function in JavaScript as well as packaging and running an example Hello World file in JavaScript for your Greengrass core.
12+
13+
## Preparing your Greengrass to run NodeJS Lambda functions
14+
15+
The environment where Greengrass is running on needs to be able to run NodeJS 8.10 applications.
16+
17+
* Install NodeJS 8.10 for your platform. You can download the newest NodeJS from [https://nodejs.org/en/download/](https://nodejs.org/en/download/).
18+
* When you untar the package downloaded from NodeJS website, you will find `node` file under `bin` directory.
19+
* Copy the file to _**/usr/bin**_ or _**/usr/local/bin**_ folder.
20+
* Rename the file to _**nodejs8.10**_
21+
* Make sure the file is not a symlink.
22+
23+
## Getting Started - Hello World
24+
25+
* Copy `samples/HelloWorld` folder to your workspace.
26+
* Create a folder `node_modules` under `HelloWorld` folder.
27+
* Unzip aws-greengrass-core-sdk-js.zip into the folder. It should create a folder HelloWorld/node_modules/aws-greengrass-core-sdk
28+
* Zip up the content of HelloWorld folder so that the index.js is on the top of the zip file structure.
29+
* Go to AWS Lambda Console.
30+
* Create a new function.
31+
* Choose the Runtime as `Node.js 8.10`
32+
* Upload the zip file in _Lambda function code_ section.
33+
* Handler is _index.handler_
34+
* Choose any role as the role is not used within Greengrass.
35+
* After creating the function, publish the Lambda.
36+
* Create an Alias and point to the Published version (not $LATEST).
37+
* Go to your Greengrass Group and add the Lambda under Lambdas section.
38+
* Click on the Lambda and change the _Lambda lifecycle_ to _Make this function long-lived and keep it running indefinitely._
39+
* Add a Subscription with the following configuration:
40+
* Source: Lambda which you just created and added to the group
41+
* Target: IoT Cloud
42+
* Topic: hello/world
43+
* Deploy. A message from your Lambda should be published to the topic _hello/world_ in the cloud every 5 seconds. You can check this by going to AWS IoT's _Test_ page and subscribing to topic _hello/world_.
44+
45+
## Including aws-greengrass-core-sdk with your function
46+
47+
Unzip the SDK into your node_modules folder of your function. This should create a aws-greengrass-core-sdk folder which includes the SDK.
48+
49+
## Logging in NodeJS Lambdas
50+
51+
Your _console.log_ operation will be logged as INFO. A _console.error_ operation will be logged as ERROR. Currently, our NodeJS SDK only allows you to log at INFO or ERROR level only.
52+
53+
## Supported Datatypes
54+
55+
From GGC version 1.5, you can send both JSON and binary data as a payload when you invoking other Lambdas or publishing a message using IotData service. In order to make your lambda be able to handle binary payload, you need to configure the lambda in Greengrass console to mark it using binary input payload so that GGC can know how to deal with the data.
56+
57+
## Supported Context
58+
59+
In Greengrass, you can send a context object in a JSON format to be passed to another Lambda that is being invoked. The context format looks like this: `{ custom: { customData: 'customData', }, }`
60+
61+
## Compatibility
62+
63+
</div>
64+
65+
<div class="section" id="compatibility">
66+
67+
## Compatibility[](#compatibility "Permalink to this headline")
68+
69+
As new features are added to AWS Greengrass, previous versions of the Greengrass SDK will be incompatible with newer versions of the AWS Greengrass core. The following table lists the compatible SDKs for all GGC releases.
70+
71+
72+
<table style="width:50%">
73+
74+
<tbody>
75+
76+
<tr>
77+
78+
<th>GGC Version</th>
79+
80+
<th>Compatible SDK Versions</th>
81+
82+
</tr>
83+
84+
<tr>
85+
86+
<td>1.0.x-1.6.x</td>
87+
88+
<td>1.0.x-1.2.x</td>
89+
90+
</tr>
91+
92+
<tr>
93+
94+
<td>1.7.x-1.8.x</td>
95+
96+
<td>1.0.x-1.3.x</td>
97+
98+
</tr>
99+
100+
<tr>
101+
102+
<td>1.9.x</td>
103+
104+
<td>1.0.x-1.4.x</td>
105+
106+
</tr>
107+
108+
</tbody>
109+
110+
</table>
111+
112+
</div>
113+
114+
<div class="Section" id="1.4.0updates">
115+
116+
## 1.4.0 Updates[](#1.4.0updates "Permalink to this headline")
117+
118+
Added support for Node.js 8.10 Lambda runtime. Lambda functions that use Node.js 8.10 runtime can now run on an AWS IoT Greengrass core. (Existing Lambda functions that use Node.js 6.10 runtime can still run on Greengrass core, but they can’t be updated after 5/30/2019\. Please refer to [AWS Lambda Runtimes Support Policy](https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html).)
119+
120+
</div>
121+
122+
<div class="Section" id="1.3.1updates">
123+
124+
## 1.3.1 Updates[](#1.3.1updates "Permalink to this headline")
125+
126+
Improved log level granularity.
127+
128+
</div>
129+
130+
<div class="Section" id="1.3.0updates">
131+
132+
## 1.3.0 Updates[](#1.3.0updates "Permalink to this headline")
133+
134+
SDK supports SecretsManager client.
135+
136+
</div>
137+
138+
<div class="Section" id="1.2.0updates">
139+
140+
## 1.2.0 Updates[](#1.2.0updates "Permalink to this headline")
141+
142+
SDK and GGC compatibility check takes place in the background.
143+
144+
</div>
145+
146+
<div class="Section" id="1.1.0updates">
147+
148+
## 1.1.0 Updates[](#1.1.0updates "Permalink to this headline")
149+
150+
Lambda only accepted payload in JSON format. With this update, Invoking or publishing binary payload to a lambda is supported.
151+
152+
</div>
153+
154+
<div class="Section" id="1.0.1updates">
155+
156+
## 1.0.1 Updates[](#1.0.1updates "Permalink to this headline")
157+
158+
SShadow operations were not receiving responses from the local shadow properly. This has been fixed.
159+
160+
Lambda Invoke function's InvocationType's default value was Event. This has been changed to RequestResponse.
161+
162+
</div>
163+
164+
## Getting Help
165+
166+
* [Ask on a Greengrass forum](https://forums.aws.amazon.com/forum.jspa?forumID=254)
167+
168+
## License
169+
170+
Apache 2.0</div>
171+
172+
</div>
173+

aws-greengrass-core-sdk/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*/
4+
exports.GreengrassInterfaceVersion = '1.3';
5+
exports.Lambda = require('./lambda');
6+
exports.IotData = require('./iotdata');
7+
exports.SecretsManager = require('./secretsmanager');

aws-greengrass-core-sdk/iotdata.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*/
4+
5+
const Buffer = require('buffer').Buffer;
6+
7+
const Lambda = require('./lambda');
8+
const Util = require('./util');
9+
const GreengrassCommon = require('aws-greengrass-common-js');
10+
11+
const envVars = GreengrassCommon.envVars;
12+
const MY_FUNCTION_ARN = envVars.MY_FUNCTION_ARN;
13+
const SHADOW_FUNCTION_ARN = envVars.SHADOW_FUNCTION_ARN;
14+
const ROUTER_FUNCTION_ARN = envVars.ROUTER_FUNCTION_ARN;
15+
16+
class IotData {
17+
constructor() {
18+
this.lambda = new Lambda();
19+
}
20+
21+
getThingShadow(params, callback) {
22+
/*
23+
* Call shadow lambda to obtain current shadow state.
24+
* @param {object} params object contains parameters for the call
25+
* REQUIRED: 'thingName' the name of the thing
26+
*/
27+
const thingName = Util.getRequiredParameter(params, 'thingName');
28+
if (thingName === undefined) {
29+
callback(new Error('"thingName" is a required parameter.'), null);
30+
return;
31+
}
32+
33+
const payload = '';
34+
this._shadowOperation('get', thingName, payload, callback);
35+
}
36+
37+
updateThingShadow(params, callback) {
38+
/*
39+
* Call shadow lambda to update current shadow state.
40+
* @param {object} params object contains parameters for the call
41+
* REQUIRED: 'thingName' the name of the thing
42+
* 'payload' the state information in JSON format
43+
*/
44+
const thingName = Util.getRequiredParameter(params, 'thingName');
45+
if (thingName === undefined) {
46+
callback(new Error('"thingName" is a required parameter.'), null);
47+
return;
48+
}
49+
50+
const payload = Util.getRequiredParameter(params, 'payload');
51+
if (payload === undefined) {
52+
callback(new Error('"payload" is a required parameter.'), null);
53+
return;
54+
}
55+
56+
this._shadowOperation('update', thingName, payload, callback);
57+
}
58+
59+
deleteThingShadow(params, callback) {
60+
/*
61+
* Call shadow lambda to delete the shadow state.
62+
* @param {object} params object contains parameters for the call
63+
* REQUIRED: 'thingName' the name of the thing
64+
*/
65+
const thingName = Util.getRequiredParameter(params, 'thingName');
66+
if (thingName === undefined) {
67+
callback(new Error('"thingName" is a required parameter.'), null);
68+
return;
69+
}
70+
71+
const payload = '';
72+
this._shadowOperation('delete', thingName, payload, callback);
73+
}
74+
75+
publish(params, callback) {
76+
/*
77+
* Publishes state information.
78+
* @param {object} params object contains parameters for the call
79+
* REQUIRED: 'topic' the topic name to be published
80+
* 'payload' the state information in JSON format
81+
*/
82+
const topic = Util.getRequiredParameter(params, 'topic');
83+
if (topic === undefined) {
84+
callback(new Error('"topic" is a required parameter'), null);
85+
return;
86+
}
87+
88+
const payload = Util.getRequiredParameter(params, 'payload');
89+
if (payload === undefined) {
90+
callback(new Error('"payload" is a required parameter'), null);
91+
return;
92+
}
93+
94+
const context = {
95+
custom: {
96+
source: MY_FUNCTION_ARN,
97+
subject: topic,
98+
},
99+
};
100+
101+
const buff = Buffer.from(JSON.stringify(context));
102+
const clientContext = buff.toString('base64');
103+
104+
const invokeParams = {
105+
FunctionName: ROUTER_FUNCTION_ARN,
106+
InvocationType: 'Event',
107+
ClientContext: clientContext,
108+
Payload: payload,
109+
};
110+
111+
console.log(`Publishing message on topic "${topic}" with Payload "${payload}"`);
112+
113+
this.lambda.invoke(invokeParams, (err, data) => {
114+
if (err) {
115+
callback(err, null); // an error occurred
116+
} else {
117+
callback(null, data); // successful response
118+
}
119+
});
120+
}
121+
122+
_shadowOperation(operation, thingName, payload, callback) {
123+
const topic = `$aws/things/${thingName}/shadow/${operation}`;
124+
const context = {
125+
custom: {
126+
subject: topic,
127+
},
128+
};
129+
130+
const clientContext = Buffer.from(JSON.stringify(context)).toString('base64');
131+
const invokeParams = {
132+
FunctionName: SHADOW_FUNCTION_ARN,
133+
ClientContext: clientContext,
134+
Payload: payload,
135+
};
136+
137+
console.log(`Calling shadow service on topic "${topic}" with payload "${payload}"`);
138+
this.lambda.invoke(invokeParams, (err, data) => {
139+
if (err) {
140+
callback(err, null);
141+
} else {
142+
callback(null, data);
143+
}
144+
});
145+
}
146+
}
147+
148+
module.exports = IotData;

0 commit comments

Comments
 (0)