title: AWS lambda URL request and write binary to /tmp/
body: <p>I'm using API gateway + lambda function integration and Python 3.8. I need to recieve a post from an external webhook. The webhook sends a JSON object with some data and a url. I'm trying to use requests lib to get a pdf file from the url and write in /tmp/ dir. I already have uploaded requests lib into lambda from a zip and works fine. I'm further planning to upload the pdf into S3.</p>
  
  <p>The problem: the requested file from URL are not being writen to /tmp/ dir.</p>
  
  <ul>
  <li>No error messages.</li>
  <li>Not a heavy file (&lt;10 mb).</li>
  </ul>
  
  <pre class="lang-py prettyprint-override"><code>import json
  import requests
  import os
  
  def lambda_handler(event, context):
      prods = event
      body = json.loads(prods["body"])
      url = body["url"]
  
      print("Body:", body)
  
      r = requests.get(url)
      content = r.content
  
      with open('/tmp/test.pdf', 'wb') as fd:
          fd.write(content)
  
      print(os.listdir("/tmp/"))
  
      return {
          'statusCode': 200,
          'body': {
              'file_path': '/tmp/test.pdf'
          }
      }
  </code></pre>
  
  <p>I already tryied to chunk the file using the following strategy:</p>
  
  <pre class="lang-py prettyprint-override"><code>url = 'http://www.example.com'
  r = requests.get(url, stream=True)
  
  with open('/tmp/test.pdf', 'wb') as fd:
      for chunk in r.iter_content(chunk_size):
          fd.write(chunk)
  </code></pre>
  
  <p>Edit: seems to be a 'time out' question. Saw this on CloudWatch:
  <strong>Task timed out after 3.00 seconds</strong></p>