培训中的 AWS NoCredentials

AWS NoCredentials in training

我正尝试在本地 GPU 上为 Amazon Sagemaker 运行 example code。我已将代码从 Jupyter notebook 复制到以下 Python 脚本:

import boto3
import subprocess
import sagemaker
from sagemaker.mxnet import MXNet
from mxnet import gluon
from sagemaker import get_execution_role
import os

sagemaker_session = sagemaker.Session()
instance_type = 'local'
if subprocess.call('nvidia-smi') == 0:
    # Set type to GPU if one is present
    instance_type = 'local_gpu'
# role = get_execution_role()

gluon.data.vision.MNIST('./data/train', train=True)
gluon.data.vision.MNIST('./data/test', train=False)

# successfully connects and uploads data
inputs = sagemaker_session.upload_data(path='data', key_prefix='data/mnist')

hyperparameters = {
    'batch_size': 100,
    'epochs': 20,
    'learning_rate': 0.1,
    'momentum': 0.9,
    'log_interval': 100
}

m = MXNet("mnist.py",
          role=role,
          train_instance_count=1,
          train_instance_type=instance_type,
          framework_version="1.1.0",
          hyperparameters=hyperparameters)

# fails in Docker container
m.fit(inputs)
predictor = m.deploy(initial_instance_count=1, instance_type=instance_type)
m.delete_endpoint()

其中引用的 mnist.py 文件与 Github 中指定的完全一致。该脚本在 Docker 容器中的 m.fit 上失败,并出现以下错误:

algo-1-1DUU4_1  | Downloading s3://<S3-BUCKET>/sagemaker-mxnet-2018-10-07-00-47-10-435/source/sourcedir.tar.gz to /tmp/script.tar.gz
algo-1-1DUU4_1  | 2018-10-07 00:47:29,219 ERROR - container_support.training - uncaught exception during training: Unable to locate credentials
algo-1-1DUU4_1  | Traceback (most recent call last):
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/container_support/training.py", line 36, in start
algo-1-1DUU4_1  |     fw.train()
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/mxnet_container/train.py", line 169, in train
algo-1-1DUU4_1  |     mxnet_env.download_user_module()
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/container_support/environment.py", line 89, in download_user_module
algo-1-1DUU4_1  |     cs.download_s3_resource(self.user_script_archive, tmp)
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/container_support/utils.py", line 37, in download_s3_resource
algo-1-1DUU4_1  |     script_bucket.download_file(script_key_name, target)
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/boto3/s3/inject.py", line 246, in bucket_download_file
algo-1-1DUU4_1  |     ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/boto3/s3/inject.py", line 172, in download_file
algo-1-1DUU4_1  |     extra_args=ExtraArgs, callback=Callback)
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/boto3/s3/transfer.py", line 307, in download_file
algo-1-1DUU4_1  |     future.result()
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 73, in result
algo-1-1DUU4_1  |     return self._coordinator.result()
algo-1-1DUU4_1  |   File "/usr/local/lib/python2.7/dist-packages/s3transfer/futures.py", line 233, in result
algo-1-1DUU4_1  |     raise self._exception
algo-1-1DUU4_1  | NoCredentialsError: Unable to locate credentials

我很困惑,我可以在容器外向 S3 进行身份验证(以加载 training/test 数据),但我不能在 Docker 容器内。所以我猜测问题与将 AWS 凭证传递给 Docker 容器有关。这是生成的 Docker-compose 文件:

networks:
  sagemaker-local:
    name: sagemaker-local
services:
  algo-1-1DUU4:
    command: train
    environment:
    - AWS_REGION=us-west-2
    - TRAINING_JOB_NAME=sagemaker-mxnet-2018-10-07-00-47-10-435
    image: 123456789012.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1.0-gpu-py2
    networks:
      sagemaker-local:
        aliases:
        - algo-1-1DUU4
    stdin_open: true
    tty: true
    volumes:
    - /tmp/tmpSkaR3x/algo-1-1DUU4/input:/opt/ml/input
    - /tmp/tmpSkaR3x/algo-1-1DUU4/output:/opt/ml/output
    - /tmp/tmpSkaR3x/algo-1-1DUU4/output/data:/opt/ml/output/data
    - /tmp/tmpSkaR3x/model:/opt/ml/model
version: '2.1'

是否应将 AWS 凭据作为环境变量传入?

我在阅读 Using boto3 in install local mode? 后升级了我的 sagemaker 安装,但这没有任何效果。我检查了在 Sagemaker 会话(容器外)中获取的凭据,它们似乎是空白的,即使我有一个 ~/.aws/config~/.aws/credentials 文件:

