R-Car/Boards/Yocto-Gen3/AWS IoT Greengrass/v5.9.0

Introduction
This page describes how to setup the Yocto environment to use the AWS IoT Greengrass with R-Car and run it.

This page contains information abot building and running AWS IoT Greengrass on Yocto environment on:
 * Renesas R-Car-H3 Starter Kit Premier (unofficial name - H3ULCB)
 * Renesas R-Car-M3 Starter Kit Pro (unofficial name - M3ULCB)
 * https://www.renesas.com/us/en/products/automotive-products/automotive-system-chips-socs/r-car-h3-m3-starter-kit
 * R-Car/Boards/CCPF-SK
 * http://www.shimafuji.co.jp/products/2308

Boards confirmed to work
NT='Not Tested' but will work

See also:
 * R-Car/Boards/H3SK
 * R-Car/Boards/M3SK

Getting Started using prebuilt binaries

 * 1) Download prebuilt binaries
 * 2) * Download Yocto BSP binary with Greengrass Core from following page:
 * Binary:
 * Download for Starter Kit Premiere(H3)
 * Download for Starter Kit Pro(M3)
 * Download for CCPF-SK + Starter Kit Premiere(H3)
 * Download for CCPF-SK + Starter Kit Pro(M3)
 * Source code is not needed for this guide. But, if source code is required, please refer to following:
 * Source:
 * Download for Starter Kit Premiere(H3)
 * Download for Starter Kit Pro(M3)
 * Download for CCPF-SK + Starter Kit Premiere(H3)
 * Download for CCPF-SK + Starter Kit Pro(M3)
 * 1) * Extract downloaded files
 * ex.) R-Car Starter Kit Premier(H3)
 * Unzip Yocto-v5.9.0-ccpfsk-ggc-h3ulcb-binary.zip -d
 * 1) Setup SW
 * 2) * Setup SW (Prepare SD card, Flashing IPL, Booting). Please refer to R-Car/Boards/CCPF-SK/GettingStarted/Linux

Required packages

 * 1) Install required packages
 * Refer to Yocto Project Quick Start for more information.
 * If the OS version of the host PC is earlier than "Ubuntu 20.04", the default gcc version is old, so use "gcc-9/g++-9" or later version.
 * If the OS version of the host PC is earlier than "Ubuntu 20.04", the default gcc version is old, so use "gcc-9/g++-9" or later version.


 * 1) Set up initial git configuration

Building the BSP

 * 1) Create the following script(build.sh) for building
 * 2) Build
 * target_board_name is specified for each board according to the following table:
 * R-car-sk-target-board-name.png
 * Depending on the performance of the host PC, it may take several hours for the build to complete.
 * When the build completes successfully, you will see the following output:
 * 1) Check the built images
 * The built images are stored in the following:
 * -build/build__bsp(-release)/tmp/deploy/images//
 * 1) Write the images to the SD card
 * Write the images to the SD card with reference to Loading kernel and rootfs via eMMC/SD card.
 * Please replace "core-image-weston" with "core-image-minimal".
 * 1) Write the images to the SD card
 * Write the images to the SD card with reference to Loading kernel and rootfs via eMMC/SD card.
 * Please replace "core-image-weston" with "core-image-minimal".

Confirmation of Greengrass Core activation

 * 1) Turn on the target board
 * 2) Insert the SD card into the target board, and connect the LAN cable to it.
 * 3) Use a microUSB cable to connect the target board, connect a power supply to it, and connect to serial console.
 * 4) The PC will start the terminal software and make a serial connection.
 * 5) Power on.
 * Short-press SW8 "Power" to switch the board on.
 * If the board is attached to the CCPF board, short-press SW4 "Power" instead of SW8.
 * 1) Configure U-Boot to boot from SD card
 * Refer to the Configure U-Boot to boot from SD card.
 * 1) Check that the Greengrass Core is running.
 * Aws-status.png
 * Aws-status.png

