简体   繁体   English

在本地 docker localhost:8000 中使用 dynamodb 和在 localhost:4500 上运行的 serverless-framework serverless-offline 应用程序

[英]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.

相关问题 无服务器离线 + DynamoDb 本地问题 - Serverless offline + DynamoDb local issue 无服务器离线 - 本地 dynamoDb 上的迁移不起作用 - Serverless offline - Migration on local dynamoDb is not working 无服务器离线 - API REST 节点 typescript - Serverless-offline - API REST node typescript serverless-offline 可选路径参数 - serverless-offline optional path parameter 带有无服务器离线阶段中断路由的 aws-serverless-express - aws-serverless-express with serverless-offline stage breaks routing 无服务器框架不会从 dynamodb local 开始 - Serverless framework won't start with dynamodb local 如何在无服务器离线中使用 Cognito 事件/触发器调用 lambda 以进行本地测试 - How to invoke lambda with Cognito event/trigger in serverless-offline for local test 如何使用无服务器捆绑包在无服务器框架项目中调试 Visual Studio Code 中的测试? - How can I debug tests in Visual Studio Code with a serverless-framework project using the serverless-bundle package? 离线 DynamoDB + Serverless + Lambda ResourceNotFoundException - Offline DynamoDB + Serverless + Lambda ResourceNotFoundException 无法获取路径:/{proxy+} 工作(无服务器离线) - Cant get path: /{proxy+} to work (serverless-offline)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM