title: How to download a file from a url using AWS lambda with node.js code
body: <p>I am having a hard time to write code for my lambda function.
  This function has to download files into /tmp directory but instead of doing that it creates files with 0 size. The output looks like that:</p>
  <p>2020-07-13T21:28:09.694-05:00
  2020-07-14T02:28:09.681Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO /tmp/7za123.zip</p>
  <p>2020-07-13T21:28:09.994-05:00
  2020-07-14T02:28:09.975Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO /tmp/7za321.zip</p>
  <p>2020-07-13T21:28:10.014-05:00
  2020-07-14T02:28:09.995Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO /tmp/7za920.zip</p>
  <p>2020-07-13T21:28:10.016-05:00
  2020-07-14T02:28:10.016Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO 0</p>
  <p>2020-07-13T21:28:10.017-05:00
  2020-07-14T02:28:10.017Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO 0</p>
  <p>2020-07-13T21:28:10.017-05:00
  2020-07-14T02:28:10.017Z a914d6fa-0b29-4c66-8cc5-95095575be7b INFO 0</p>
  <p>Could you help me to fix it?</p>
  <pre><code>      var AWS = require('aws-sdk')
        var response = require('cfn-response')
        var fs = require('fs')
        var path = require('path')
        var https = require('https')
        var urlmod = require('url')
        var {promisify} = require('util')
        var stream = require('stream')
        var finished = promisify(stream.finished)
        var s3 = new AWS.S3()
  
        function getFilesizeInBytes(filename) {
          var stats = fs.statSync(filename)
          var fileSizeInBytes = stats[&quot;size&quot;]
          return fileSizeInBytes
        }
  
        download = async function (filename, url) {
          console.log(filename)
          file = fs.createWriteStream(filename)
          request = https.get(url, function(resp) {
            resp.pipe(file)
          })
          await finished(file)
        }
  
  
        exports.lambda_handler = (event, context, callback) =&gt; {
          var s3buketname = event.ResourceProperties.S3BucketName
          var urls = event.ResourceProperties.Urls
  
          for(let url of urls) {
            filename = '/tmp/' + path.basename(urlmod.parse(url, true).pathname)
            download(filename, url)
          }
  
          for(let url of urls) {
            filename = '/tmp/' + path.basename(urlmod.parse(url, true).pathname)
            console.log(getFilesizeInBytes(filename))
          }
  
          responseData = {Result: &quot;good&quot;}
          response.send(event, context, response.SUCCESS, responseData)
        }
  </code></pre>