AWS IoT Greengrass software is readily available on PHYTEC's phyCORE-i.MX7 Linux BSP to enable cloud capabilities. Greengrass allows data to be collected, analyzed, and acted upon at the source, while using the cloud for storage, analytics, and securely connecting to other devices. Code can be deployed to individual targets or to device fleets through the cloud with AWS Lambda functions via Greengrass, facilitating the process of remote software updates and certificate rotation.  

This quick-start will demonstrate how to deploy a simple "Hello World" Lambda function to the phyCORE-i.MX7 Development Kit and view the results in the AWS IoT Console. 

This tutorial is based off of "Getting Started with AWS IoT Greengrass" in the AWS IoT Greengrass Developer Guide, which can be found here: https://docs.aws.amazon.com/greengrass/latest/developerguide/gg-gs.html.

Guide Requirements:

  • A phyCORE-i.MX7 Development Kit running BSP Yocto FSL iMX7 PD18.2.1 or later
    • You will need to have the i.MX7 booted into Linux. See this Quickstart for more information.
    • The i.MX7 will need to be connected to the internet. See this Ethernet guide for more information.
  • A Windows Host Machine (Other Host Machines may be used with slight modification to the steps below)



1. Create or log into your AWS account.

 Here is the current link to the console sign-in: https://portal.aws.amazon.com/billing/signup#/start


AWS Sign-in Page



2. Connect the phyBoard-Zeta to the Internet

If your i.MX7 isn't already connected to your network, use the following command with an Ethernet cable connected to ETH0:

Target (Linux)

ifconfig eth0 up && udhcpc -i eth0
CODE

See the Ethernet Peripheral Guide for more information.



3. Verify Greengrass Configuration

Run the following commands to make sure everything is correctly configured for Greengrass: 

Target (Linux)

mkdir ~/Downloads && cd ~/Downloads
wget https://github.com/aws-samples/aws-greengrass-samples/raw/master/greengrass-dependency-checker-GGCv1.7.1.zip
unzip greengrass-dependency-checker-GGCv1.7.1.zip
cd greengrass-dependency-checker-GGCv1.7.1
sudo modprobe configs
chmod +700 check_ggc_dependencies
sudo ./check_ggc_dependencies | more
CODE

Missing Node v6.10 and Java 8 is normal.

If there are missing dependencies beyond Node v6.10 and Java 8, then the Greengrass option was not correctly configured in the image build. Try rebuilding the image and then proceed to the next step when all of the required dependencies are configured and installed.

image2019-2-19_9-40-46.png



4. Create a Greengrass group

On the AWS IoT console, click "Greengrass" on the left side Panel and then click "Create a group".



image2019-3-14_8-12-49.png



5. Set Up your Greengrass Group

Select "use easy creation" to automatically generate certificates for the phyBoard-Zeta to connect to AWS Greengrass 

Then select "provision a core" in the IoT Registry

image2019-3-14_8-13-33.png



6. Name your Group

Name your group in the "Group Name" box and click "Next".

image2019-3-14_8-14-13.png



7. Name the Core

Use the name provided for the core, and click "Next".








image2019-3-14_8-14-42.png



8. Finalize Greengrass Group Creation

Click "Create Group and Core" to finalize creating the Greengrass Group

This group will serve as the AWS-side's resources for your set of devices.

image2019-3-14_8-15-5.png



9. Download the Security Certificate Package

On the confirmation page, download the certificate package. No need to download the software configuration package as this is pre-installed in BSP Yocto FSL iMX7 PD18.2.1.

Click "Finish".

This compressed security package file contains security certificates that enable secure communications between AWS IoT and the /greengrass/config/config.json file that contains configuration information specific to your AWS IoT Greengrass core and the AWS IoT endpoint.

image2019-3-14_8-16-0.png



10. Transfer the Certificate Package to the i.MX7

In order to use the SCP protocol you will first need the PSCP command line utility which is not a standard internal command in Windows. 

  • Use the Command Prompt to navigate to the directory containing the downloaded certificate package:

    Host (Windows)

    cd <insert-path-to-files>
    CODE
  • Use the following command to transfer your file: 

    Host (Windows)

    pscp -scp <insert-name-of-file> root@<insert-IP-address>:~/Downloads/
    CODE
  • Your copied file will appear in the ~/Downloads/ directory on the i.MX7.

Please see Copying Files to the Device for more information/strategies for transferring files between your i.MX7 and Host Machine. 



11. Decompress the Security Package

Decompress the certificate file and move the contents to the /greengrass directory.

Target (Linux)

cd ~/Downloads
tar -xzvf <insert hash-setup>.tar.gz -C /greengrass
CODE


12. Download a Root Certificate Authority (CA)

Get a copy of the root CA and store it on the i.MX7 at /greengrass/certs/.

Check that the root.ca.pem isn't empty.

Target (Linux)

cd /greengrass/certs/
wget -O root.ca.pem https://www.amazontrust.com/repository/AmazonRootCA1.pem
cat root.ca.pem
CODE

Expected Output

-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54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QnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----
CODE


13. Start the AWS GG daemon

