node.js - Docker Dynamodb 错误 : Nodejs App with dynamodb does not work in docker but works without it

标签 node.js amazon-dynamodb

我用 dynamodb 中只有一个表制作了一个简单的 nodejs 应用程序。我正在使用 Vogels作为数据包装器。 如果我不对它进行 docker 化,它会工作得很好。

我也尝试按照在开发模式下运行应用程序的方式运行 docker。那也没有用。

这是我用来创建表格的代码:

module.exports = function () {
    const vogels = require('vogels');
    // const AWS= require('aws-sdk');
    const Lr = require("../api/models/LRModel");
    if (process.env.NODE_ENV === "test") {
        AWS.config.update({ accessKeyId: "myKeyId", secretAccessKey: "secretKey", region: "us-west-2" });
        vogels.dynamoDriver(new AWS.DynamoDB({ endpoint: 'http://localhost:8000' }));
    } else {
        console.log("AWS access key=", process.env.AWS_ACCESS_KEY, "AWS Secret key=", process.env.AWS_SECRET_KEY, "AWS Region =", process.env.AWS_REGION);
        vogels.AWS.config.update({accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION, sslEnabled: false });

    }

    vogels.createTables({
        'lrs': {readCapacity: 10, writeCapacity: 10}
    }, function(err) {
        if (err) {
            console.log('Error creating tables: ', err);
        } else {
            console.log('Tables have been created');
        }
    });
}

这是我在 docker 中遇到的错误:

server-0   | Error creating tables:  { TimeoutError: Missing credentials in config
server-0   |     at ClientRequest.<anonymous> (/app/node_modules/vogels/node_modules/aws-sdk/lib/http/node.js:61:34)
server-0   |     at ClientRequest.g (events.js:291:16)
server-0   |     at emitNone (events.js:86:13)
server-0   |     at ClientRequest.emit (events.js:185:7)
server-0   |     at Socket.emitTimeout (_http_client.js:626:10)
server-0   |     at Socket.g (events.js:291:16)
server-0   |     at emitNone (events.js:86:13)
server-0   |     at Socket.emit (events.js:185:7)
server-0   |     at Socket._onTimeout (net.js:339:8)
server-0   |     at ontimeout (timers.js:365:14)
server-0   |   message: 'Missing credentials in config',
server-0   |   code: 'CredentialsError',
server-0   |   time: 2017-06-29T05:36:23.724Z,
server-0   |   originalError:
server-0   |    { message: 'Could not load credentials from any providers',
server-0   |      code: 'CredentialsError',
server-0   |      time: 2017-06-29T05:36:23.724Z,
server-0   |      originalError:
server-0   |       { message: 'Missing credentials in config',
server-0   |         code: 'CredentialsError',
server-0   |         time: 2017-06-29T05:36:23.721Z,
server-0   |         originalError: [Object] } } }

当我在 WITHOUT docker 上运行它时,我清楚地注意到一件事是 dynamodb 端点是一个 http 端点。这是从 https://github.com/aws/aws-sdk-js/blob/8904e9c730fb2fccf9d201f66266a6e2cbb75348/lib/http/node.js 记录的端点
(第 13 行)。

server-0   |   Endpoint {
server-0   |   protocol: 'https:',
server-0   |   host: 'dynamodb.us-west-1.amazonaws.com',
server-0   |   port: 443,
server-0   |   hostname: 'dynamodb.us-west-1.amazonaws.com',
server-0   |   pathname: '/',
server-0   |   path: '/',
server-0   |   href: 'https://dynamodb.us-west-1.amazonaws.com/',
server-0   |   constructor: { [Function: Endpoint] __super__: [Function: Object] } }

当我WITH docker 运行它时,dynamodb 端点是一个httpS 端点。这是从 https://github.com/aws/aws-sdk-js/blob/8904e9c730fb2fccf9d201f66266a6e2cbb75348/lib/http/node.js 记录的端点
(第 13 行)。

server-0   |   Endpoint {
server-0   |   protocol: 'http:',
server-0   |   host: '169.254.169.254',
server-0   |   port: 80,
server-0   |   hostname: '169.254.169.254',
server-0   |   pathname: '/latest/meta-data/iam/security-credentials/',
server-0   |   path: '/latest/meta-data/iam/security-credentials/',
server-0   |   href: 'http://169.254.169.254/latest/meta-data/iam/security-credentials/' }

我哪里出错了?

最佳答案

当我在不使用 docker 的情况下运行应用程序时,将从 /.aws/credentials 中提取凭据。我的印象是它是通过 vogels.AWS.config.update({accessKeyId: process.env.AWS_ACCESS_KEY, secretAccessKey: process.env.AWS_SECRET_KEY, region: process.env.AWS_REGION, sslEnabled: false } );

我在 aws-sdk 的 config.js 中记录了 update 函数中的选项,并在 friend 的输入帮助下找到了这个。

罪魁祸首似乎是表定义应该在 up config 更新之后。 我带来了这一行 const Lr = require("../api/models/LRModel"); 在 config.update 下,它现在运行良好

关于node.js - Docker Dynamodb 错误 : Nodejs App with dynamodb does not work in docker but works without it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819541/

相关文章:

node.js - 有没有办法从node-rdkafka中的生产者队列缓冲区获取消息?

javascript - Node.JS 回调 post 请求关闭

node.js - 如何从 NodeJS 中的特定子集获取所有 Redis 哈希值?

amazon-web-services - 带排序键的无服务器框架Dynamo DB表资源定义

c# - Dynamodb : No defined key schema. 必须为所有表定义至少包含哈希键的键模式

amazon-web-services - 为什么使用 `aws-sdk-go-v2` 时看不到 dynamodb 本地容器中的表?

PHP - DynamoDB - 唯一键

performance - 在效率方面,GetItem/BatchGetItem 与查询和扫描 DynamoDB 表相比如何?

javascript - PM2 管理 PHP 脚本 - 集群模式

node.js - 将参数传递给要求(加载模块时)