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.
- A phyCORE-i.MX7 Development Kit running BSP Yocto FSL iMX7 PD18.2.1 or later
- 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
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:
ifconfig eth0 up && udhcpc -i eth0
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:
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
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.
4. Create a Greengrass group
On the AWS IoT console, click "Greengrass" on the left side Panel and then click "Create a group".
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
6. Name your Group
Name your group in the "Group Name" box and click "Next".
7. Name the Core
Use the name provided for the core, and click "Next".
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.
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.
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.
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 this link to download the most recent, stable version of pscp.exe (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html).
- Move pscp.exe to your Programs Folder and setup a PATH variable for ease of use.
Use the Command Prompt to navigate to the directory containing the downloaded certificate package:
Use the following command to transfer your file:
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.
11. Decompress the Security Package
Decompress the certificate file and move the contents to the /greengrass directory.
cd ~/Downloads tar -xzvf <insert hash-setup>.tar.gz -C /greengrass
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.
cd /greengrass/certs/ wget -O root.ca.pem https://www.amazontrust.com/repository/AmazonRootCA1.pem cat root.ca.pem
-----BEGIN CERTIFICATE----- MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6 b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM 9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6 VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L 93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU 5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy rqXRfboQnoZsG4q5WTP468SQvvG5 -----END CERTIFICATE-----
13. Start the AWS GG daemon
Start the daemon and ensure it is actually running.
cd /greengrass/ggc/core sudo ./greengrassd start ps aux | grep -E 'greengrass.*daemon'
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
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
16. Decompress the SDK
Decompress the "greengrass-core-python-sdk-1.3.0.tar.gz" downloaded from the last step using 7-Zip.
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.
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.
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".
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:
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"
22. Create an Alias for the Lambda Function
- "Actions" → "Create alias"
- Enter a name (e.g. "GG_HelloWorld")
- Set version to "1"
- Click "Create"
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
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"
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"
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
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
28. Great Success!
There should now be a "Hello world!" MQTT message appearing in the AWS IoT test console every five or so seconds!