pythonamazon-web-servicesaws-lambdagoogle-cloud-pubsub

AWS Lambda unable to work with \n as string


I have a private key from GCP Pub Sub. It is stored like this

secret_manager_data = {'google_pubsub_private_key':'-----BEGIN PRIVATE KEY-----\nYourPrivateKeyHere\n-----END PRIVATE KEY-----\n'}

I'm now parsing the private key in the below code

signer = crypt.Signer.from_string(secret_manager_data['google_pubsub_private_key'])

Now I am getting an error like "No key could be detected"

But when I give string directly, then it is working. Example:

signer = crypt.Signer.from_string('-----BEGIN PRIVATE KEY-----\nYourPrivateKeyHere\n-----END PRIVATE KEY-----\n')

Means no issue in input but don't know how to handle \n in Lambda Function. Lambda is taking \n as next line instead of string.

Can you please help me?


Solution

  • It looks like the \n newlines in the source event are being escaped to \\n before presentation to your Lambda function.

    One way to deal with this is to unescape them, as follows:

    mystr.replace("\\n", "\n"))
    

    Example:

    >>> print("fake\\nnews\\nhere".replace("\\n", "\n"))
    fake
    news
    here