Configure and check AWS IoT Greengrass
In subsequent chapters, the tags will be included in the title as follows.
 * Work on the target board (R-Car SK) is described as .
 * Work on the AWS Cloud is described as .
 * Work on the host PC is described as .
 * 1)  Create an AWS account
 * https://console.aws.amazon.com/console/home
 * Create an AWS account and log in to the AWS Management Console as the root user.
 * 1)  Creating an IAM user
 * Create an IAM user for work with reference to the following.
 * https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_users_create.html#id_users_create_console
 * The following policies should be attached at the time of creation.
 * AWSLambda_FullAccess
 * IAMFullAccess
 * AWSIoTFullAccess
 * AWSGreengrassFullAccess
 * IAMAccessAnalyzerFullAccess
 * Be sure to write down the "Access Key ID", "Secret Access Key", and "Password" that are displayed when users access the system. Note that the secret access key and password can only be confirmed at this time.
 * These three items are very important information for security reasons, and should be managed with great care.
 * Sign out the AWS root user and log in with the IAM user you created.
 * https://console.aws.amazon.com/console/home
 * 1)  Setting up a region
 * Set the desired region from the upper right corner of the screen.
 * In this procedure, we will set "Asia Pacific(Tokyo) ap-northeast-1".
 * 1)  Creating an IoT Core device
 * Run "Confirmation of Greengrass Core activation" beforehand and make sure that the terminal can connect to the Internet by connecting a LAN cable.
 * 1)  Select "IoT Core" from AWS services.
 * Aws-iot-core.png
 * 1)  Select "Greengrass" → "Getting started" from the side menu.
 * 2)  Press "Set up one core device".
 * 3)  Follow the instructions on the screen to perform the following tasks.
 * 4) <Web> "Step 1: Register a Greengrass core device"
 * Enter a core device name of your choice.
 * In this procedure, the core device name shall be "R-CarH3SKDevice".
 * 1) <Web> "Step 2: Add to a thing group to apply a continuous deployment"
 * Check "Enter a new group name" and enter a name of your choice.
 * In this procedure, the thing group name shall be "R-CarH3SKDeviceGroup".
 * 1) <Terminal> "Step 3: Install the Greengrass Core software"
 * 2) "Step 3.1: Install Java on the device"
 * No work required as it is included in the image generated by the R-Car SK build.
 * 1) "Step 3.2: Configure AWS credentials on the device"
 * Execute the following command to set the information to access AWS as an environment variable.
 * 1) "Step 3.3: Run the installer"
 * "Download the installer" requires no work.
 * "Run the installer" does not match the path displayed in the web browser, so execute the following command in the terminal.
 * Replace "R-CarH3SKDevice" and "R-CarH3SKDeviceGroup" with the values set in "Step 1" and "Step 2".
 * When the above command is executed, the R-Car SK terminal communicates with AWS IoT and is registered as a Greengrass core device.
 * After the command finishes, Greengrass CLI will be deployed to the terminal, and check if the following commands can be executed.
 * You can check the deployment status in the following log.
 * 1) <Web> Add policies to be sent to IoT Core
 * The examples in this document are intended only for dev environments.
 * All devices in your production fleet must have credentials with privileges that authorize only intended actions on specific resources.
 * The specific permission policies can vary for your use case. Identify the permission policies that best meet your business and security requirements.
 * For more information, refer to Example policies and Security Best practices.
 * Add a policy for the Lambda function running on the terminal to send messages to the IoT Core in the AWS cloud.
 * 1) Select "IAM" from AWS services.
 * 2) Select "Policies" from the side menu.
 * 3) Select "Create Policy".
 * 4) Select "IoT" for Service, "Publish" for Actions, and "All Resources" for Resources.
 * Aws-iot-core-policy.png
 * 1) Click on "Next: Tags" and "Next: Review", then enter any "Name" in the "Review policy", and click on the "Create Policy" button.
 * In this procedure, the name shall be "R-CarH3SKDevice-IoT-Publish".
 * 1) Return to the IAM screen and select "Roles" from the side menu.
 * 2) Enter "GreengrassV2TokenExchangeRole" in the search and select the role with the same name.
 * 3) Click "Attach Policies", search for the policy you just created, and click "Attach Policy".
 * 4) <Terminal> Preparing the execution environment for Lambda functions
 * Build an environment to run the sample program on a terminal.
 * Execute the following commands.
 * 1) <Web><PC> Creating a Lambda Function
 * Create a Lambda function to deploy to the terminal.
 * 1) <PC> Run the following command to create a template for a Lambda function.
 * 2) <PC> Copy and paste the following code into the lambda_function.py that you created.
 * The following part of the above code needs to be replaced with its own endpoint.
 * https://xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com
 * The endpoints can be found below.
 * 1) <Web> Select "IoT Core" from AWS services.
 * 2) <Web> Select "Settings" from the side menu.
 * 3) <Web> Refer to "Endpoint" in Device data endpoint.
 * 4) <PC> Zip the packages.
 * Zip the lambda_function.py and boto3 packages you have created.
 * The zip file is copied to a PC that has access to the AWS cloud for uploading to the cloud.
 * 1) <Web> Registering the Lambda function
 * Register the created Lambda function to the AWS cloud.
 * 1) Select "Lambda" from AWS services.
 * 2) Select "Functions" from the side menu.
 * 3) Select "Create function".
 * Aws-lambda-function-create-1.png
 * 1) Check the "Author from scratch" box.
 * 2) Give an arbitrary name to the "Function name". In this procedure, "DummyTemperatureSensor" is used.
 * 3) Select "Python 3.8" as the Runtime.
 * 4) Select "arm64" as the Architecture.
 * 5) Other items keep the default values and press "Create Function".
 * 6) When the screen changes, from the right side of "Code source", select "Upload from" → ".zip file", and upload → save the zip file you created.
 * 7) Select "Actions" → "Publish new version" in the upper right corner and press Publish.
 * 8) <Web> Deploying the Lambda function
 * Deploy the registered Lambda function to the terminal.
 * 1) Select "IoT Core" from AWS services.
 * 2) Select "Greengrass" → "Components" from the side menu.
 * 3) Click on "Create component".
 * Aws-lambda-function-deploy-1.png
 * 1) Check the "Import Lambda function" box.
 * 2) Select the function that you created in "Lambda function".
 * Aws-lambda-function-deploy-2.png
 * 1) Enter "topic/sensor/get" in the "Topic" field of the Event sources.
 * 2) Select "AWS IoT Core MQTT" as the "Type" of the Event sources.
 * Aws-lambda-function-deploy-3.png
 * 1) Set the "Memory size" of Container parameters to "64 MB".
 * Aws-lambda-function-deploy-4.png
 * 1) Other items keep the default values and press "Create component".
 * 2) When the screen changes, press "Deploy" in the upper right corner.
 * 3) Select the group you created for the deployment destination and click "Next".
 * 4) Other items keep the default values and press  "Next" several times, and press "Deploy" at the last confirmation screen.
 * 5) <WEB> <Terminal> Operation check
 * Confirm that the function you created will be deployed on the terminal side.
 * Also, check the log on the terminal side to see if messages are being received by AWS IoT → terminal.
 * 1) <Terminal> Checking the operation of R-Car SK
 * 2) Confirm that the Lambda function has been deployed.
 * Execute the following command to confirm.
 * When you execute the command the following log will be output, and you can see that the deployed function (DummyTemperatureSensor) is displayed in the list.
 * 1) Check the log.
 * Run the following command to check the log.
 * Verify that the following log is output.
 * 1) <Web><Terminal> Checking the operation of the AWS cloud
 * 2) <Web> Select "IoT Core" from AWS services.
 * 3) <Web> Select "Test" → "MQTT Test Client" from the side menu.
 * 4) <Web> Enter "topic/sensor/temperature" in the Topic filter and press "SUBSCRIBE".
 * 5) <Web> Enter "topic/sensor/get" in the "Topic name" of "Publish to a topic" and click "Publish.
 * The default message payload will be garbled on the terminal side, so change it to any alphanumeric text if necessary.
 * 1) <Terminal> The message received will be displayed in the log as follows.
 * 2) <Web> A message is received in the subscription.
 * Aws-result1.png
 * Verify that the following log is output.
 * 1) <Web><Terminal> Checking the operation of the AWS cloud
 * 2) <Web> Select "IoT Core" from AWS services.
 * 3) <Web> Select "Test" → "MQTT Test Client" from the side menu.
 * 4) <Web> Enter "topic/sensor/temperature" in the Topic filter and press "SUBSCRIBE".
 * 5) <Web> Enter "topic/sensor/get" in the "Topic name" of "Publish to a topic" and click "Publish.
 * The default message payload will be garbled on the terminal side, so change it to any alphanumeric text if necessary.
 * 1) <Terminal> The message received will be displayed in the log as follows.
 * 2) <Web> A message is received in the subscription.
 * Aws-result1.png
 * 1) <Web> A message is received in the subscription.
 * Aws-result1.png
 * Aws-result1.png