var https = require('https');
var qs = require('querystring');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

var config = {
  untappdUrl: 'https://api.untappd.com/v4/thepub/local',
  untappdClientId: '',
  untappdClientSecret: '',
  bucketName: ''
};

exports.handler = function(event, context) {
  /*console.log('Received request:', JSON.stringify(event, null, 2));*/

  var httpQueryParams = {
    client_id: config.untappdClientId,
    client_secret: config.untappdClientSecret,
    lat: 61.498171,
    lng: 23.762101,
    dist_pref: 'km',
    radius: 2
  };
  var httpOptions = {
    host: 'api.untappd.com',
    path: '/v4/thepub/local'+'?'+qs.stringify(httpQueryParams)
  };

  https.get(httpOptions, function(res) {
    if (res.statusCode != 200) {
      context.fail('Received non-ok status code from Untappd: ', res.statusCode);
    }

    var body = '';
    res.on('data', function(chunk) { body += chunk; });
    res.on('end', function() {

      var s3params = {
        'Bucket': config.bucketName,
        'Key': 'data/thepub.json',
        'ContentType': 'application/json',
        'Body': body
      };

      s3.putObject(s3params, function(err, data) {
        if (err) {
          console.log(err, err.stack);
          context.fail('Error while uploading to S3');
        }
        else {
          context.succeed('Successfully updated data');
        }
      });
    });

  }).on('error', function(e) {
    console.log(e.message);
    context.fail('Failed to get data from Untappd');
  });

};

@@ -301,80 +301,8 @@
 t) %7B
-%0A  /*console.log('Received request:', JSON.stringify(event, null, 2));*/
 %0A%0A  
@@ -778,117 +778,8 @@
  %7D%0A%0A
-    var body = '';%0A    res.on('data', function(chunk) %7B body += chunk; %7D);%0A    res.on('end', function() %7B%0A%0A  
     
@@ -795,18 +795,16 @@
 ams = %7B%0A
-  
       'B
@@ -836,18 +836,16 @@
 ,%0A      
- 
- 
 'Key': '
@@ -861,24 +861,76 @@
 ub.json',%0A  
+    'ContentLength': res.headers%5B'content-length'%5D,%0A
       'Conte
@@ -968,18 +968,16 @@
     
-  
 'Body': 
 body
@@ -976,15 +976,12 @@
 y': 
-body%0A  
+res%0A
     
@@ -992,22 +992,17 @@
     
- 
- 
 s3.
-putObject
+upload
 (s3p
@@ -1026,26 +1026,24 @@
 rr, data) %7B%0A
-  
       if (er
@@ -1055,18 +1055,16 @@
         
-  
 console.
@@ -1084,34 +1084,32 @@
 stack);%0A        
- 
- 
 context.fail('Er
@@ -1139,30 +1139,26 @@
 S3');%0A      
-  %7D%0A  
+%7D%0A
       else %7B
@@ -1162,26 +1162,24 @@
 e %7B%0A        
- 
- 
 context.succ
@@ -1222,21 +1222,9 @@
     
-  %7D%0A      %7D);
+%7D
 %0A   

Use streaming upload instead of buffering whole response in lambda