本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用自定义容器进行分析
本节包括有关如何使用 Jupyter 笔记本构建 Docker 容器的信息。如果重复使用第三方构建的笔记本,则存在安全风险:包含的容器可能使用您的用户权限执行任意代码。此外,笔记本生成的 HTML 可以显示在Amazon IoT Analytics控制台,在显示 HTML 的计算机上提供潜在的攻击向量。在使用之前,请确保您信任任何第三方笔记本的作者。
您可以创建自己的自定义容器并运行它Amazon IoT Analytics服务。为此,您需要设置一个 Docker 镜像并将其上传到 Amazon ECR,然后设置一个数据集来运行容器操作。本节提供一个使用 Octave 的过程示例。
本教程假定:
-
本地计算机上已安装 Octave
-
在本地计算机上设置的 Docker 帐户
-
网络 ACL 和安全组都允许 (因此可到达您的实例) 的发起 ping 的Amazon在Amazon ECR 开设账户Amazon IoT Analytics访问
步骤 1: 设置 Docker 映像
本教程需要三个主要文件。其名称和内容如下:
-
Dockerfile
— Docker 容器化过程的初始设置。FROM ubuntu:16.04 # Get required set of software RUN apt-get update RUN apt-get install -y software-properties-common RUN apt-get install -y octave RUN apt-get install -y python3-pip # Get boto3 for S3 and other libraries RUN pip3 install --upgrade pip RUN pip3 install boto3 RUN pip3 install urllib3 # Move scripts over ADD moment moment ADD run-octave.py run-octave.py # Start python script ENTRYPOINT ["python3", "run-octave.py"]
-
run-octave.py
— 解析来自的 JSONAmazon IoT Analytics,运行 Octave 脚本并将构件上传到 Amazon S3。import boto3 import json import os import sys from urllib.parse import urlparse # Parse the JSON from IoT Analytics with open('/opt/ml/input/data/iotanalytics/params') as params_file: params = json.load(params_file) variables = params['Variables'] order = variables['order'] input_s3_bucket = variables['inputDataS3BucketName'] input_s3_key = variables['inputDataS3Key'] output_s3_uri = variables['octaveResultS3URI'] local_input_filename = "input.txt" local_output_filename = "output.mat" # Pull input data from S3... s3 = boto3.resource('s3') s3.Bucket(input_s3_bucket).download_file(input_s3_key, local_input_filename) # Run Octave Script os.system("octave moment {} {} {}".format(local_input_filename, local_output_filename, order)) # # Upload the artifacts to S3 output_s3_url = urlparse(output_s3_uri) output_s3_bucket = output_s3_url.netloc output_s3_key = output_s3_url.path[1:] s3.Object(output_s3_bucket, output_s3_key).put(Body=open(local_output_filename, 'rb'), ACL='bucket-owner-full-control')
-
moment
— 一个简单的 Octave 脚本,它根据输入或输出文件和指定的顺序计算力矩。#!/usr/bin/octave -qf arg_list = argv (); input_filename = arg_list{1}; output_filename = arg_list{2}; order = str2num(arg_list{3}); [D,delimiterOut]=importdata(input_filename) M = moment(D, order) save(output_filename,'M')
-
下载每个文件的内容。创建一个新目录并将所有文件放入其中,然后
cd
到那个目录。 -
运行以下命令。
docker build -t octave-moment .
-
你应该在 Docker 存储库中看到一个新镜像。运行以下命令对其进行验证。
docker image ls | grep octave-moment
步骤 2: 将 Docker 镜像上载到 Amazon ECR 存储库
-
在亚马逊 ECR 中创建存储库。
aws ecr create-repository --repository-name octave-moment
-
登录您的 Docker 环境。
aws ecr get-login
-
复制输出并运行它。输出应与以下内容类似。
docker login -u AWS -p
password
-e none https://your-aws-account-id
.dkr.ecr..amazonaws.com -
使用 Amazon ECR 存储库标签标记您创建的映像。
docker tag
your-image-id
your-aws-account-id
.dkr.ecr.region
.amazonaws.com/octave-moment -
将映像推送到 Amazon ECR
docker push
your-aws-account-id
.dkr.ecr.region
.amazonaws.com/octave-moment
步骤 3: 将示例数据上载到 Simple StoragAmazon S3 存储桶
-
将以下内容下载到文件
input.txt
.0.857549 -0.987565 -0.467288 -0.252233 -2.298007 0.030077 -1.243324 -0.692745 0.563276 0.772901 -0.508862 -0.404303 -1.363477 -1.812281 -0.296744 -0.203897 0.746533 0.048276 0.075284 0.125395 0.829358 1.246402 -1.310275 -2.737117 0.024629 1.206120 0.895101 1.075549 1.897416 1.383577
-
创建名为的 Amazon S3 存储桶
octave-sample-data-
.your-aws-account-id
-
上传文件
input.txt
存储到您刚刚创建的 Amazon S3 存储桶。现在,您应该有了一个名为的存储桶octave-sample-data-
其中包含your-aws-account-id
input.txt
文件。
步骤 4: 创建容器执行角色
-
将以下内容复制到名为的文件中
role1.json
. Replace(替换)your-aws-account-id
用你的Amazon账户 ID 和aws-region
用Amazon您所在区域Amazon资源。注意
此示例包括一个全局条件上下文密钥,用于防止混淆代理安全问题。有关更多信息,请参阅 跨服务混淆代理问题防范。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "sagemaker.amazonaws.com", "iotanalytics.amazonaws.com" ] }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "aws:SourceAccount": "
your-aws-account-id
" }, "ArnLike": { "aws:SourceArn": "arn:aws:iotanalytics:aws-region
:your-aws-account-id
:dataset/DOC-EXAMPLE-DATASET
" } } ] } -
创建一个向其授予访问权限的角色 SageMaker 和Amazon IoT Analytics,使用文件
role1.json
您下载的。aws iam create-role --role-name container-execution-role --assume-role-policy-document file://role1.json
-
将以下内容下载到名为的文件中
policy1.json
并更换
使用您的账户 ID(参见下面的第二个 ARN)your-account-id
Statement:Resource
)。{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetBucketLocation", "s3:PutObject", "s3:GetObject", "s3:PutObjectAcl" ], "Resource": [ "arn:aws:s3:::*-dataset-*/*", "arn:aws:s3:::octave-sample-data-
your-account-id
/*" }, { "Effect": "Allow", "Action": [ "iotanalytics:*" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ecr:GetAuthorizationToken", "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability", "logs:CreateLogGroup", "logs:CreateLogStream", "logs:DescribeLogStreams", "logs:GetLogEvents", "logs:PutLogEvents" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "s3:GetBucketLocation", "s3:ListBucket", "s3:ListAllMyBuckets" ], "Resource" : "*" } ] } -
创建 IAM policy
policy.json
你刚刚下载的文件。aws iam create-policy --policy-name ContainerExecutionPolicy --policy-document file://policy1.json
-
将 策略附加到该角色。
aws iam attach-role-policy --role-name container-execution-role --policy-arn arn:aws:iam::
your-account-id
:policy/ContainerExecutionPolicy
步骤 5:使用容器操作创建数据集
-
将以下内容下载到名为的文件中
cli-input.json
并替换所有实例
和your-account-id
使用适当的值。region
{ "datasetName": "octave_dataset", "actions": [ { "actionName": "octave", "containerAction": { "image": "
your-account-id
.dkr.ecr.region
.amazonaws.com/octave-moment", "executionRoleArn": "arn:aws:iam::your-account-id
:role/container-execution-role", "resourceConfiguration": { "computeType": "ACU_1", "volumeSizeInGB": 1 }, "variables": [ { "name": "octaveResultS3URI", "outputFileUriValue": { "fileName": "output.mat" } }, { "name": "inputDataS3BucketName", "stringValue": "octave-sample-data-your-account-id
" }, { "name": "inputDataS3Key", "stringValue": "input.txt" }, { "name": "order", "stringValue": "3" } ] } } ] } -
使用文件创建数据集
cli-input.json
你刚刚下载并编辑。aws iotanalytics create-dataset —cli-input-json file://cli-input.json
步骤 6:调用数据集内容生成
-
运行以下命令。
aws iotanalytics create-dataset-content --dataset-name octave-dataset
步骤 7:获取数据集内容
-
运行以下命令。
aws iotanalytics get-dataset-content --dataset-name octave-dataset --version-id \$LATEST
-
您可能需要等待几分钟,直到
DatasetContentState
是SUCCEEDED
.
步骤 8:在 Octave 上打印输出
-
通过运行以下命令,使用 Octave shell 打印容器的输出。
bash> octave octave> load output.mat octave> disp(M) -0.016393 -0.098061 0.380311 -0.564377 -1.318744