デバイスフリートを登録して認証する - Amazon SageMaker AI

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

デバイスフリートを登録して認証する

このセクションでは、 AWS IoT モノのオブジェクトの作成、デバイスフリートの作成、デバイスフリートの登録を行い、クラウドとやり取りできるようにします。また、デバイスを認証する X.509 証明書の作成 AWS IoT Core、フリートの作成時に AWS IoT 生成されたロールエイリアスとの関連付け、認証情報プロバイダーの AWS アカウント固有のエンドポイントの取得、公式の Amazon ルート CA ファイルの取得、Amazon CA ファイルの Amazon S3 へのアップロードを行います。

  1. AWS IoT モノを作成します。

    SageMaker Edge Manager は、 AWS IoT Core サービスを活用して、 AWS クラウド内のエッジデバイスとエンドポイント間の接続を容易にします。Edge Manager と連携するようにデバイスをセットアップした後、既存の AWS IoT 機能を活用できます。

    デバイスを に接続するには AWS IoT、 AWS IoT オブジェクトを作成し、クライアント証明書を作成して AWS IoT に登録し、デバイスの IAM ロールを作成して設定する必要があります。

    まず、Boto3 で前に作成した AWS IoT クライアント (iot_client) を使用して AWS IoT モノのオブジェクトを作成します。以下の例では、2 つのモノオブジェクトを作成する方法を示しています。

    iot_thing_name = 'sample-device' iot_thing_type = 'getting-started-demo' iot_client.create_thing_type( thingTypeName=iot_thing_type ) # Create an AWS IoT thing objects iot_client.create_thing( thingName=iot_thing_name, thingTypeName=iot_thing_type )
  2. デバイスフリートを作成する。

    前のステップで定義した SageMaker AI クライアントオブジェクトを使用してデバイスフリートを作成します。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 を指定します。これにより、 はデバイスに一時的な認証情報 AWS 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 } )

    AWS IoT ロールエイリアスは、デバイスフリートを作成するときに作成されます。このロールエイリアスは、後のステップで iot_client オブジェクト AWS IoT を使用することに関連付けられます。

  3. デバイスフリートを登録する。

    クラウドと通信するには、SageMaker Edge Manager を使用してデバイスを登録する必要があります。この例では、作成したフリートにデバイスを 1 つ登録します。デバイスを登録するには、次の例に示すように、デバイス名と AWS 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 証明書を作成する。

    AWS IoT モノのオブジェクトを作成したら、モノのオブジェクトの X.509 デバイス証明書を作成する必要があります。証明書により、 AWS IoT Coreにデバイスを認証できます。

    以下を使用して、前に定義した AWS 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. ロールエイリアスを関連付けます AWS IoT。

    SageMaker AI (sagemaker_client.create_device_fleet()) を使用してデバイスフリートを作成すると、ロールエイリアスが生成されます。 AWS IoT ロールエイリアスは、接続されたデバイスが X.509 証明書 AWS IoT を使用して を認証し、ロールエイリアスに関連付けられた AWS IoT IAM ロールから有効期間の短い AWS 認証情報を取得するメカニズムを提供します。ロールエイリアスを使用することで、デバイスを更新しなくてもロールを変更できます。DescribeDeviceFleet を使用して、ロールのエイリアス名と ARN を取得します。

    # Print Amazon Resource Name (ARN) and alias that has access # to AWS Internet 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、デバイスフリートの作成によって生成されたロールエイリアスの関連付けを容易にします AWS IoT。

    role_alias = iot_client.describe_role_alias( roleAlias=role_alias_name)

    IAM ロールエイリアスの詳細については、「ロールエイリアスが未使用サービスへのアクセスを許可します」を参照してください。

    デバイスの認証を成功させるために、 AWS 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. 認証情報プロバイダーの AWS アカウント固有のエンドポイントを取得します。

    エッジデバイスには、認証情報を継承するエンドポイントが必要です。認証情報プロバイダー向けの AWS アカウント固有のエンドポイントを取得します。

    # Get the unique endpoint specific to your AWS account 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 Notebook で以下を使用するか AWS 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/