Search code examples
aws-lambdaaws-cloudformationaws-amplifyamazon-dynamodb-streams

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


I´m trying to get the ARN from a DynmoDB table created with @model from the api category.

The ARN is an output from the autogenerated cloudformation template under /amplify/backend/api/{api-name}/build/stacks.

I tried to import the ARN with the following statement in the EventSourceMapping for my Lambda function:

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

But this throws the an error when pushing to the cloud:

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

I also added a dependsOn in the backend-config.json, which doesn’t resolve the problem

So, what would be the correct way to get this stream ARN in a cloudformation template of a lambda function?


Solution

  • So, I recently discovered it is indeed possible:

    You must add this statement for allowing access to the stream in your policy:

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

    And additionally, add this EventSourceMapping:

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

    Amplify is exporting the Stream ARNs in the folder

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

    This worked for me in an existing project and also when setting up a new env.