{'_token': None, '_time_fetcher': <function _local_now at 0x7f4dbbe75230>, '_access_key': None, '_frozen_credentials': None, '_refresh_using': <bound method AssumeRoleCredentialFetcher.fetch_credentials of <botocore.credentials.AssumeRoleCredentialFetcher object at 0x7f4d2de48bd0>>, '_secret_key': None, '_expiry_time': None, 'method': 'assume-role', '_refresh_lock': <thread.lock object at 0x7f4d9f2aafd0>}

我是 AWS 的新手,所以我不知道如何诊断有关 AWS 凭据的问题。我的 .aws/config 文件包含以下信息(带有占位符值):

[default]
output = json
region = us-west-2
role_arn = arn:aws:iam::123456789012:role/SageMakers
source_profile = sagemaker-test

[profile sagemaker-test]
output = json
region = us-west-2

sagemaker-test 配置文件在 IAM 管理控制台中具有 AmazonSageMakerFullAccess

.aws/credentials 文件具有以下信息(由占位符值表示):

[default]
aws_access_key_id = 1234567890
aws_secret_access_key = zyxwvutsrqponmlkjihgfedcba
[sagemaker-test]
aws_access_key_id = 0987654321
aws_secret_access_key = abcdefghijklmopqrstuvwxyz

最后,这些是来自 pip freeze:

的适用库的版本
awscli==1.16.19
boto==2.48.0
boto3==1.9.18
botocore==1.12.18
docker==3.5.0
docker-compose==1.22.0
mxnet-cu91==1.1.0.post0
sagemaker==1.11.1

如果我遗漏了任何相关信息,请告诉我,感谢您提供的任何 help/feedback。

更新:感谢大家的帮助!在尝试您建议的一些修复时,我注意到 boto3 已过时,并将其更新(至 boto3-1.9.26botocore-1.12.26)似乎可以解决问题。我找不到任何关于 boto3==1.9.18 问题的文档。如果有人可以帮助我理解 boto3 的问题所在,我很乐意将他们的回答标记为正确。

看起来您在 ~/.aws/credentials 的主机上配置了凭据,但正试图在主机上的 docker 容器 运行 上访问它们。

最简单的解决方案似乎是将您的 aws 凭据安装在容器上的预期位置。您似乎正在使用 sagemaker-mxnet:1.1.0-gpu-py2 图像,该图像似乎使用 root 用户。基于此,如果您将 docker-compose 文件中的卷更新为 algo-1-1DUU4 以包括:

volumes:
  ...
  ~/.aws/:/root/.aws/

这会将您的凭据挂载到容器中的根用户,以便您的 python 脚本应该能够访问它们。

我假设您使用的库的核心是 boto3boto3 建议 that there are several methods of authentication 可用。

  • 在 boto.client() 方法中将凭据作为参数传递
  • 创建会话对象时将凭据作为参数传递
  • 环境变量
  • 共享凭证文件(~/.aws/credentials)
  • AWS 配置文件 (~/.aws/config)
  • 担任提供者角色
  • Boto2 配置文件(/etc/boto.cfg 和 ~/.boto)
  • 配置了 IAM 角色的 Amazon EC2 实例上的实例元数据服务。

但听起来 docker 沙箱无法访问您的 ~/.aws/credentials.conf 文件,因此我会考虑其他可能对您可用的选项。由于我不熟悉 docker,我无法为您的场景提供有保证的解决方案。

我刚刚确认他的示例可以在我本地的机器上运行。请确保您使用的角色有权使用名称以 sagemaker 开头的存储桶。 Sagemaker 默认创建前缀为 sagemaker.

的桶

SageMaker 本地模式旨在获取 boto3 会话中可用的任何凭据,并将它们作为环境变量传递到 docker 容器中。

但是,您使用的 sagemaker sdk 版本(1.11.1 及更早版本)将忽略包含令牌的凭据,因为这通常表示短期凭据不会保持足够长的有效期完成训练工作或终点有用。

如果您使用的是临时凭证,请尝试将其替换为永久凭证,或者 运行 来自分配了适当实例角色的 ec2 实例(或 SageMaker 笔记本!)。

此外,sagemaker sdk 对凭据的处理在 v1.11.2 及更高版本中发生了变化——临时凭据将传递到本地模式容器,但会显示一条警告消息。因此,您可以升级到更新的版本并重试 (pip install -U sagemaker)。

另外,尝试升级boto3可以改变,所以尝试使用最新版本。