Issue passing a dictionary into Fargate Docker container

Viewed 44

I have a container that runs fine locally consisting of a simple python script that accepts a dictionary converted to a string as a system argument and prints out the key/value pairs:

calc.py:

import sys
import json
if __name__ == "__main__":
    print("Entered in main")
    print(f"Function name: {sys.argv[0]}")
    #load
    print(sys.argv[1])
    payload = json.loads(sys.argv[1])
    for k,v in payload.items():
        print(f"Key: {k},Value: {v}")

I build this as a docker container using an entrypoint with an environmental varible:

   ENTRYPOINT python3 calc.py "${LAMBDA_PAYLOAD}"

This allows me to run a test docker script of

docker run -e LAMBDA_PAYLOAD="{\"names\":\"J.J. Robichard\",\"Age\":25}" calc

which successfully prints out the key/value pairs.

When I put this into an AWS ECS Fargate task using boto3 and overriding the environmental variable by using:

'environment': [
      {
       'name': 'LAMBDA_PAYLOAD',
        'value': "{\"names\":[\"J.J. Robichard\",\"April\"],\"years\":[25,29]}"
       },
   ],

I get the print(sys.argv[1]) returns:

{"names":["J.J.

and everything breaks down. How do I get the task to accept a string with space? I've tried "\ " and f"""{}""", but nothing is working.

1 Answers

As a workaround, I was able to pass the dictionary through by encoding it as follows:

#inital dictionary
payload = {"names":"J.J. Robichard","April","years":[25,29]}
#converted to string
payload_s = json.dumps(payload).replace("\"","\\\"")
#this was input into the boto3 client as the value

on the other side, I had to do this:

input_1 = sys.argv[1]
payload = json.loads(input_1.replace("\\\"", "\""))
Related