AWS Greengrass with DynamoDB (Part – I)

AWS Greengrass, a service that extends Amazon Web Services functionality to Internet of Things (IoT) devices is now allowing a business to perform data collection and analysis quite closer to its origin. Know all about it in this post.

AWS Greengrass with DynamoDB (Part - I)

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 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.

2. Setup Raspberry pi:

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

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:
sudo adduser --system ggc_user
sudo addgroup --system ggc_group
2) To improve security on the Pi device, enable hardlink and softlink protection on the operating system at startup. a) Navigate to the 98-rpi.conf file
cd /etc/sysctl.d

Note: If you don’t see the 98-rpi.conf file, follow the instructions in the README.sysctl 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).
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
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:
sudo sysctl -a 2> /dev/null | grep fs.protected
You should see fs.protected_hardlinks = 1 and fs.protected_symlinks = 1.
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.
cd /boot/
b) Use a text editor to open cmdline.txt. Append the following to the end of the existing line, not as a new line.
cgroup_enable=memory cgroup_memory=1
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.
cd /home/pi/Downloads
mkdir greengrass-dependency-checker-GGCv1.9.x
cd greengrass-dependency-checker-GGCv1.9.x
sudo modprobe configs
sudo ./check_ggc_dependencies | more
Where more appears, press the Spacebar key to display another screen of text.

3. Installing the AWS IoT Greengrass Core Software:

  • 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 core device
  • You will be following whole module till 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:

Step1: Configure IAM Roles:

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.

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.
  • For Role name, enter Greengrass_DynamoDB_Role.
  • For Role description, enter Greengrass group role.

7. Repeat the previous step to create the role for 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.

9. Choose Settings, and then choose Add Role.

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 sent 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 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 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
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.
pip install boto3 -t path-to- 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.

9. Compress the contents of the AWS-Greengrass-With-DynamoDB folder into a .zip file named (Compress the folder’s contents, not the folder.) This is your Lambda function deployment package.

10. Open the Lambda console and choose Create function.
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.
  • Choose Author from scratch.

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 (The size of your file might be different from what’s shown here.)

c) Choose Save.

Note: 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.


To see your uploaded code, from Code entry type, choose Edit code inline.

15. Publish the Lambda function:

a) From Actions, choose Publish new version

b) For Version description, enter First version, and then choose Publish.

16. Create an alias for the Lambda 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.

a) From Actions, choose Create alias.

b) Name the alias GG_HelloWorld, set the version to 1 (which corresponds to the version that you just published), and then choose Create.


AWS IoT Greengrass doesn’t support Lambda aliases for $LATEST versions.

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 Lambdas, and then choose Add Lambda.

b) Choose Use existing Lambda.

18. Choose Greengrass_HelloWorld, and then choose Next

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.

20. Choose the ellipsis (…), and then choose Edit Configuration:

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
  • For Lambda lifecycle, choose Make this function long-lived and keep it running indefinitely.
  • Set Timeout to 25 seconds. This Lambda function sleeps for 20 seconds before each invocation
  • For Lambda lifecycle, choose Make this function long-lived and keep it running indefinitely.

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 asContainerization, 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.

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.

27. For Topic filter, enter hello/world4, 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

6. Verify the Lambda Function Is Running on the Device

To verify whether everything is running you can refer this link