AWS放大CLI未能使用Lambda和DynamoDB部署API

发布于 2025-01-31 05:38:13 字数 1128 浏览 2 评论 0原文

我正在使用放大CLI工具来创建我的React Demo应用程序的无服务器后端。我创建了一个存储(使用放大添加存储),该存储是一个DynamoDB表。然后,我创建了一个Python lambda函数,并告诉Amplify-CLI,它需要访问另一个资源(将其指向DynamoDB表格)。在这一点上,我认为Amplify-CLI工具将在Lambda中添加权限以访问DyanModB表。然后,我使用使用我创建的lambda的Amplify-CLI创建API(REST)。

现在,在我的React应用程序中,我想向该API端点提出请求,该端点将触发lambda,Lambda将从DynamoDB中检索数据并返回响应。

我使用“ Amplify Push”部署了后端

,但是,我有两个问题:

  1. 当我去AWS仪表板测试已部署功能时,当我从AWS控制台运行测试时,我看到一个错误,说现在允许使用Lambda访问DynamoDB表。然后,我去了IAM并找到了政策,然后添加了“ DynamoDB读取访问”。这解决了访问问题;但是,我仍然想知道如何在放大CLI级别上执行此操作,而不是通过AWS控制台手动执行此操作。我知道我的修复程序不是便携式的,理想情况下,Amplify-CLI应该为我处理这个问题,这是否有可能?
  2. (1)修复后,我的React应用程序仍然无法使用以下错误访问API:“ Origin http:// localhost:accesse-control-allow-allow-Origin不允许3000。状态代码:400”。我知道这是一个CORS错误,但是我的lambda在其响应中返回:
        return {
            'statusCode': 200,
            'headers': {
                'Access-Control-Allow-Headers': '*',
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
            },
            'data': my_data
        }

还要在创建终点时,我对“限制”问题我说“不”。

任何建议将不胜感激。谢谢!

I am using the amplify CLI tool to create the serverless backend of my React demo app. I have created a storage (using amplify add storage) which is a DynamoDB table. Then I created a Python lambda function, where I told amplify-cli that it needs to access another resource (pointing it to the DynamoDB table). At this point, I thought that the amplify-cli tool will add permissions to the lambda to access the DyanmoDB table. Then I create an API (REST) using the amplify-cli which uses the lambda I have created.

Now in my React app, I want to make a request to that API end-point, which is going to trigger the lambda, the lambda will retrieve data from DynamoDB, and return a response.

I deployed the backend using “amplify push”

However, I have two problems:

  1. When I go to the AWS dashboard to test the deployed function, when I run a test from the AWS Console, I see an error saying that the lambda is now allowed to access the DynamoDB table. I then went to IAM and found the policy and I added "DynamoDB Read Access". This solved the access problem; however, I still want to know how I could do this at the amplify-cli level instead of manually doing it through AWS Console. I understand my fix is not portable, and ideally the amplify-cli should have handled this for me, is it possible?
  2. After the (1) fix, my React app still fails to access the API with the following error: "Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. Status code: 400". I understand that this is a CORS error, but my lambda, in its response it returns:
        return {
            'statusCode': 200,
            'headers': {
                'Access-Control-Allow-Headers': '*',
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
            },
            'data': my_data
        }

Also, when creating the end-point, to the question “restriction” I said “no”.

Any advice will be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文