AWS Greengrass with DynamoDB
AWS Greengrass
with DynamoDB
1. Introduction:
AWS IoT Greengrass is software that extends cloud
capabilities to local devices. This enables devices to collect and analyze data
closer to the source of information, react autonomously to local events, and communicate
securely with each other on local networks. AWS IoT Greengrass developers can
use AWS Lambda functions and store data on DynamoDB. So, for this blog, we will
store the local data to DynamoDB using Greengrass on the local device.
·
As shown in the figure we will follow the
further flow in the following manner we have discussed flow before the block in
previous blogs.
·
You
can refer our earlier blog WIRELESS COMMUNICATION BETWEEN ARDUINO AND RASPBERRY
PI USING NRF24L01 Part 2
for setting up of the Raspberry Pi or visit the AWS documentation for Setting
up of the Raspberry Pi https://docs.aws.amazon.com/greengrass/latest/developerguide/setup-filter.rpi.html
1)
You are now ready to set up the Raspberry Pi for AWS IoT Greengrass.
First, run the following commands from a local Raspberry Pi terminal window or
an SSH terminal window:
2)
To
improve security on the Pi device, enable hard link, and soft link protection on
the operating system at startup.
a)
Navigate
to the 98-rpi.conf file
b)
Use
a text editor (such as Leafpad, GNU nano, or vi) to add the following two lines
to the end of the file. You might need to use the sudo command to edit as root
(for example, sudo nano 98-rpi.conf).
3) Reboot the Pi.
4)
After about a minute, connect to the Pi using SSH and
then run the following command to confirm the change:
5)
Edit
your command line boot file to enable and mount memory cgroups. This allows AWS
IoT Greengrass to set the memory limit for Lambda functions.
a)
Navigate to your boot directory.
c)
Now reboot the Pi.
6)
Your
Raspberry Pi should now be ready for AWS IoT Greengrass.
7) To make sure that you
have all required dependencies, download and run the Greengrass dependency
checker from the AWS
IoT Greengrass Samples repository on GitHub. These
commands unzip and run the dependency checker script in
the Downloads directory.
Where more
appears, press the Spacebar key to display another screen of text.
·
This shows
you how to install the AWS IoT Greengrass Core software on your device. This tutorial
provides instructions for setting up a Raspberry Pi, but you can use any
supported device.
·
Below is
Step by Step link provided by AWS Greengrass documentation to install its
software on the core device
·
You
will be following the whole module until the Greengrass is successfully running on
the core device.


