How to get table name in AWS dynamodb trigger function?

I am new to AWS and working on creating a lambda function in Python. The function will receive the dynamodb table stream and write the file to s3. Here the file name should be the name of the table. Can someone please tell me how to get the table name if the trigger calling the lambda function?

Thanks for the help.

+8
source share
3 answers

Since you mentioned that you are new to AWS, I am going to answer descriptively.

I assume that you set the "With Stream Enabled" parameter for the DynamoDB table to "Yes" and configured this as the event source for your lambda function.

This is how I got the table name from the thread that called my lambda function -

def lambda_handler(event, context): print(json.dumps(event, indent=2)) # Shows what in the event object for record in event['Records']: ddbARN = record['eventSourceARN'] ddbTable = ddbARN.split(':')[5].split('/')[1] print("DynamoDB table name: " + ddbTable) return 'Successfully processed records.' 

In principle, the event object, which contains all the information about a particular DynamoDB thread that was responsible for this special function of the lambda function, contains the eventSourceARN parameter. This eventSourceARN is the ARN (Amazon Resource Number) that uniquely identifies your DynamoDB table from which the event came.

This is an example value for eventSourceARN -

ARN: AWS: dynamodb: us-east-1: 111111111111: table / test /stream/2020-10-10T08:18:22.385

Pay attention to the bold text above - test ; This is the name of the table you are looking for.

In the line ddbTable = ddbARN.split(':')[5].split('/')[1] above, I tried to split the entire ARN into ':' first and then to '/' to get the test value. Once you get this value, you can call the S3 API to write to the file in S3 with the same name.

Hope this helps.

+15
source

One way to do this would be through pattern matching in Scala using regex:

 val ddbArnRegex: Regex = """arn:aws:dynamodb:(.+):(.+):table/(.+)/stream/(.+)""".r def parseTableName(ddbARN: String): Option[String] = { if (null == ddbARN) None ddbARN match { case ddbArnRegex(_, _, table, _) => Some(table) case _ => None } } 
0
source

Please note that eventSourceArn is not always provided. From my testing today, I did not see the eventSourceArn presented in the record. You can also refer to the links:

0
source

Source: https://habr.com/ru/post/1242564/


All Articles