运行放大模拟时无法访问 DynamoDB 主机 [英] Inaccessible DynamoDB host when running amplify mock

查看:13
本文介绍了运行放大模拟时无法访问 DynamoDB 主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AWS Amplify 设置 AppSync GraphQL API.我有一个带有 @model 注释的架构,我正在尝试编写一个 lambda 解析器,它将读取/写入 @model 生成的 DynamoDB 表.但是,当我尝试使用 amplify mock 在本地进行测试时,我的 JS 函数会抛出

I am using AWS Amplify to set up an AppSync GraphQL API. I have a schema with an @model annotation and I am trying to write a lambda resolver that will read/write to the DynamoDB table that @model generates. However, when I attempt to test locally using amplify mock my JS function throws

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 解析器示例都是从不属于放大应用程序的其他表/API 中读取的),因此任何指针都值得赞赏.是否支持运行这种类型的设置,还是我必须推送到 AWS 才能进行测试?

I can't seem to find much documentation around this use case at all (most examples of lambda resolvers read from other tables / APIs that are not part of the amplify app) so any pointers are appreciated. Is running this type of setup even supported or do I have to push to AWS in order to test?

推荐答案

新答案:

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

Amplify now has documentation on this use case: https://docs.amplify.aws/cli/usage/mock#connecting-to-a-mock-model-table

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

You can set environment variables for mock that will point the DDB client in the mock lambda to the local DDB instance

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

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

原答案:

在深入研究了 Amplify CLI 代码后,我找到了一个现在可以使用的解决方案.

After some digging into the Amplify CLI code, I have found a solution that will work for now.

这里amplify mock 初始化 DynamoDB Local 的地方.如您所见,它没有设置 基于文档 表示创建的数据库文件将以请求的访问密钥ID为前缀,然后是区域.来自 Amplify 的请求的访问密钥 ID 将是假的";并且该区域是us-fake-1";按照定义 这里.此外,Amplify 启动的 DynamoDB Local 实例的端口为 62224 定义 这里.

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 配置

Therefore, to connect to the tables that are created by Amplify, the following DynamoDB config is needed

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

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

If you want to use the AWS CLI with the tables created by Amplify, you'll have to create a new profile with the region and access keys above.

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

I'll still need to do some additional work to figure out a good way to have those config values switch between the local mock values and the actual ones, but this unblocks local testing for now.

关于 AWS::Region of "us-east-1-fake" 的另一个问题.正在设置中,该设置已设置ts#L10" rel="nofollow noreferrer">here 但它似乎没有在其他任何地方使用.即,它在运行 amplify mock 时被设置为占位符值,但在其他地方将其用作区域进行本地测试似乎不起作用.

As for another question that I had about where AWS::Region of "us-east-1-fake" was being set, that gets set here but it does not appear to be used anywhere else. ie, it gets set as a placeholder value when running amplify mock but using it as a region in other places for testing locally doesn't seem to work.

这篇关于运行放大模拟时无法访问 DynamoDB 主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