[英]Using dynamodb in local docker localhost:8000 with serverless-framework serverless-offline application running on localhost:4500
I'm looking to add state to serverless-framework node application running locally.我希望将状态添加到本地运行的无服务器框架节点应用程序。 I came across the official DynamoDb docker image, i'd like to use serverless framework with this dynamodb instance running on docker exposed at localhost:8000 without using the sls install dynamodb version.
我遇到了官方的 DynamoDb docker 镜像,我想使用无服务器框架,这个 dynamodb 实例在 docker 上运行,暴露在 localhost:8000 上,而不使用 sls install dynamodb 版本。
I have tried using it normally with the nodejs aws-sdk with the endpoint and region configured to local.我已经尝试将它与 nodejs aws-sdk 一起正常使用,并将端点和区域配置为本地。 The new user table is lready created and database is accessible via aws-cli --endpoint localhost:8000 but can't access the dynamodb instance through nodejs aws-sdk
新的用户表已经创建,并且可以通过 aws-cli --endpoint localhost:8000 访问数据库,但无法通过 nodejs aws-sdk 访问 dynamodb 实例
// server.js // server.js
const AWS = require('aws-sdk');
AWS.config.update({
region: 'localhost',
endpoint: "http://127.0.0.1:8000"
});
const ddb = new AWS.DynamoDB.DocumentClient();
const params = {
"TableName":tableName,
"IndexName":"email-index",
"KeyConditions":{
"email":{
"ComparisonOperator": "EQ",
"AttributeValueList": [{"S":email}]
}
}
};
ddb.query(params, (err,data) => {
console.log('query', data); // returns query null
}
//handler.js //handler.js
const server = require('./server');
const http = require('serverless-http');
module.exports.client = http(server);
// serverless.yml // serverless.yml
provider:
name: aws
runtime: nodejs10.16.0
region: ca-central-1
profile: default
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:CreateTable
- dynamodb:ListTables
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:ddblocal:000000000000:table/user"
plugins:
- serverless-offline
functions:
client:
handler: handler.client
events:
- http: GET /
- http: 'GET /{param+}'
- http:
path: /signin
method: post
cors: true
- http:
path: /signup
method: post
cors: true
I expected to get a response from the dynamodb in docker local but the aws-sdk cannot connect to it.我希望从 docker local 中的 dynamodb 得到响应,但 aws-sdk 无法连接到它。 The above http events go to express.js which works well.
上面的 http 事件转到 express.js,效果很好。
Try to update if local如果本地尝试更新
let dynamoDb = new AWS.DynamoDB.DocumentClient();
if (process.env.STAGE === 'dev') dynamoDb = new AWS.DynamoDB.DocumentClient({
region: 'localhost',
endpoint: 'http://localhost:8000',
accessKeyId: 'DEFAULT_ACCESS_KEY',
secretAccessKey: 'DEFAULT_SECRET'
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.