amazon-dynamodbaws-step-functions

How can I send a list to an API with AWS step function


I'm creating an AWS step function that makes a request to an API.

I'm trying to send a list that I retrieved from dynamoDB to an api that receives it. however, when I am sending the array it is going in an incorrect format with the dynamoDB structure.

 "informacoes": [
            {
                "M": {
                    "chave": {
                        "S": "LSJ33"
                    },
                    "valor": {
                        "N": "39"
                    },
                    "numero": {
                        "S": "22BRQ"
                    }
                }
            }
        ]

I would like send to my api it:

 "informacoes": [
        {          
                "chave": "LSJ33",
                "valor": "39",
                "numero": "22BRQ"                        
        }
    ]

Does anyone know any way I can fix this?


Solution

  • You would need to pre-process the data received from dynamodb as it usually returns data along with datatype, M representing Map here.

    You could integrate a lambda in your step function as a new state and input the dynamodb data in it to pre-process it.

    import json
    
    def lambda_handler(event, context):
    informacoes = event['informacoes']
    
    # Transform the DynamoDB format to the desired format
    transformed_informacoes = []
    for info in informacoes:
        transformed_info = {
            "chave": info['M']['chave']['S'],
            "valor": info['M']['valor']['N'],
            "numero": info['M']['numero']['S']
        }
        transformed_informacoes.append(transformed_info)
    
    return {
        'statusCode': 200,
        'body': json.dumps({'informacoes': transformed_informacoes})
    }