Show Contents

# 4. Set up a Greengrass group

An AWS Greengrass group is a collection of local devices and Lambda functions that represents your local environment. You can program your group components to communicate with each other through a Greengrass core device.

Groups contain:

- A core device with which all other devices in the group communicate.
- A list of devices that belong to the group.
- A list of Lambda functions that are deployed to the core.
- A subscription table that contains the predefined MQTT protocol-based routes that Greengrass cores use to transmit messages between devices, Lambda functions, and AWS.

From http://docs.aws.amazon.com/greengrass/latest/developerguide

To define your Greengrass group, follow these steps:

- 4.1. Create a Greengrass group
- 4.2. Create device things
- 4.3. Add devices to your group
- 4.4. Create Lambda functions
- 4.5. Add Lambda functions to your group
- 4.6. Add subscriptions to your group
- 4.7. Configure logging

## 4.1. Create a Greengrass group

Follow these steps to create a Greengrass group:

1. Go to the AWS IoT console.
2. In the navigation pane, select Greengrass and then Groups.
3. Click Create Group.
4. On the Set up your Greengrass Group page, click Customize.
5. Type a name for your Group, for example GGStoryLine.
6. Click Next.
7. On the Attach an IAM Role to your Group page, click Add role later.
8. On the Add a core to your Group page, click Use existing Thing.
9. On the Select an existing IoT Thing as a core page, look for your core name, GGC_Thing, and select it.
10. Click Create Group and core.

## 4.2. Create device things

Use the AWS IoT console to create two things:

- RobotArm_Thing to represent the robotic arm.
- Switch_Thing to represent the switch on the robotic arm.

Follow these steps to create the things, their certificates, and policies:

1. Go to the AWS IoT console.
2. Create an AWS IoT Thing for:
   - The robot arm: RobotArm_Thing
   - The switch: Switch_Thing

For more information, see Register a Device in the Thing Registry.

3. Create AWS IoT certificates and private keys for the robot arm and the switch. For more information, see Create and Activate a Device Certificate ..
4. Download the certificates and private keys and save them in a safe place. You will copy them to your robot arm and switch devices later (see 7.2 Install your thing certificates).
5. Attach the device certificates to your AWS IoT things, RobotArm_Thing and Switch_Thing. For more information, see Attach a Certificate to a Thing.
6. Make sure the certificates are activated.
7. Create and attach an AWS IoT policy to your device certificate.

An AWS IoT policy designates the AWS IoT resources your Greengrass device can access. To create and attach a policy, see Attach an AWS IoT Policy.

Use the Advanced mode to enter the following policy that allows things to automatically sync their shadow status online and use discovery functionality.

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "iot:*",
            "greengrass:*"
          ],
          "Resource": [
            "*"
          ]
        }
      ]
    }

## 4.3. Add devices to your group

Once the robot arm and the switch things are registered, add them to your Greengrass group.

1. Go to the AWS IoT console.
2. In the navigation pane, select Greengrass and then Groups.
3. Click on your group, GGStoryLine.
4. In the navigation pane, click Devices and then Add Device.
5. Click Select an IoT Thing.
6. Select the RobotArm_Thing you created at 4.2. Create device things.
7. Click Finish.
8. Repeat steps 4 to 7 for Switch_Thing.
9. Configure the RobotArm_Thing. Click the ellipsis on the top right of the RobotArm_Thing box and select Sync to the Cloud.

## 4.4. Create Lambda functions

Greengrass cores can run Lambda functions in response to messages sent by your devices or other Lambda functions. Create two Lambda functions:

- uptime to monitor the robot arm state when it is turned on and off.
- message to convert generic messages that control the robot arm from the cloud and into messages that our system can use with the local Shadows Service.

Later, you will add these Lambda functions to your Greengrass group so you can deploy them to your Greengrass core.

Follow these steps to create two Lambda functions with the data in the following table:

Name | Zip file | Handler | Alias name
--- | --- | --- | ---
uptimeLambda | storyline_uptimeLambda.zip | uptimeLambda.uptime_handler | storyLineUptime
messageLambda | storyline_messageLambda.zip | messageLambda.message_handler | storyLineMessage

1. Download the AWS Greengrass Core SDK from your account.
   1. Go to the AWS IoT console.
   2. Click Software in the navigation bar.

   3. Inside SDKs > AWS Greengrass Core SDK, click Configure download.

   4. Select Python 2.7 version 1.0.0 in the combo box and click Download Greengrass Core SDK.

   5. Decompress the greengrass-core-python-sdk-<version>.tar.gz.
2. Use the Service drop down menu to navigate to the AWS Lambda console.

Note Use the same region for Greengrass and Lambda console. If you are accessing Greengrass in the us-west-2 region, use the Lambda console in the us-west-2 region.

3. In the Lambda console, click Create function.

