2

I've been trying to read a config json in my python AWS lambda package (I don't want to use the config in the console as I generate it with my code and it has odd properties), I zip it with everything else. I get the following error:

{"errorMessage": "unknown url type: '/var/taskconstants/aws.json'", "errorType": "ValueError", "stackTrace": [ " File \"/var/lang/lib/python3.7/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n", " File \"/var/lang/lib/python3.7/imp.py\", line 171, in load_source\n module = _load(spec)\n", " File \"\", line 696, in _load\n", " File \"\", line 677, in _load_unlocked\n", " File \"\", line 728, in exec_module\n", " File \"\", line 219, in _call_with_frames_removed\n", " File \"/var/task/lambda_function.py\", line 13, in \n AWS_JSON = json.load(urllib.request.urlopen(os.getcwd()+\"constants/aws.json\"))\n", " File \"/var/lang/lib/python3.7/urllib/request.py\", line 222, in urlopen\n return opener.open(url, data, timeout)\n", " File \"/var/lang/lib/python3.7/urllib/request.py\", line 510, in open\n req = Request(fullurl, data)\n", " File \"/var/lang/lib/python3.7/urllib/request.py\", line 328, in init\n self.full_url = url\n", " File \"/var/lang/lib/python3.7/urllib/request.py\", line 354, in full_url\n self._parse()\n", " File \"/var/lang/lib/python3.7/urllib/request.py\", line 383, in _parse\n raise ValueError(\"unknown url type: %r\" % self.full_url)\n" ] }

I've tried the solution from here and I got the above. The same thing happens with our without, it's just a different url to get an error on. Here's the relevant code

import json, random, boto3, os
import urllib.request
# Loading constants from aws json
AWS_JSON = json.load(urllib.request.urlopen(os.getcwd()+"constants/aws.json"))

What am I missing?

GileBrt
  • 1,830
  • 3
  • 20
  • 28
Sidhanth Tuli
  • 21
  • 1
  • 4
  • It might be trivial, but the error says: unknown url type: '/var/taskconstants/aws.json whilst the code statement is json.load(urllib.request.urlopen(os.getcwd()+"constants/aws.json")). I am suspecting there is a missing '/' in your code before constants/aws.json – Ujjwal Bhardwaj Mar 23 '19 at 18:55
  • Thanks for pointing that out. I tried it but it didn't seem to fix the problem. I get the same error but with a new url with just the slash added. Thanks for the help! – Sidhanth Tuli Mar 26 '19 at 00:34

1 Answers1

0

Try this: The application code unpacks to the directory /var/task

import json

with open('/var/task/constants/aws.json') as json_file:  
    data = json.load(json_file)
Til
  • 5,150
  • 13
  • 26
  • 34
Joseph
  • 512
  • 2
  • 5
  • Sorry for the late reply but I did try this before asking the question. I'm not sure if maybe it's something I did wrong elsewhere in my code but regardless I no longer use a constants file to read from but just paste the constants at the top with a bash script I use to package my lambda zip. – Sidhanth Tuli Apr 28 '19 at 18:19