Step 3: Publish the component to the Amazon IoT Greengrass service - Amazon IoT Greengrass
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Step 3: Publish the component to the Amazon IoT Greengrass service

In this section, you publish the Hello World component to the Amazon IoT Greengrass cloud service. After a component is available in the Amazon IoT Greengrass cloud service, you can deploy it to core devices. You use the GDK CLI to publish the component from your development computer to the Amazon IoT Greengrass cloud service. The GDK CLI uploads the component's recipe and artifacts for you.

To publish the Hello World component to the Amazon IoT Greengrass service
  1. Run the following command to build the component using the GDK CLI. The component build command creates a recipe and artifacts based on the GDK CLI configuration file. In this process, the GDK CLI creates a ZIP file that contains the component's source code.

    gdk component build

    You should see messages similar to the following example.

    [2022-04-28 11:20:16] INFO - Getting project configuration from gdk-config.json [2022-04-28 11:20:16] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2022-04-28 11:20:16] INFO - Building the component 'com.example.BatteryAwareHelloWorld' with the given project configuration. [2022-04-28 11:20:16] INFO - Using 'zip' build system to build the component. [2022-04-28 11:20:16] WARNING - This component is identified as using 'zip' build system. If this is incorrect, please exit and specify custom build command in the 'gdk-config.json'. [2022-04-28 11:20:16] INFO - Zipping source code files of the component. [2022-04-28 11:20:16] INFO - Copying over the build artifacts to the greengrass component artifacts build folder. [2022-04-28 11:20:16] INFO - Updating artifact URIs in the recipe. [2022-04-28 11:20:16] INFO - Creating component recipe in 'C:\Users\finthomp\greengrassv2\com.example.BatteryAwareHelloWorld\greengrass-build\recipes'.
  2. Run the following command to publish the component to the Amazon IoT Greengrass cloud service. The component publish command uploads the component's ZIP file artifact to an S3 bucket. Then, it updates the ZIP file's S3 URI in the component recipe and uploads the recipe to the Amazon IoT Greengrass service. In this process, the GDK CLI checks what version of the Hello World component is already available in the Amazon IoT Greengrass cloud service, so it can choose the next patch version after that version. If the component doesn't exist yet, the GDK CLI uses version 1.0.0.

    gdk component publish

    You should see messages similar to the following example. The output tells you the version of the component that the GDK CLI created.

    [2022-04-28 11:20:29] INFO - Getting project configuration from gdk-config.json [2022-04-28 11:20:29] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2022-04-28 11:20:29] INFO - Found credentials in shared credentials file: ~/.aws/credentials [2022-04-28 11:20:30] INFO - No private version of the component 'com.example.BatteryAwareHelloWorld' exist in the account. Using '1.0.0' as the next version to create. [2022-04-28 11:20:30] INFO - Publishing the component 'com.example.BatteryAwareHelloWorld' with the given project configuration. [2022-04-28 11:20:30] INFO - Uploading the component built artifacts to s3 bucket. [2022-04-28 11:20:30] INFO - Uploading component artifacts to S3 bucket: greengrass-component-artifacts-us-west-2-123456789012. If this is your first time using this bucket, add the 's3:GetObject' permission to each core device's token exchange role to allow it to download the component artifacts. For more information, see https://docs.aws.amazon.com/greengrass/v2/developerguide/device-service-role.html. [2022-04-28 11:20:30] INFO - Not creating an artifacts bucket as it already exists. [2022-04-28 11:20:30] INFO - Updating the component recipe com.example.BatteryAwareHelloWorld-1.0.0. [2022-04-28 11:20:31] INFO - Creating a new greengrass component com.example.BatteryAwareHelloWorld-1.0.0 [2022-04-28 11:20:31] INFO - Created private version '1.0.0' of the component in the account.'com.example.BatteryAwareHelloWorld'.
  3. Copy the S3 bucket name from the output. You use the bucket name later to allow the core device to download component artifacts from this bucket.

  4. (Optional) View the component in the Amazon IoT Greengrass console to verify that it uploaded successfully. Do the following:

    1. In the Amazon IoT Greengrass console navigation menu, choose Components.

    2. On the Components page, choose the My components tab, and then choose com.example.BatteryAwareHelloWorld.

      On this page, you can see the component's recipe and other information about the component.

  5. Allow the core device to access component artifacts in the S3 bucket.

    Each core device has a core device IAM role that allows it to interact with Amazon IoT and send logs to the Amazon Cloud. This device role doesn't allow access to S3 buckets by default, so you must create and attach a policy that allows the core device to retrieve component artifacts from the S3 bucket.

    If your device's role already allows access to the S3 bucket, you can skip this step. Otherwise, create an IAM policy that allows access and attach it to the role, as follows:

    1. In the IAM console navigation menu, choose Policies, and then choose Create policy.

    2. On the JSON tab, replace the placeholder content with the following policy. Replace greengrass-component-artifacts-us-west-2-123456789012 with the name of the S3 bucket where the GDK CLI uploaded the component's artifacts.

      For example, if you specified greengrass-component-artifacts and us-west-2 in the GDK CLI configuration file, and your Amazon Web Services account ID is 123456789012, the GDK CLI uses the S3 bucket named greengrass-component-artifacts-us-west-2-123456789012.

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws-cn:s3:::greengrass-component-artifacts-us-west-2-123456789012/*" } ] }
    3. Choose Next.

    4. In the Policy details section, for Name, enter MyGreengrassV2ComponentArtifactPolicy.

    5. Choose Create policy.

    6. In the IAM console navigation menu, choose Role, and then choose the name of the role for the core device. You specified this role name when you installed the Amazon IoT Greengrass Core software. If you did not specify a name, the default is GreengrassV2TokenExchangeRole.

    7. Under Permissions, choose Add permissions, then choose Attach policies.

    8. On the Add permissions page, select the check box next to the MyGreengrassV2ComponentArtifactPolicy policy that you created, and then choose Add permissions.