title: Amazon Lambda - return docx file - Node.js
body: <p>I am trying to migrate my Node.js application from <strong>Google Cloud Functions</strong> to  <strong>Amazon Lambda</strong>. The application load docx file from Amazon S3, process it and return that docx file. However I got stuck on the process of file return. In Google Cloud Platform I could do this:</p>
  <pre><code>module.exports = function(customENV){ return function(req, res) {
      new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
          if(!err) { 
              res.set('Access-Control-Allow-Origin', &quot;*&quot;);
              res.set('Access-Control-Allow-Methods', 'GET, POST');
              res.set('Content-Disposition', `inline; filename=&quot;rename.docx&quot;`);
  
              res.type('docx');
              res.status(200);
  
              res.end(data.Body, 'binary');
          }
      });
  }};
  </code></pre>
  <p>In Amazon Lambda I replicated the solution like this:</p>
  <pre><code>exports.handler = function(event, context, callback) {
      new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
          if(!err) {
              var response = {
                  statusCode: 200,
                  headers: {
                      'Access-Control-Allow-Origin': &quot;*&quot;,
                      'Access-Control-Allow-Methods': 'GET, POST',
                      'Content-type' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                      'Content-Disposition': 'inline; filename=&quot;rename.docx&quot;'
                  },
                  isBase64Encoded: true,
                  body: data.Body,
              };
              
              callback(null, response);
          }
      });
  };
  
  </code></pre>
  <p>As API Gateway I used LAMBDA_PROXY with ANY method. Models/responses/mappings are all at &quot;default&quot;. However the only response I get is &quot;Internal server error&quot;. In CloudWatch logs I also see &quot;Execution failed due to configuration error: Unable to base64 decode the body&quot;.</p>
  <p>I tried replicating various solutions and/or different configurations of API Gateway, however with no luck.  Probably I just don't understand Amazon's API Gateway and therefore I don't know how to configure it properly.</p>
  <p>Perhaps as log says it could be also data conversion, but I tried conversions like <em>toString(&quot;base64&quot;)</em>, also no success.</p>
  <p>Any ideas what should I do to make this minimal solution working? Thank you!</p>