R-Car/AWS IoT Greengrass

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

This page contains information abot running AWS IoT Greengrass on Ubuntu environment on:
 * Renesas R-Car S4 Reference Board/Spider
 * Renesas R-Car-S4
 * AWS IoT Greengrass

Start Ubuntu on R-car S4
In subsequent chapters, the tags will be included in the title as follows.
 * Work on the target board (R-Car S4) is described as .
 * Work on the host PC is described as .
 * 1)  Install software.
 * 2)  Make directory.
 * 3)  Change directory.
 * 4)  Copy tools>renesas-rcar-s4>make-rootfs.sh from https://github.com/sparkgene/aws-iot-fleetwise-edge/tree/fcc9bc471eee520a31d3805c628c236f69aae056 to Host PC.
 * 5)  Give authority.
 * 6)  Execute.
 * 7)  Setup TFTP server and NFS server.
 * For more details, please refer to section "Setup TFTP server" and "Setup nfs-kernel-server" in "R-Car S4_StartupGuide_x_x_x.pdf"
 * 1)  Boot NFS.
 * 2)  Copy Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 from current directory to /work/export/.
 * 3)  Flash Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4.
 * 4)  Synchronize.
 * 5)  Reboot S4.
 * Power off and Power on SW1.
 * 1)  Boot eMMC.
 * 1)  Copy Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4 from current directory to /work/export/.
 * 2) <Terminal> Flash Ubuntu-20.04.5-rootfs-image-rcar-spider.ext4.
 * 3) <Terminal> Synchronize.
 * 4) <Terminal> Reboot S4.
 * Power off and Power on SW1.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Reboot S4.
 * Power off and Power on SW1.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Boot eMMC.
 * 1) <Terminal> Boot eMMC.

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 S4) is described as <Terminal>.
 * Work on the AWS Cloud is described as <Web>.
 * Work on the host PC is described as <PC>.
 * 1) <Web> 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) <Web> 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
 * Note: 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) <Web> 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) <Terminal> Expand partition on R-Car S4.
 * 2) <Terminal> Download AWS IoT Greengrass Core Software.
 * 3) <Terminal> Unzip AWS IoT Greengrass Core Software.
 * 4) <Terminal> Install Java.
 * 5) <Terminal> Confirm AWS IoT Greengrass Core Software’s version.
 * 6) <Terminal> Setup environment value and directory.
 * 7) <Terminal> Install AWS IoT Greengrass Core Software.
 * 8) <Terminal> Wait a minute, confirm version and component list.
 * 9) <Terminal> Confirm greengrass.service status.
 * 10) <Terminal> If greengrass.service doesn’t start, you need to start it.
 * 11) Attach policy.
 * 12) <Web> Select "IAM" from AWS services.
 * 13) <Web> Select "Policies" from the side menu.
 * 14) <Web> Select "Create Policy".
 * 15) <Web> Select "IoT" for Service, "Publish" for Actions, and "All Resources" for Resources.
 * 16) <Web> Click "Next: Tags" and "Next: Review", then enter any "Name" in the Review policy, and click "Create Policy" button.
 * In this procedure, the name shall be "R-CarS4Device-IoT-Publish".
 * 1) <Web> Return to the IAM screen and select "Roles" from the side menu.
 * 2) <Web> Enter "GreengrassV2TokenExchangeRole" in the search and select the role with the same name.
 * 3) <Web> Click "Attach Policies", search for the policy you just created(Account name), and click "Attach Policy".
 * 4) Prepare the execution environment for Lambda functions.
 * Build an environment to run the sample program on a terminal.
 * <Terminal> Execute the following commands.
 * 1) The endpoints can be found below.
 * 2) <Web> Select "IoT Core" from AWS services.
 * 3) <Web> Select "Settings" from the side menu.
 * 4) <Web> Refer to "Endpoint" in Device data endpoint, take notes.
 * xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com
 * 1) Create a Lambda Function.
 * Create a Lambda function to deploy to the terminal.
 * <PC> Run the following command to create a template for a Lambda function.
 * 1) <PC> Copy and paste the following code into the lambda_function.py that you created.
 * Rewrite the endpoint_url to xxxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com that you wrote down.
 * 1) Zip the packages.
 * <PC> 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) Register the Lambda function.
 * Register the created Lambda function to the AWS cloud.
 * 1) <Web> Select "Lambda" from AWS services.
 * 2) <Web> Select "Functions" from the side menu.
 * 3) <Web> Select "Create function".
 * 4) <Web> Check the "Author from scratch" box.
 * 5) <Web> Give an arbitrary name to the "Function name". In this procedure, "DummyTemperatureSensor" is used.
 * 6) <Web> Select "Python 3.8" as the Runtime.
 * 7) <Web> Select "arm64" as the Architecture.
 * 8) <Web> Other items keep the default values and click "Create Function".
 * 9) <Web> When the screen changes, Move Configuration tab and Timeout change 1 min 0 sec.
 * 10) <Web> Move Code tab, select "Upload from" → ".zip file", and upload → click "Save".
 * 11) <Web> Select "Actions" → "Publish new version" in the upper right corner and click “Publish”.
 * 12) Deploy the Lambda function.
 * Deploy the registered Lambda function to the terminal.
 * 1) <Web> Select "IoT Core" from AWS services.
 * 2) <Web> Select "Greengrass devices" → "Components" from the side menu.
 * 3) <Web> Click "Create component".
 * 4) <Web> Check the "Import Lambda function" box and select the function that you created in "Lambda function", enter "topic/sensor/get" in the Topic field of the Event sources, select "AWS IoT Core MQTT" as the Type of the Event sources, enter "60" in the Timeout field of the Event sources.
 * 5) <Web> Set the Memory size of Container parameters to "64 MB".
 * 6) <Web> Other items keep the default values and click "Create component".
 * 7) <Web> When the screen changes, click "Deploy" in the upper right corner.
 * 8) <Web> Select “Add create”, and click your deployment and "Next".
 * 9) <Web> Other items keep the default values and click "Next" several times, and click "Deploy" at the last confirmation screen.
 * 10) 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.
 * Check the operation of R-Car SK.
 * Confirm that the Lambda function has been deployed.
 * <Terminal> 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.
 * <Terminal> Run the following command to check the log.
 * Verify that the following log is output.
 * serviceName=, currentState=RUNNING
 * 1) Checking the operation of the AWS cloud.
 * 2) <Web> Select "IoT Core" from AWS services.
 * 3) <Web> Select "MQTT Test Client" from the side menu.
 * 4) <Web> Enter "topic/sensor/temperature" in the Topic filter and click "Subscribe".
 * 5) <Web> Enter "topic/sensor/get" in the Topic name of Publish to a topic and click "Publish.
 * Note: 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.
 * <Terminal> 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.
 * <Terminal> Run the following command to check the log.
 * Verify that the following log is output.
 * serviceName=, currentState=RUNNING
 * 1) Checking the operation of the AWS cloud.
 * 2) <Web> Select "IoT Core" from AWS services.
 * 3) <Web> Select "MQTT Test Client" from the side menu.
 * 4) <Web> Enter "topic/sensor/temperature" in the Topic filter and click "Subscribe".
 * 5) <Web> Enter "topic/sensor/get" in the Topic name of Publish to a topic and click "Publish.
 * Note: 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.
 * Note: 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.
 * 1) <Web> A message is received in the subscription.