분석에 사용자 지정 컨테이너 사용 - AWS IoT Analytics

지원 종료 알림: 2025년 12월 15일에 AWS 에 대한 지원이 종료됩니다 AWS IoT Analytics. 2025년 12월 15일 이후에는 AWS IoT Analytics 콘솔 또는 AWS IoT Analytics 리소스에 더 이상 액세스할 수 없습니다. 자세한 내용은 AWS IoT Analytics 지원 종료를 참조하세요.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

분석에 사용자 지정 컨테이너 사용

이 단원에는 Jupyter Notebook 사용하여 Docker 컨테이너를 빌드하는 방법에 대한 정보가 포함되어 있습니다. 타사에서 빌드한 노트북을 다시 사용하면 보안상 위험할 수 있습니다. 포함된 컨테이너는 사용자 권한으로 임의의 코드를 실행할 수 있습니다. 또한 노트북에서 생성된 HTML을 AWS IoT Analytics 콘솔에 표시하여 HTML을 표시하는 컴퓨터에 잠재적 공격 벡터를 제공할 수 있습니다. 타사 노트북을 사용하기 전에 해당 노트북의 작성자를 신뢰할 수 있는지 확인하십시오.

자체 사용자 지정 컨테이너를 생성하고 AWS IoT Analytics 서비스와 함께 실행할 수 있습니다. 이렇게 하려면 도커 이미지를 설정해 Amazon ECR에 업로드한 다음, 컨테이너 작업을 실행할 데이터 세트를 설정합니다. 이 섹션에서는 Octave를 사용하는 프로세스에 대한 예를 제공합니다.

이 자습서는 다음을 가정합니다.

  • 로컬 컴퓨터에 설치된 Octave

  • 로컬 컴퓨터에 설정된 Docker 계정

  • Amazon ECR 또는 AWS IoT Analytics 액세스 권한이 있는 AWS 계정

1단계: 도커 이미지 설정

이 자습서에 필요한 3가지 주요 파일이 있습니다. 다음은 파일 이름과 내용입니다.

  • 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 - JSON을 구문 분석하고 AWS 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')
  1. 각 파일의 내용을 다운로드합니다. 새 디렉터리를 생성하고, 그 안에 모든 파일을 넣은 다음 cd를 해당 디렉터리에 넣습니다.

  2. 다음 명령을 실행합니다.

    docker build -t octave-moment .
  3. Docker 리포지터리에 새 이미지가 표시될 것입니다. 다음 명령을 실행하여 인증서를 확인합니다.

    docker image ls | grep octave-moment

2단계: 도커 이미지를 Amazon ECR 리포지토리로 업로드

  1. Amazon ECR 리포지토리를 생성합니다.

    aws ecr create-repository --repository-name octave-moment
  2. Docker 환경에 로그인을 합니다.

    aws ecr get-login
  3. 출력을 복사해 실행시킵니다. 출력은 다음과 같아야 합니다.

    docker login -u AWS -p password -e none https://your-aws-account-id.dkr.ecr..amazonaws.com
  4. Amazon ECR 리포지토리 태그로 생성한 이미지를 태그 처리합니다.

    docker tag your-image-id your-aws-account-id.dkr.ecr.region.amazonaws.com/octave-moment
  5. 이미지를 Amazon ECR로 푸시합니다.

    docker push your-aws-account-id.dkr.ecr.region.amazonaws.com/octave-moment

3단계: 샘플 데이터를 Amazon S3 버킷으로 업로드

  1. 다음을 파일 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
  2. octave-sample-data-your-aws-account-id로 호출된 Amazon S3 버킷을 생성합니다.

  3. 파일 input.txt를 방금 생성한 Amazon S3 버킷으로 업로드합니다. input.txt 파일을 포함한 octave-sample-data-your-aws-account-id이라는 이름의 버킷이 있어야 합니다.

4단계: 컨테이너 실행 역할 생성

  1. 다음을 role1.json 파일에 복사합니다. your-aws-account-id를 AWS 계정 ID로 바꾸고 aws-region을 AWS 리소스의 AWS 리전으로 바꿉니다.

    참고

    이 예에는 혼동된 대리인 보안 문제로부터 보호하기 위한 전역 조건 컨텍스트 키가 포함되어 있습니다. 자세한 내용은 교차 서비스 혼동된 대리인 방지 단원을 참조하십시오.

    { "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" } } ] }
  2. role1.json 다운로드한 파일을 AWS IoT Analytics사용하여 SageMaker AI 및에 대한 액세스 권한을 부여하는 역할을 생성합니다.

    aws iam create-role --role-name container-execution-role --assume-role-policy-document file://role1.json
  3. 이름이 policy1.json인 파일에 다음을 다운로드하고, your-account-id을 계정 ID로 대체합니다(Statement:Resource의 두 번째 ARN 참조).

    { "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" : "*" } ] }
  4. 방금 다운로드한 policy.json 파일을 사용하여 IAM 정책을 생성합니다.

    aws iam create-policy --policy-name ContainerExecutionPolicy --policy-document file://policy1.json
  5. 정책을 역할에 연결합니다.

    aws iam attach-role-policy --role-name container-execution-role --policy-arn arn:aws:iam::your-account-id:policy/ContainerExecutionPolicy

5단계: 컨테이너 작업으로 데이터 세트 생성

  1. 이름이 cli-input.json인 파일에 다음을 다운로드하고, your-account-idregion의 모든 인스턴스를 적절한 값으로 바꿉니다.

    { "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" } ] } } ] }
  2. 방금 다운로드해 편집한 cli-input.json 파일을 사용하여 데이터 세트를 생성합니다.

    aws iotanalytics create-dataset —cli-input-json file://cli-input.json

6단계: 데이터 세트 내용 생성 간접 호출

  1. 다음 명령을 실행합니다.

    aws iotanalytics create-dataset-content --dataset-name octave-dataset

7단계: 데이터 세트 내용 가져오기

  1. 다음 명령을 실행합니다.

    aws iotanalytics get-dataset-content --dataset-name octave-dataset --version-id \$LATEST
  2. DatasetContentStateSUCCEEDED가 되기까지 몇 분 정도 기다려야 할 수도 있습니다.

8단계: 출력을 Ocatave로 인쇄

  1. Octave 셸을 사용해 다음 명령을 실행하고 컨테이너에서 출력을 인쇄합니다.

    bash> octave octave> load output.mat octave> disp(M) -0.016393 -0.098061 0.380311 -0.564377 -1.318744