无服务器或 AWS NodeJS SDK 是否需要 HTTPS?

时间:2021-03-29 23:11:38

标签: amazon-web-services amazon-dynamodb serverless

我正在尝试使用 serverless-dynamodb-local,但在运行时出现 NetworkError。我已将问题缩小到插件在启动时将我的表迁移到本地数据库的时间。它在 createTable 命令上失败并显示以下错误:

NetworkingError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:": DynamoDB - Error - 
 
 Networking Error ----------------------------------------

  TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
      at new ClientRequest (_http_client.js:144:11)
      at Object.request (http.js:44:10)
      at features.constructor.handleRequest ($root\node_modules\aws-sdk\lib\http\node.js:45:23)
      at executeSend ($root\node_modules\aws-sdk\lib\event_listeners.js:348:29)
...
      at Request.callListeners ($root\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
      at Timeout.callNextListener [as _onTimeout] ($root\node_modules\aws-sdk\lib\sequential_executor.js:96:12) 
      at listOnTimeout (internal/timers.js:531:17)
      at processTimers (internal/timers.js:475:7)

我编写了这个测试脚本,当 DynamoDB local 在单独的命令窗口中运行时运行。这个脚本每次都成功运行,包括创建表!

const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB({
  endpoint: 'http://localhost:8000',
  region: 'localhost',
  accessKeyId: 'DEFAULT_ACCESS_KEY', // needed if you don't have aws credentials at all in env
  secretAccessKey: 'DEFAULT_SECRET', // needed if you don't have aws credentials at all in env
});

async function main() {
  try {
    console.log(`${JSON.stringify(dynamo.endpoint, null, 2)}`);
    const tables = await dynamo.listTables().promise();
    console.log(`Tables:`, JSON.stringify(tables, null, 2));

    var params = {
      AttributeDefinitions: [
        {
          AttributeName: 'Artist',
          AttributeType: 'S',
        },
        {
          AttributeName: 'SongTitle',
          AttributeType: 'S',
        },
      ],
      KeySchema: [
        {
          AttributeName: 'Artist',
          KeyType: 'HASH',
        },
        {
          AttributeName: 'SongTitle',
          KeyType: 'RANGE',
        },
      ],
      ProvisionedThroughput: {
        ReadCapacityUnits: 5,
        WriteCapacityUnits: 5,
      },
      TableName: 'Music',
    };
    const tableData = await dynamo.createTable(params).promise();
    console.log('Created table', JSON.stringify(tableData, null, 2));
  } catch (err) {
    console.error(`Unexpected error!`, err);
  }
}

main();

由于我的测试脚本中的代码与 Serverless 插件中的代码基本相同,我不明白是什么导致了问题。是否有关于如何启动需要 https 的无服务器或 AWS 开发工具包?另见this Github issue

更新

package.json

{
  "name": "test",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "serverless dynamodb install && serverless offline start"
  },
  "dependencies": {},
  "devDependencies": {
    "aws-sdk": "^2.407.0",
    "serverless": "^2.31.0",
    "serverless-dynamodb-client": "^0.0.2",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-offline": "^6.9.0"
  }
}

serverless.yml

service: dynamodb-local-test

frameworkVersion: ">=2.0.0"

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-2

plugins:
  - serverless-dynamodb-local
  - serverless-offline

custom:
  dynamodb:
    stages:
      - dev
    start:
      port: 8000
      inMemory: true
      migrate: true
      seed: true
    seed:
      dev:
        sources:
          - table: Users
            sources: [seed-data/Users.json] 
          - table: Tweets
            sources: [seed-data/Tweets.json]

resources:
  Resources:
    UserTable:
      Type: "AWS::DynamoDB::Table"
      Properties:
        KeySchema:
          -
            AttributeName: handle
            KeyType: HASH
        AttributeDefinitions:
          -
            AttributeName: handle
            AttributeType: S
        ProvisionedThroughput:
          ReadCapacityUnits: 10
          WriteCapacityUnits: 10
        TableName: "Users"

    TweetsTable:
      Type: "AWS::DynamoDB::Table"
      Properties:
        KeySchema:
          -
            AttributeName: tweet_id
            KeyType: HASH
        AttributeDefinitions:
          -
            AttributeName: tweet_id
            AttributeType: S
          -
            AttributeName: handle
            AttributeType: S
          -
            AttributeName: retweet_count
            AttributeType: N
          -
            AttributeName: created_at
            AttributeType: S
        ProvisionedThroughput:
          ReadCapacityUnits: 10
          WriteCapacityUnits: 10
        TableName: "Tweets"
        GlobalSecondaryIndexes:
          -
            IndexName: top-index
            KeySchema:
              -
                AttributeName: handle
                KeyType: HASH
              -
                AttributeName: retweet_count
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 50
              WriteCapacityUnits: 50
          -
            IndexName: tweet-index
            KeySchema:
              -
                AttributeName: handle
                KeyType: HASH
              -
                AttributeName: created_at
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 50
              WriteCapacityUnits: 50

更新

我暂时不再遇到这个问题,但我不知道为什么。另外,几个小时后,在另一个项目中,我又遇到了这个问题。我尝试更改以下两件事:

  1. 我断开了与公司 VPN 的连接。
  2. 我将 aws-sdk 软件包从 ^2.407.0 更新为 ^2.875.0。

但是,在看到 dynamodb-local 正在启动、迁移和播种后,我恢复了这两个更改以测试这是否是问题所在。我还清除了本地 .dynamodb 文件夹。这三个更改都不会导致插件再次失败。我什至还能够将本地 dynamodb 实例连接到本地 appsync 实例。

插件和本地 DynamoDB 实例之间是否存在竞争条件?

0 个答案:

没有答案
相关问题