错误[CredentialsError]:配置中缺少凭据,但仅在Fargate AWS上运行时

时间:2020-06-14 22:31:08

标签: node.js amazon-web-services docker aws-fargate aws-sdk-js

我构建了一个dockerfile和docker-compose.yml文件来导入环境。环境看起来像这样:

AWS_ACCESS_KEY_ID=XXX
AWS_SECRET_ACCESS_KEY=XXX
ROLE_ARN=XXX
BUCKET_NAME=BUCKET
AWS_PROFILE=default
AWS_SDK_LOAD_CONFIG=1
AWS_SHARED_CREDENTIALS_FILE=[path]
AWS_CONFIG_FILE=[path]
AWS_REGION=region

当我尝试将文件上传到S3时,当我使用docker-compose up在我的PC上运行该docker时,它工作起来非常完美,但是每当我在Fargate上运行它时,都会出现此错误

Error [CredentialsError]: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at Request.extractError (/home/myuser/node_modules/aws-sdk/lib/protocol/query.js:50:29)
    at Request.callListeners (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/home/myuser/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/home/myuser/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/home/myuser/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /home/myuser/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/home/myuser/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/home/myuser/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at Request.emit (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/home/myuser/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/home/myuser/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/home/myuser/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /home/myuser/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/home/myuser/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/home/myuser/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at callNextListener (/home/myuser/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
    at IncomingMessage.onEnd (/home/myuser/node_modules/aws-sdk/lib/event_listeners.js:307:13)
    at IncomingMessage.emit (events.js:322:22)
    at IncomingMessage.EventEmitter.emit (domain.js:482:12) {
  code: 'CredentialsError',
  time: 2020-06-11T12:47:25.609Z,
  requestId: '0d6fd3d4-df21-4195-a6e4-de470006429d',
  statusCode: 403,
  retryable: false,
  retryDelay: 80.8300420619739,
  originalError: {
    message: 'Could not load credentials from ChainableTemporaryCredentials',
    code: 'CredentialsError',
    time: 2020-06-11T12:47:25.609Z,
    requestId: '0d6fd3d4-df21-4195-a6e4-de470006429d',
    statusCode: 403,
    retryable: false,
    retryDelay: 80.8300420619739,
    originalError: {
      message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
      code: 'CredentialsError',
      time: 2020-06-11T12:47:25.609Z,
      requestId: '0d6fd3d4-df21-4195-a6e4-de470006429d',
      statusCode: 403,
      retryable: false,
      retryDelay: 80.8300420619739,
      originalError: [Object]
    }
  }
}

这是我的凭据代码的一部分:

    var envCreds = new AWS.EnvironmentCredentials('AWS');
    s3.config.credentials = envCreds;
    s3.config.credentials = new AWS.ChainableTemporaryCredentials({
        params: {
            RoleArn: process.env.ROLE_ARN,
            region: process.env.AWS_REGION
        }
    });

我需要切换角色。我尝试使用SharedIniFileCredentials,但没有遇到麻烦,我总是得到AccessDenied。

我使用控制台日志记录了凭证,唯一的区别是 region在Fargate上设为ap-northeast-1,而在本地undefined上设为。由于存储桶和Fargate在同一服务器上。我真的不知道这个区域来自哪里以及它的重要性。

这是一个错误还是我在某个地方错了?为什么它可以在我的PC上正常运行,但不能运行Fargate?那是因为Fargate中的环境冲突吗?我已经在这个麻烦中待了几天,任何帮助将不胜感激。谢谢。

编辑:我想通了。不知何故,我的Fargate中的Docker使用了不同的环境变量,当我尝试使用这些变量时,我在本地遇到了相同的错误。

1 个答案:

答案 0 :(得分:1)

基于评论。

一种针对身份验证问题的建议解决方案是使用IAM Roles for Tasks,而不是将访问和密钥硬编码到代码中或将它们作为env变量传递。

使用任务角色符合AWS 良好做法,AWS不对任何凭证进行硬编码,而是使用临时凭证:

通过具有用于Amazon ECS任务的IAM角色,您可以指定任务中的容器可以使用的 IAM角色。应用程序必须使用AWS凭证签名其AWS API请求,此功能提供了一种管理凭证的策略,供您的应用程序使用,类似于Amazon EC2实例配置文件向EC2实例提供凭证的方式。 >