Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

993
Visualizações
AWS Lambda "Unable to marshal response" Error

I am trying to setup an AWS lambda that will start an SSM session in my EC2 instance and run a command. For simplicity right now I am just trying to run ls. Here is my lambda function:

import json
import boto3

def lambda_handler(commands, context):
    """Runs commands on remote linux instances
    :param client: a boto/boto3 ssm client
    :param commands: string, each one a command to execute on the instance
    :return: the response from the send_command function (check the boto3 docs for ssm client.send_command() )
    """
    client = boto3.client('ssm')
    print ("Hello world")
    resp = client.send_command(
        DocumentName="AWS-RunShellScript",
        # Would normally pass commands param here but hardcoding it instead for testing
        Parameters={"commands":["ls"]},
        InstanceIds=["i-01112223333"],
    )
    return resp

However when I run "Test" on this function I get the following log output:

START RequestId: d028de04-4004-4a0p-c8d2-975755c84777 Version: $LATEST
Hello world
[ERROR] Runtime.MarshalError: Unable to marshal response: datetime.datetime(2020, 5, 12, 19, 34, 36, 349000, tzinfo=tzlocal()) is not JSON serializable
END RequestId: d028de04-4asdd4-4a0f-b8d2-9asd847813
REPORT RequestId: d42asd04-44d4-4a0f-b9d2-275755c6557   Duration: 1447.76 ms    Billed Duration: 1500 ms    Memory Size: 128 MB Max Memory Used: 76 MB  Init Duration: 301.68 ms

I am not sure what this Runtime.MarshalError: Unable to marshal response error means or how to fix it.

The payload I'm passing to run the Test lambda shouldn't matter since I'm not using the commands parameter but it is:

{
  "command": "ls"
}

Any help would be appreciated

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try the following:

return json.loads(json.dumps(resp, default=str))

This will dump resp as JSON and use the str function for datetime serialization. Then it restores back the same object but instead of datetime object you will get strings that can be serialized.

over 4 years ago · Santiago Trujillo Relatório

0

The error has pretty clear wording, you're just fixating on wrong part.

I assume you use the resp object somewhere down the line and that part tries to do something like json.load() or related.

datetime.datetime(2020, 5, 12, 19, 34, 36, 349000, tzinfo=tzlocal()) is not JSON serializable

This is a common error for people running into datetime for the first time, here's pretty comprehensive question about it: How to overcome "datetime.datetime not JSON serializable"?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda