运行 放大模拟时无法访问 DynamoDB 主机

Inaccessible DynamoDB host when running amplify mock

我正在使用 AWS Amplify 设置 AppSync GraphQL API。我有一个带有 @model 注释的模式,我正在尝试编写一个 lambda 解析器,它将 read/write 到 @model 生成的 DynamoDB table。但是,当我尝试使用 amplify mock 在本地进行测试时,我的 JS 函数会抛出

error { UnknownEndpoint: Inaccessible host: `dynamodb.us-east-1-fake.amazonaws.com'. This service may not be available in the `us-east-1-fake' region.

我似乎找不到太多关于这个用例的文档(大多数 lambda 解析器的例子是从其他 tables / APIs 读取的,它们不属于放大应用程序的一部分)所以任何指针表示赞赏。 运行 是否支持这种类型的设置,或者我是否必须推送到 AWS 才能进行测试?

您的 dynamodb 主机不正确。 dynamodb.us-east-1-fake 不是有效主机。请使用真实的 dynamodb 主机名更新它。
如果您 运行 首先在 cli 上本地设置 aws configure。

新答案:

Amplify 现在有关于此用例的文档:https://docs.amplify.aws/cli/usage/mock#connecting-to-a-mock-model-table

您可以为 mock 设置环境变量,将 mock lambda 中的 DDB 客户端指向本地 DDB 实例

=========================================== ========================

原答案:

在深入研究 Amplify CLI 代码后,我找到了一个目前可行的解决方案。

Here is where amplify mock initializes DynamoDB Local. As you can see, it does not set the --sharedDb flag which based on the docs means that the created database files will be prefixed with the access key id of the request and then the region. The access key id of requests from Amplify will be "fake" and the region is "us-fake-1" as defined here. Furthermore, the port of the DynamoDB Local instance started by Amplify is 62224 defined here.

因此,要连接到由 Amplify 创建的表,需要以下 DynamoDB 配置

const ddb = new AWS.DynamoDB({
  region: 'us-fake-1',
  endpoint: "http://172.16.123.1:62224/",
  accessKeyId: "fake",
  secretAccessKey: "fake"
})

如果您想将 AWS CLI 与 Amplify 创建的表一起使用,则必须使用上述区域和访问密钥创建一个新的配置文件。

我仍然需要做一些额外的工作来找出一种让这些配置值在本地模拟值和实际值之间切换的好方法,但这暂时解除了本地测试的阻塞。

至于我的另一个问题,关于“us-east-1-fake”的 AWS::Region 被设置的位置,它被设置 here 但它似乎没有在任何地方使用别的。即,它在 运行 amplify mock 时被设置为占位符值,但在其他地方将其用作本地测试的区域似乎不起作用。

请尝试以下设置,它对我来说工作正常,

const AWS = require('aws-sdk');

// Local
const dynamoDb = new AWS.DynamoDB.DocumentClient({
    region: 'us-fake-1',
    endpoint: "http://localhost:62224/",
    accessKeyId: "fake",
    secretAccessKey: "fake"
});

// Live
// const dynamoDb = new AWS.DynamoDB.DocumentClient();