Start the daemon and ensure it is actually running.

Target (Linux)

cd /greengrass/ggc/core
sudo ./greengrassd start
ps aux | grep -E 'greengrass.*daemon'
CODE

Example Output

root@imx7d-phyboard-zeta-004:/greengrass/ggc/core# ps aux | grep -E 'greengrass.*daemon'
root 705 2.0 1.5 896844 16196 ? Sl 23:06 0:02 /greengrass/ggc/packages/1.7.0/bin/daemon -core-dir /greengrass/ggc/packages/1.7.0 -greengrassdPid 699
CODE


14. Locate the SDK

In the AWS IoT Core Console select "Software" near the bottom of the left side panel.



15. Download the SDK

  • Scroll down to "SDKs" and under "AWS IoT Greengrass Core SDK", select "View all SDKs"
  • Click "v1.3.0" under "Python 2.7" to download the AWS IoT Greengrass Core SDK for Python

image2019-3-14_8-25-41.png

image2019-3-14_8-27-45.png



16. Decompress the SDK

Decompress the "greengrass-core-python-sdk-1.3.0.tar.gz" downloaded from the last step using 7-Zip. 

The latest release of 7-Zip can be found at their website HERE.

Right click the tarball in File Explorer and choose "7-Zip" → "Open archive" → and specify the desired location (image shows contents of folder) before selecting Extract.

image2019-2-21_14-45-35.png

Navigate to the sdk folder and Extract the python_sdk_1_3_0.zip. This will create a directory named greengrasssdk.



17. Zip the SDK and HelloWorld Script Together

Move the greengrasssdk folder into the aws_greengrass_core_sdk/examples/HelloWorld folder and zip the greengrassHelloWorld.py and greengrasssdk together.

image2019-2-21_15-51-16.png

HelloWorld.zip will be referred to as the Lambda Function Deployment Package.



18. Create the Hello World Lambda Function

On the Group page in the AWS IoT Console, select "Lambda" → "Add Lambda" → "Create new Lambda".


image2019-2-21_15-57-5.png



19. Configure the Lambda Function

In the AWS Lambda Console, fill out the "Author from Scratch" box like in the following screenshot:


Then select "Create function" on the bottom:

image2019-3-14_8-31-11.png



20. Upload the Lambda Function Deployment Package

  • Fill out the fields like in the image
    • Code entry type: .zip file
    • Runtime: Python 2.7
    • Handler: greengrassHelloWorld.function_handler
  • Upload the Lambda Function Deployment Package created in step 17.
  • Save the function (upper right hand corner)



21. Publish the Lambda Function

  • "Actions" → "Publish new version"
  • In "Version Description" , enter anything (e.g. "First version" and then "Publish"

image2019-3-14_8-32-39.png

image2019-3-14_8-32-58.png



22. Create an Alias for the Lambda Function

  • "Actions" → "Create alias"
  • Enter a name (e.g. "GG_HelloWorld")
  • Set version to "1"
  • Click "Create"

image2019-3-14_8-33-22.png

image2019-3-14_8-34-10.png



23. Add the Lambda to your Greengrass Group

  • Return to the group created in the AWS IoT Console
  • Choose "Use existing Lambda"
  • Select the Lambda created in the last step

image2019-3-14_8-34-48.png

image2019-3-14_8-35-10.png



24. Edit the Lambda's Configuration for the Group

  • For version, choose "Alias: GG_HelloWorld", or whatever alias was chosen for the Lambda
  • Set the settings as the ones in the lower image
  • Choose "Update"

image2019-3-14_8-35-51.png

image2019-3-14_8-36-17.png



25. Create a Subscription to the MQTT Topic

  • Choose "Subscriptions" and "Add your first Subscription"
  • For "Select a source", choose the "Lambdas" tab and then "Greengrass_HelloWorld"
  • For "Select a target" choose "Select"
  • Choose the "Services" tab, and then choose "IoT Cloud", "Next"
  • In the "Topic filter" section, type "hello/world", and then hit "Next"

image2019-3-14_8-36-48.png

image2019-3-14_8-37-30.png

image2019-3-14_8-37-54.png



26. Deploy the Lambda

  • Select "Actions" → "Deploy"
    • The first time may take awhile
    • If deployment takes 5 minutes or more, try:
      • "Actions" → "Reset Deployments"
      • Check the box that says "Do you want to force the reset"
      • Click "Reset deployment"
      • Redeploy like before
  • The "Deployments" tab will contain a log of successes/failures, also indicated by the colored dot in the heading
  • Choose "Automatic detection" in the next window

image2019-3-14_8-38-35.png

image2019-3-14_8-38-53.png



27. See the MQTT Messages

  • In the side bar of the AWS IoT Core console, choose "Test"
  • Choose "Subscribe to topic" in the left-hand column and set the settings to look like the lower right image
  • Now click "Subscribe to topic" to the right of the "Subscription Topic" field

image2019-3-14_8-39-41.png

image2019-3-14_8-40-5.png



28. Great Success!

There should now be a "Hello world!" MQTT message appearing in the AWS IoT test console every five or so seconds!


image2019-3-14_8-41-7.png