本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
註冊並驗證裝置機群
在本節中,您將建立AWS IoT物件、建立裝置機群、註冊裝置機群,以便與雲端互動、建立 X.509 憑證以驗證您的裝置AWS IoT Core、建立機群AWS IoT時產生的角色別名與 建立關聯、取得登入資料提供者AWS的帳戶特定端點、取得官方 Amazon 根 CA 檔案,以及將 Amazon CA 檔案上傳至 Amazon S3。
-
建立AWS IoT物件。
SageMaker Edge Manager 利用 AWS IoT Core服務來促進AWS雲端中邊緣裝置和端點之間的連線。您可以在設定裝置以使用 Edge Manager 之後,利用現有的AWS IoT功能。
若要將裝置連接到 AWS IoT,您需要建立AWS IoT物件、使用 AWSIoT 建立和註冊用戶端憑證,以及為裝置建立和設定 IAM 角色。
首先,使用您先前使用 Boto3 建立的AWS IoT用戶端 (
iot_client) 建立AWS IoT物件物件。下列範例說明如何建立兩個實物物件:iot_thing_name = 'sample-device' iot_thing_type = 'getting-started-demo' iot_client.create_thing_type( thingTypeName=iot_thing_type ) # Create anAWSIoT thing objects iot_client.create_thing( thingName=iot_thing_name, thingTypeName=iot_thing_type ) -
建立您的裝置機群。
使用上一步定義的 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角色別名。此角色別名與在後續步驟中使用AWS IoT
iot_client物件相關聯。 -
註冊您的裝置機群。
若要與雲端互動,您必須向 SageMaker Edge Manager 註冊您的裝置。在此範例中,您會使用建立的機群註冊單一裝置。若要註冊裝置,您需要提供裝置名稱和 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 } ] ) -
建立 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') -
將角色別名與 建立關聯AWS IoT。
當您使用 SageMaker AI (
sagemaker_client.create_device_fleet()) 建立裝置機群時,會產生角色別名。AWS IoT角色別名提供一種機制,讓連線的裝置AWS IoT使用 X.509 憑證向 驗證,然後從與AWS IoT角色別名相關聯的 IAM 角色取得短期AWS憑證。此角色別名讓您無需更新裝置即可變更裝置角色。用DescribeDeviceFleet以取得角色別名和 ARN。# Print Amazon Resource Name (ARN) and alias that has access # toAWSInternet 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']) -
取得登入資料提供者AWS的帳戶特定端點。
Edge 裝置需要端點才能擔任憑證。獲得憑證供應商的 AWS 帳戶特有端點。
# Get the unique endpoint specific to yourAWSaccount 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) -
取得官方 Amazon 根 CA 檔案並將其上傳至 Amazon S3 儲存貯體。
在您的 Jupyter 筆記本或 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/