Creating component

Creating component artifacts

  1. Execute the following command in the terminal of Cloud9 environment to create a folder of artifacts and an empty script file
mkdir -p ~/environment/GreengrassCore/artifacts/com.example.HelloWorld/1.0.0 && touch ~/environment/GreengrassCore/artifacts/com.example.HelloWorld/1.0.0/hello_world.py

The path of the artifact must always follow the following format: artifacts/componentName/componentVersion/. componentName and componentVersion are specified in the recipe created in the next step.

  1. Open the created script file hello_world.py and paste the following source code

C9

hello_world.py

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import sys
import datetime
import time

while True:
    
    message = f"Hello, {sys.argv[1]}! Current time: {str(datetime.datetime.now())}."
    
    # Print the message to stdout.
    print(message)
    
    # Append the message to the log file.
    with open('/tmp/Greengrass_HelloWorld.log', 'a') as f:
        print(message, file=f)
        
    time.sleep(1)

Make sure to save after each copy/paste in Cloud9 Editor pane.

Creating a recipe for a component

You have an option to write your recipe in either YAML or JSON, in this workshop we will use JSON.

  1. Execute the following command to create a folder to put the recipe and an empty file
mkdir -p ~/environment/GreengrassCore/recipes && touch ~/environment/GreengrassCore/recipes/com.example.HelloWorld-1.0.0.json 
  1. Open the created recipe file com.example.HelloWorld-1.0.0.json, paste the following contents and save

com.example.HelloWorld-1.0.0.json

{
   "RecipeFormatVersion": "2020-01-25",
   "ComponentName": "com.example.HelloWorld",
   "ComponentVersion": "1.0.0",
   "ComponentDescription": "My first AWS IoT Greengrass component.",
   "ComponentPublisher": "Amazon",
   "ComponentConfiguration": {
      "DefaultConfiguration": {
         "Message": "world"
      }
   },
   "Manifests": [
      {
         "Platform": {
            "os": "linux"
         },
         "Lifecycle": {
            "Run": "python3 {artifacts:path}/hello_world.py '{configuration:/Message}'\n"
         }
      }
   ]
}

If you encounter any issues during the workshop, first see if you can find your solution in Troubleshooting section