4. In the Name field, type the name of the Lambda function you are creating. See the table.

5. In the Runtime drop down menu, select Python 2.7.

6. In the Role drop down, select Choose an existing role. In the Existing role drop down, select any role. The role is not important because AWS Greengrass does not use it. If you do not have a role, choose Create a custom role and follow the steps.

7. Click Create function.

8. In the Function code section, select Upload a .ZIP file in the Code entry type drop down.

9. Click Upload and select the corresponding zip file located at examples/Storyline inside the greengrass-core-python-sdk-<version>.tar.gz tarball.

10. In the Handler field, type the handler for your Lambda function.

11. Review the Lambda settings and click Save button at the top right.

12. Publish a new version of the Lambda function from Actions > Publish new version.

13. Click Publish in the Publish new version dialog. AWS Lambda creates a new version and assigns an increasing sequence number to it, 1 for the first version.

14. Associate an alias with this published version from Actions > Create alias.

15. In the Name field, type the corresponding alias name.

16. In the Version menu, select the version that was just published. In this case, select 1.

17. Click Create.

## 4.5. Add Lambda functions to your group

Once the Lambda functions are created, add them to your Greengrass group.

1. Go to the AWS IoT console.
2. In the navigation pane, select Greengrass and then Groups.
3. Click on your group, GGStoryLine.
4. In the navigation pane, click Lambdas and then Add Lambda.
5. Click Use existing Lambda.
6. Select the uptimeLambda you created at 4.4. Create Lambda functions.
7. Click Next.
8. Select the alias of for this function, Alias: storyLineUptime.
9. Click Finish.
10. Repeat steps 4 to 9 for messageLambda function.

## 4.6. Add subscriptions to your group

Subscriptions are the predefined MQTT protocol-based routes that Greengrass cores use to transmit messages between devices, Lambda functions, and AWS. A subscription consists of:

- A source. It is an ARN the originator of the message. It can be the ARN of a thing, a Lambda function, or the cloud.
- A target. It is an ARN that identifies the destination of the message. It can be the ARN of a thing, a Lambda function, or the cloud.
- A topic. It allows you to filter the data that is sent from the source to the target.

A message will only be sent if there is a defined subscription for the source and the target.

For this example, create the following subscriptions:

| Source | Target | Topic
--- | --- | --- | ---
1 | Switch_Thing | Local Shadow Service | $aws/things/RobotArm_Thing/shadow/update
2 | Local Shadow Service | RobotArm_Thing | $aws/things/RobotArm_Thing/shadow/update/accepted
3 | Local Shadow Service | RobotArm_Thing | $aws/things/RobotArm_Thing/shadow/update/rejected
4 | RobotArm_Thing | uptimeLambda | /topic/state
5 | RobotArm_Thing | Local Shadow Service | $aws/things/RobotArm_Thing/shadow/update
6 | Local Shadow Service | RobotArm_Thing | $aws/things/RobotArm_Thing/shadow/update/delta
7 | uptimeLambda | IoT Cloud | /topic/metering
8 | messageLambda | Local Shadow Service | $aws/things/RobotArm_Thing/shadow/update
9 | IoT Cloud | messageLambda | /topic/update
10 | Local Shadow Service | Switch_Thing | $aws/things/RobotArm_Thing/shadow/update/accepted
11 | Local Shadow Service | Switch_Thing | $aws/things/RobotArm_Thing/shadow/update/rejected

Repeat the following steps for each subscription in the table:

1. Go to the AWS IoT console.
2. In the navigation pane, select Greengrass and then Groups.
3. Click on your group, GGStoryLine.
4. In the navigation pane, click Subscriptions and then Add Subscription.
5. Under Select a source, select the source of the subscription.
6. Under Select a target, select the target of the subscription.
7. Click Next.
8. In the Optional topic filter, type the topic of the subscription.
9. Click Next.
10. Review the subscription information and click Finish to create it.

## 4.7. Configure logging

You can define where to store the logs generated by the core and your Lambda functions. You can store them on the file system of the Greengrass core device or on CloudWatch. In this example, you will store the logs on the Greengrass core hardware.

1. Go to the AWS IoT console.
2. In the navigation pane, select Greengrass and then Groups.
3. Click on your group, GGStoryLine.
4. In the navigation pane, click Settings.
5. Find Local logs configuration and click Edit.
6. Click Add another log type.
7. Select User Lambdas and Greengrass system as the components you would like to log.
8. Click Update.
9. Under User lambda logs section:
   1. Select Debug logs as the level.
   2. Set 5 MB as space limit.
10. Under Greengrass system logs section:
    1. Select Informational logs as the level.
    2. Set 5 MB as space limit.
11. Click Save.

© 2018 Digi International Inc. All rights reserved.
4. Set up a Greengrass group updated on 22 January 2018 02:43:38 PM