[英]How do I use AWS Amplify tables from an API Gateway Lambda?
I had an HTTP API built using the AWS API Gateway.我有一个使用 AWS API Gateway 构建的 HTTP API。 Then I designed an admin web interface for the API using AWS Amplify.
然后我使用 AWS Amplify 为 API 设计了一个管理 Web 界面。
The two systems communicate only through DynamoDB tables: the API writes to a table, the web interface reads from it.这两个系统仅通过 DynamoDB 表进行通信:API 写入表,Web 界面从中读取。
For the sake of DRY the tables are created in Amplify from a GraphQL schema and not by CloudFormation resources with explicit table names.为了 DRY,表是在 Amplify 中从 GraphQL 模式创建的,而不是由具有显式表名的 CloudFormation 资源创建的。 This way there's no need to define the tables in 2 places.
这样就不需要在 2 个地方定义表。
But now I have a problem: the Amplify tables come with random names such as foo-55c3wkhfdhj-bar
, so to write to them from an AWS Lambda I need a way to know the name or its random part.但是现在我遇到了一个问题:Amplify 表带有随机名称,例如
foo-55c3wkhfdhj-bar
,因此要从 AWS Lambda 写入它们,我需要一种方法来知道名称或其随机部分。
How do I do that?我怎么做? I know about the CloudFormation export mechanism.
我了解 CloudFormation 导出机制。 Can I use it?
我可以使用它吗?
Now I have a simple solution of hardcoding the table names in a configuration file such as现在我有一个简单的解决方案,可以在配置文件中对表名进行硬编码,例如
{ foo: "foo-55c3wkhfdhj-bar" }
Are there better solutions?有更好的解决方案吗?
Note that I like the ApiGateway service the way it is.请注意,我喜欢 ApiGateway 服务的方式。 I don't want to move the lambdas into the Amplify backend.
我不想将 lambdas 移动到 Amplify 后端。 Imagine that it wasn't APIGateway but some code that cannot be easily migrated to AWS Lambda backend, say EC2.
想象一下,它不是 APIGateway,而是一些无法轻松迁移到 AWS Lambda 后端的代码,例如 EC2。
https://github.com/aws-amplify/amplify-cli/issues/996 has some ideas, but creating a table elsewhere and using it in the AWS Amplify part is exactly what I want to avoid. https://github.com/aws-amplify/amplify-cli/issues/996有一些想法,但在其他地方创建一个表并在 AWS Amplify 部分使用它正是我想要避免的。 And unlike there I don't care if Amplify names are random or not.
与那里不同,我不在乎 Amplify 名称是否随机。
I've solved this exact issue in a bash script using the aws cli:我已经使用 aws cli 在 bash 脚本中解决了这个确切的问题:
aws dynamodb list-tables --output text | sed -n "s/^.*\($TABLE_NAME-.*-$ENV\)[.\n]*/\1/p"
The variable $TABLE_NAME is the predictable prefix of the table and $ENV is the amplify environment suffix.变量 $TABLE_NAME 是表的可预测前缀,$ENV 是放大环境后缀。
Then fancy sed command uses regex to capture the full table name using a capture group so that the full table name is the only thing that comes out.然后花哨的 sed 命令使用 regex 使用捕获组捕获完整的表名,以便完整的表名是唯一出来的。
You could probably do something similar with the AWS API:您可能可以使用 AWS API 做类似的事情:
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ListTables.html https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ListTables.html
Ping the api to get a list of the tables and the parse and sort them to find the table you're looking for. Ping api 以获取表和解析的列表并对它们进行排序以找到您要查找的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.