·
The end result should look like this.
4. Lambda
Functions on AWS IoT Greengrass with DynamoDB:
·
When you create Lambda functions that
access AWS services, you must grant sufficient permissions in the Greengrass
group role to allow the functions to access those services. The group role is
an IAM role that you attach to your group.
·
In
this module, you create a role that allows a Greengrass Lambda function to
access DynamoDB. For more information about IAM, see the AWS Identity and Access Management
documentation.
1. In
the IAM console, in the navigation pane, choose Roles and then choose Create
Role.
2. Under Select type of trusted entity, choose AWS service.
2. Under Select type of trusted entity, choose AWS service.
3. Under
Choose the service that will use this role, choose Greengrass,
and then choose Next: Permissions.
4. On
the Attach permissions policies page, select the following policies and
then choose Next: Tags.
·
AWSGreengrassResourceAccessRolePolicy
·
AWSGreengrassFullAccess
·
AmazonDynamoDBFullAccess
5. Choose
Next: Review. You don't need to create tags for this tutorial.
6. Enter
the following values, and then choose Create role.
7. Repeat
the previous step to create the role of the AWS Lambda service. Select
the same policies (AWSGreengrassResourceAccessRolePolicy,
AWSGreengrassFullAccess, andAmazonDynamoDBFullAccess). For Role name,
enter Lambda_DynamoDB_Role.
8. In
the AWS IoT Core console, under Greengrass, choose Groups and
then choose your AWS IoT Greengrass group.
10. Choose
Greengrass_DynamoDB_Role from the list of roles, and then choose Save.
Step2: Create and Configure the Lambda Function:
1. In
this step, you create a Lambda function that sends the water level sensed by
Arduino to Raspberry Pi, the Lambda function is created in such a way that the
table is created and the data which is useful that is stored in the DynamoDB table.
2. On
your computer, create a folder named AWS-Greengrass-With-DynamoDB
3. From
the AWS-Greengrass-With-DynamoDB repository
on GitHub, download the greengrassHelloWorld.py function to the AWS-Greengrass-With-DynamoDB
folder.
4. To
run on an AWS IoT Greengrass core, a Python Lambda function requires the AWS
IoT Greengrass Core SDK for Python. For this tutorial, you include the
greengrasssdk folder in the Lambda function deployment package.
5. Download
the AWS IoT Greengrass Core SDK for Python from the AWS
IoT Greengrass Core SDK downloads page. In the Python section,
choose v1.4.0 - Current version (on GitHub) and then download the SDK.
6. Unzip
the downloaded package to get the SDK.
·
The greengrasssdk folder. This is the SDK.
7. Copy
greengrasssdk to AWS-Greengrass-With-DynamoDB the folder that
contains greengrassHelloWorld.py.
8. Run the following command in a
command-line window to install the boto3 (AWS SDK for Python) package and its
dependencies in the AWS-Greengrass-With-DynamoDB folder.
This results in a directory listing similar to the following:
Greengrass Lambda functions use the AWS SDK to access other AWS services. For more information, see Boto 3 - The AWS SDK for Python.
This results in a directory listing similar to the following:
Greengrass Lambda functions use the AWS SDK to access other AWS services. For more information, see Boto 3 - The AWS SDK for Python.
9. Compress
the contents of the AWS-Greengrass-With-DynamoDB folder into a .zip file named hello_world_python_lambda.zip.
(Compress the folder's contents, not the folder.) This is your Lambda function
deployment package.
11. Choose
Author from scratch.
12. Name
your function Greengrass_HelloWorld, and set the remaining fields as
follows:
·
Open the Lambda console and choose Create
function.
13. Choose Create function.
14. Upload
your Lambda function deployment package:
a) On
the Configuration tab, under Function code, set the following
fields:
i.
For Code entry type, choose Upload a .zip
file.
ii.
For Runtime, choose Python 2.7.
iii.
For Handler, enter greengrassHelloWorld.function_handler
b) Choose
Upload, and then choose hello_world_python_lambda.zip. (The size of your
hello_world_python_lambda.zip file might be different from what's shown here.)

The Test button on the AWS Lambda console
doesn't work with this function. The AWS IoT Greengrass Core SDK doesn't
contain modules that are required to run your Greengrass Lambda
functions independently in the AWS Lambda console.
Tip
To see your uploaded code, from Code entry type,
choose Edit code inline.
15. Publish
the Lambda function:
16. Create
an alias for the Lambda function version
Note
Greengrass groups can reference a Lambda function by alias (recommended) or by version. Using an alias makes it easier to manage code updates because you don't have to change your subscription table or group definition when the function code is updated. Instead, you just point the alias to the new function version.
Greengrass groups can reference a Lambda function by alias (recommended) or by version. Using an alias makes it easier to manage code updates because you don't have to change your subscription table or group definition when the function code is updated. Instead, you just point the alias to the new function version.
b) Name
the alias GG_HelloWorld, set the version to 1 (which corresponds
to the version that you just published), and then choose Create.
Note
17. In
the AWS IoT Core console, add the Lambda function that you just created to your
AWS IoT Greengrass group:
a) On
the group configuration page, choose Lambda and then choose Add
Lambda.
19. For
the version, choose Alias: GG_HelloWorld, and then choose Finish. You
should see the Greengrass_HelloWorld Lambda function in your group,
using the GG_HelloWorld alias.
21. On
the Group-specific Lambda configuration page, make the following
changes:
·
Set Timeout to 25 seconds. This
Lambda function sleeps for 20 seconds before each invocation
Note
A long-lived Lambda function starts
automatically after AWS IoT Greengrass starts and keeps running in its own
container (or sandbox). This is in contrast to an on-demand Lambda
function, which starts when invoked and stops when there are no tasks
left to execute. For more information, see Lifecycle
Configuration for Greengrass Lambda Functions.
22. Keep the default
values for all other fields, such as Run as, Containerization, and Input
payload data type, and choose Update to save your changes
23. Next,
create a subscription that allows the Lambda to send MQTT messages to
AWS IoT.
A Greengrass Lambda
function can exchange MQTT messages with:
24. On
the group configuration page, choose Subscriptions and then choose Add
your first Subscription.

25. In
Select a source, choose Select. Then, on the Lambdas tab, choose Greengrass_HelloWorld
as the source.


26. For
Select a target, choose Select. Then, on the Service tab, choose IoT
Cloud, and then choose Next.

28. Choose Finish
5. Deploy
Cloud Configurations to an AWS IoT Greengrass Core Device
To deploy this lambda function on
core device refer this link https://docs.aws.amazon.com/greengrass/latest/developerguide/configs-core.html
6. Verify
the Lambda Function Is Running on the Device
To verify whether everything is running you
can refer this link
Comments
Post a Comment