注册并验证您的设备实例集 - 亚马逊 SageMaker AI
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

注册并验证您的设备实例集

在本节中,您将创建您的Amazon IoT事物对象,创建设备队列,注册您的设备队列使其可以与云交互,创建 X.509 证书来验证您的设备,将角色别名与Amazon IoT您创建队列时生成的角色别名关联,为证书提供者获取Amazon账户特定的终端节点,获取官方的 Amazon 根 CA 文件,并将亚马逊 CA 文件上传到 Amazon S3。Amazon IoT Core

  1. 创造Amazon IoT东西。

    SageMaker Edge Manager 利用这些Amazon IoT Core服务来促进边缘设备和Amazon云端点之间的连接。将设备设置为与 Edge Manager 配合使用后,您可以利用现有Amazon IoT功能。

    要将设备连接到Amazon IoT,您需要创建Amazon IoT事物对象、创建客户端证书并将其注册到Amazon物联网,以及为设备创建和配置 IAM 角色。

    首先,使用您之前使用 Boto3 创建的Amazon IoT客户端 (iot_client) 创建Amazon IoT事物对象。以下示例显示了如何创建两个事物对象:

    iot_thing_name = 'sample-device' iot_thing_type = 'getting-started-demo' iot_client.create_thing_type( thingTypeName=iot_thing_type ) # Create anAmazonIoT thing objects iot_client.create_thing( thingName=iot_thing_name, thingTypeName=iot_thing_type )
  2. 创建您的设备队列。

    使用上一步中定义的 A SageMaker I 客户端对象创建设备队列。您还可以使用 SageMaker AI 控制台创建设备队列。

    import time device_fleet_name="demo-device-fleet" + str(time.time()).split('.')[0] device_name="sagemaker-edge-demo-device" + str(time.time()).split('.')[0]

    指定您的 IoT 角色 ARN。这允许Amazon IoT向设备授予临时证书。

    device_model_directory='device_output' s3_device_fleet_output = 's3://{}/{}'.format(bucket, device_model_directory) sagemaker_client.create_device_fleet( DeviceFleetName=device_fleet_name, RoleArn=iot_role_arn, # IoT Role ARN specified in previous step OutputConfig={ 'S3OutputLocation': s3_device_fleet_output } )

    Amazon IoT角色别名是在创建设备队列时创建的。此角色别名与在后面的步骤中Amazon IoT使用该iot_client对象相关联。

  3. 注册您的设备队列。

    要与云端交互,您需要在 SageMaker 边缘管理器中注册您的设备。在此示例中,您在已创建的队列中注册单个设备。要注册设备,您需要提供设备名称和 Amazon IoT 事物名称,如以下示例所示:

    # Device name should be 36 characters device_name = "sagemaker-edge-demo-device" + str(time.time()).split('.')[0] sagemaker_client.register_devices( DeviceFleetName=device_fleet_name, Devices=[ { "DeviceName": device_name, "IotThingName": iot_thing_name } ] )
  4. 创建 X.509 证书。

    创建Amazon IoT事物对象后,必须为事物对象创建 X.509 设备证书。此证书用于向 Amazon IoT Core 验证设备身份。

    使用以下命令使用之前定义的Amazon IoT客户端 () iot_client 创建私钥、公钥和 X.509 证书文件。

    # Creates a 2048-bit RSA key pair and issues an X.509 # certificate # using the issued public key. create_cert = iot_client.create_keys_and_certificate( setAsActive=True ) # Get certificate from dictionary object and save in its own with open('./device.pem.crt', 'w') as f: for line in create_cert['certificatePem'].split('\n'): f.write(line) f.write('\n') # Get private key from dictionary object and save in its own with open('./private.pem.key', 'w') as f: for line in create_cert['keyPair']['PrivateKey'].split('\n'): f.write(line) f.write('\n') # Get a private key from dictionary object and save in its own with open('./public.pem.key', 'w') as f: for line in create_cert['keyPair']['PublicKey'].split('\n'): f.write(line) f.write('\n')
  5. 将角色别名与关联Amazon IoT。

    使用 SageMaker AI (sagemaker_client.create_device_fleet()) 创建设备队列时,系统会为您生成角色别名。Amazon IoT角色别名提供了一种机制,让连接的设备Amazon IoT使用 X.509 证书进行身份验证,然后从与角色别名关联的 IAM 角色获取短期Amazon证书。Amazon IoT使用角色别名,您可以更改角色,无需更新设备。使用 DescribeDeviceFleet 获取角色别名和 ARN。

    # Print Amazon Resource Name (ARN) and alias that has access # toAmazonInternet of Things (IoT). sagemaker_client.describe_device_fleet(DeviceFleetName=device_fleet_name) # Store iot role alias string in a variable # Grabs role ARN full_role_alias_name = sagemaker_client.describe_device_fleet(DeviceFleetName=device_fleet_name)['IotRoleAlias'] start_index = full_role_alias_name.find('SageMaker') # Find beginning of role name role_alias_name = full_role_alias_name[start_index:]

    使用iot_client可以方便地将创建设备队列时生成的角色别名与Amazon IoT以下角色相关联:

    role_alias = iot_client.describe_role_alias( roleAlias=role_alias_name)

    有关 IAM 角色别名的更多信息,请参阅角色别名允许访问未使用的服务

    您Amazon IoT之前创建并注册了证书,以便成功对设备进行身份验证。现在,您需要创建策略并将其附加到证书,以授权对安全令牌的请求。

    alias_policy = { "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": "iot:AssumeRoleWithCertificate", "Resource": role_alias['roleAliasDescription']['roleAliasArn'] } } policy_name = 'aliaspolicy-'+ str(time.time()).split('.')[0] aliaspolicy = iot_client.create_policy(policyName=policy_name, policyDocument=json.dumps(alias_policy)) # Attach policy iot_client.attach_policy(policyName=policy_name, target=create_cert['certificateArn'])
  6. 获取凭证提供商的Amazon账户专用端点。

    边缘设备需要端点才能代入凭证。获取凭证提供程序的 Amazon 账户特定端点。

    # Get the unique endpoint specific to yourAmazonaccount that is making the call. iot_endpoint = iot_client.describe_endpoint( endpointType='iot:CredentialProvider' ) endpoint="https://{}/role-aliases/{}/credentials".format(iot_endpoint['endpointAddress'],role_alias_name)
  7. 获取官方的 Amazon 根 CA 文件并将其上传到 Amazon S3 存储桶。

    在 Jupyter 笔记本中使用以下内容或Amazon CLI(如果您使用终端,请删除 '!' 魔法函数):

    !wget https://www.amazontrust.com/repository/AmazonRootCA1.pem

    使用端点向凭证提供程序发出 HTTPS 请求以返回安全令牌。以下示例命令使用 curl,但您可以使用任何 HTTP 客户端。

    !curl --cert device.pem.crt --key private.pem.key --cacert AmazonRootCA1.pem $endpoint

    如果证书已通过验证,请将密钥和证书上传到您的 Amazon S3 存储桶 URI:

    !aws s3 cp private.pem.key s3://{bucket}/authorization-files/ !aws s3 cp device.pem.crt s3://{bucket}/authorization-files/ !aws s3 cp AmazonRootCA1.pem s3://{bucket}/authorization-files/

    通过将密钥和证书移至其他目录来清理工作目录:

    # Optional - Clean up working directory !mkdir authorization-files !mv private.pem.key device.pem.crt AmazonRootCA1.pem authorization-files/