AWS Amplify 从使用 api 类别创建的 DynamoDB Table 获取 Stream ARN

AWS Amplify get Stream ARN from a DynamoDB Table created with api category

我正在尝试从 api 类别的 @model 创建的 DynmoDB table 获取 ARN。

ARN 是 /amplify/backend/api/{api-name}/build/stacks 下自动生成的 cloudformation 模板的输出。

我尝试在我的 Lambda 函数的 EventSourceMapping 中使用以下语句导入 ARN:

"EventSourceArn": {
                "Fn::ImportValue": {
                    "Fn::Join": [
                        ":",
                        [
                            {
                                "Ref": "apiGraphQLAPIIdOutput"
                            },
                            "GetAtt",
                            "CustomerTable",
                            "StreamArn"
                        ]
                    ]
                }
            },

但这会在推送到云端时引发错误:

Output 'GetAttCustomerTableStreamArn' not found in stack 'arn:aws:cloudformation:eu-central-1:124149422162:stack/myapp-stage-20191009174227-api-SHBHD6GIS7SD/5fb78d10-eaac-11e9-8a4c-0ac41be8cd2e'

我还在backend-config.json中添加了一个dependsOn,但没有解决问题

那么,在 lambda 函数的 cloudformation 模板中获取此流 ARN 的正确方法是什么?

所以,我最近发现确实可以:

您必须在策略中添加此声明以允许访问流:

    {
                            "Action": [
                                "dynamodb:*"
                            ],
                            "Effect": "Allow",
                            "Resource": [
                                {
                                    "Fn::ImportValue": {
                                        "Fn::Join": [
                                            ":",
                                            [
                                                {
                                                    "Ref": "apiGraphQLAPIIdOutput"
                                                },
                                                "GetAtt",
                                                "CustomerTable",
                                                "StreamArn"
                                            ]
                                        ]
                                    }
                                }
                            ]
                        }

此外,添加此 EventSourceMapping:

        "EventSourceArn": {
                    "Fn::ImportValue": {
                        "Fn::Join": [
                            ":",
                            [
                                {
                                    "Ref": "apiGraphQLAPIIdOutput"
                                },
                                "GetAtt",
                                "CustomerTable",
                                "StreamArn"
                            ]
                        ]
                    }
                }

Amplify 正在导出文件夹中的流 ARN

amplify\backend\api\{api_name}\build\stacks\{table_name}.json

这在现有项目中以及在设置新环境时对我有用。