-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aws
committed
Jun 12, 2019
1 parent
bdd489f
commit 4fd6cd8
Showing
28 changed files
with
3,336 additions
and
254 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
AWS Greengrass Core SDK Js | ||
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
AWS Greengrass Core SDK for JavaScript | ||
Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
This product includes software developed at | ||
Amazon Web Services, Inc. (http://aws.amazon.com/). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Instructions can be found under manual/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates a simple publish to a topic using Greengrass Core NodeJS SDK | ||
* This lambda function will retrieve underlying platform information and send | ||
* a hello world message along with the platform information to the topic | ||
* 'hello/world'. The function will sleep for five seconds, then repeat. | ||
* Since the function is long-lived it will run forever when deployed to a | ||
* Greengrass core. | ||
*/ | ||
|
||
const ggSdk = require('aws-greengrass-core-sdk'); | ||
|
||
const iotClient = new ggSdk.IotData(); | ||
const os = require('os'); | ||
const util = require('util'); | ||
|
||
function publishCallback(err, data) { | ||
console.log(err); | ||
console.log(data); | ||
} | ||
|
||
const myPlatform = util.format('%s-%s', os.platform(), os.release()); | ||
const pubOpt = { | ||
topic: 'hello/world', | ||
payload: JSON.stringify({ message: util.format('Hello world! Sent from Greengrass Core running on platform: %s using NodeJS', myPlatform) }), | ||
}; | ||
|
||
function greengrassHelloWorldRun() { | ||
iotClient.publish(pubOpt, publishCallback); | ||
} | ||
|
||
// Schedule the job to run every 5 seconds | ||
setInterval(greengrassHelloWorldRun, 5000); | ||
|
||
// This is a handler which does nothing for this example | ||
exports.handler = function handler(event, context) { | ||
console.log(event); | ||
console.log(context); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates how to return a result back to the caller of this lambda | ||
*/ | ||
|
||
exports.handler = function handler(event, context, callback) { | ||
console.log(event); | ||
console.log(context); | ||
callback(undefined, { result: 'message' }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates how to invoke another lambda with binary data and receive the value from the call | ||
*/ | ||
|
||
const ggSdk = require('aws-greengrass-core-sdk'); | ||
|
||
const lambdaClient = new ggSdk.Lambda(); | ||
|
||
exports.handler = function handler(event, context) { | ||
console.log(event); | ||
console.log(context); | ||
|
||
const cxt = { | ||
custom: { | ||
customData: 'customData', | ||
}, | ||
}; | ||
const contextString = JSON.stringify(cxt); | ||
const buff = Buffer.from(contextString); | ||
const clientContext = buff.toString('base64'); | ||
|
||
const params = { | ||
FunctionName: 'arn:<partition>:lambda:<region>:<accountId>:function:<targetFunctionName>:<targetFunctionQualifier>', | ||
InvocationType: 'RequestResponse', | ||
ClientContext: clientContext, | ||
Payload: Buffer.from('payload message', 'utf8'), | ||
}; | ||
|
||
lambdaClient.invoke(params, (err, data) => { | ||
if (err) { | ||
console.error(err, err.stack); | ||
} else { | ||
console.log(data); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates how to invoke another lambda with json data and receive the value from the call | ||
*/ | ||
|
||
const ggSdk = require('aws-greengrass-core-sdk'); | ||
|
||
const lambdaClient = new ggSdk.Lambda(); | ||
|
||
exports.handler = function handler(event, context) { | ||
console.log(event); | ||
console.log(context); | ||
|
||
const cxt = { | ||
custom: { | ||
customData: 'customData', | ||
}, | ||
}; | ||
const contextString = JSON.stringify(cxt); | ||
const buff = Buffer.from(contextString); | ||
const clientContext = buff.toString('base64'); | ||
|
||
const params = { | ||
FunctionName: 'arn:<partition>:lambda:<region>:<accountId>:function:<targetFunctionName>:<targetFunctionQualifier>', | ||
InvocationType: 'RequestResponse', | ||
ClientContext: clientContext, | ||
Payload: JSON.stringify({ message: 'payload message' }), | ||
}; | ||
|
||
lambdaClient.invoke(params, (err, data) => { | ||
if (err) { | ||
console.error(err, err.stack); | ||
} else { | ||
console.log(data); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates a shadow operation using Greengrass Core NodeJS SDK | ||
* This lambda function will retrieve underlying platform information and | ||
* update the reported state of 'platform' shadow with that message. | ||
*/ | ||
|
||
const ggSdk = require('aws-greengrass-core-sdk'); | ||
|
||
const iotClient = new ggSdk.IotData(); | ||
const os = require('os'); | ||
const util = require('util'); | ||
|
||
const myPlatform = util.format('%s-%s', os.platform(), os.release()); | ||
const shadowUpdateParams = { | ||
thingName: 'platform', | ||
payload: JSON.stringify({ state: { reported: { platform: myPlatform } } }), | ||
}; | ||
const shadowGetParams = { | ||
thingName: 'platform', | ||
}; | ||
|
||
exports.handler = function handler(event, context) { | ||
console.log(context); | ||
|
||
// Update Thing Shadow | ||
console.log('Update Thing Operation'); | ||
iotClient.updateThingShadow(shadowUpdateParams, (err, data) => { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
} | ||
}); | ||
|
||
// Get Thing Shadow | ||
console.log('Shadow Get Operation'); | ||
iotClient.getThingShadow(shadowGetParams, (err, data) => { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
The TES example package contains a Lambda function that, when run, | ||
will attempt to retrieve AWS credentials. The goal of TES is to allow | ||
users to retrieve credentials without having them hard-coded on the | ||
system itself. | ||
|
||
Assuming no credentials exist on the system, when the Lambda function | ||
is run, temporary credentials will be sourced from the cloud. | ||
|
||
The credential retrieval is retrieved with an incremental backoff | ||
in case of an error. Users must be aware that the retrieval may not | ||
always succeed on the first try and retry should be performed with care. | ||
|
||
### SETUP ### | ||
1. Copy the included index.js to your Lambda function. | ||
|
||
2. Install aws-sdk by performing "npm install aws-sdk". | ||
|
||
3. Unzip the Greengrass Core SDK for NodeJS into node_modules folder. | ||
|
||
4. Zip the folder with index.js on the top level. | ||
|
||
5. Create a Lambda in the Lambda Console. | ||
Handler: index.handler | ||
Runtime: Node.js 8.10 | ||
Timeout: 3s | ||
Role: any basic execution role can be used here | ||
|
||
6. Create a group that contains your Greengrass Core and the Lambda | ||
function you've just created. | ||
|
||
7. In the Greengrass console, click on the Lambda function and set | ||
the Memory limit is at least 40MB and "Lambda lifecycle" is set to | ||
"Make this function long-lived and keep it running indefinitely". | ||
|
||
8. Under Settings page of the group, make sure the Group Role is | ||
set to the role you want to use within Greengrass when | ||
performing actions against AWS. | ||
|
||
9. Deploy the latest Greengrass to your device. | ||
|
||
10. Local log should display the access key, secret key and session token. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
*/ | ||
|
||
/* | ||
* Demonstrates retrieving a Group Role Credential if one is set. | ||
*/ | ||
|
||
const AWS = require('aws-sdk'); | ||
const util = require('util'); | ||
|
||
const credChain = new AWS.CredentialProviderChain(); | ||
|
||
// Max retry every 30 seconds to retrieve the credentials | ||
const maxRetryInterval = 30; | ||
// Initially, try with one second retry interval | ||
const initialRetryInterval = 1; | ||
|
||
function getCredential(retryInterval) { | ||
const promise = credChain.resolvePromise(); | ||
|
||
promise.then((creds) => { | ||
console.log(util.format('Access Key: %s\nSecret Key: %s\nSession Key: %s\n', creds.accessKeyId, creds.secretAccessKey, creds.sessionToken)); | ||
}, | ||
(err) => { | ||
console.log(err); | ||
// Try again with incremental backoff | ||
setTimeout(() => { | ||
getCredential(Math.min(maxRetryInterval, retryInterval * 2)); | ||
}, retryInterval); | ||
}); | ||
} | ||
|
||
getCredential(initialRetryInterval); | ||
|
||
// This is a handler which does nothing for this example | ||
exports.handler = function handler(event, context) { | ||
console.log(event); | ||
console.log(context); | ||
}; |
Oops, something went wrong.