简体   繁体   English

如何使用来自 API Gateway Lambda 的 AWS Amplify 表?

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

相关问题 如何在AWS Lambda函数中从AWS API Gateway URI访问请求和路径变量 - How do I get access to the request and path variables from AWS API Gateway URI in an AWS Lambda function 如何阻止 CORS 拒绝我在 AWS API 网关中创建的 Lambda 代理? - How do I stop CORS from rejecting my Lambda-Proxy that I create in AWS API Gateway? AWS:如何限制从 Amplify 应用程序访问 API 网关? - AWS: how to restrict access to API gateway from Amplify app? 如何在 SAM 的 API 网关方法中取消选中“使用 Lambda 代理集成”? - How do i uncheck "Use Lambda Proxy Integration" in an API Gateway method from with SAM? 如何在AWS API Gateway和AWS Lambda中使用令牌(OAuth?) - How to use tokens (OAuth ?) with AWS API Gateway and AWS Lambda 我如何将 Python Lambda Function 集成到 AWS Amplify 的管道中 - How do i integrate a Python Lambda Function into the Pipeline of AWS Amplify 如何调试AWS Api Gateway&Lambda的“AWS / ApiGateway 5XXError” - How do I debug AWS Api Gateway & Lambda's “AWS/ApiGateway 5XXError” 如何将 AWS API Gateway 查询字符串映射到 C# AWS Lambda 函数? - How do I map AWS API Gateway query string to C# AWS Lambda function? C#如何配置AWS API Gateway参数以映射到基本的AWS Lambda函数? - C# How do i configure AWS API Gateway parameters to map to a basic AWS Lambda Function? 如何以 APIGatewayProxyRequestEvent 格式发送 API 请求? - AWS API 网关 Lambda 代理 - How do I send API request in APIGatewayProxyRequestEvent format? - AWS API Gateway Lambda Proxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM