title: AWS Enabling CORS for my API triggering a Lambda
body: <p>I managed to create an AWS Lambda that does two things: writes on a dynamo DB and sends an SMS to a mobile number. Then I call this Lambda through a API Gateway POST call, and it works great from the Test section on AWS console but it gives error both on Postman and my own website. I inserted a callback to handle CORS on Lambda and deployed + enabled CORS on my API via console and deployed it but still get errors:</p>
  <p>Errors via postman call: {
  &quot;message&quot;: &quot;Internal server error&quot;
  }</p>
  <p>Errors via my website (jquery ajax POST call): Lambda calling failed: {&quot;readyState&quot;:4,&quot;responseText&quot;:&quot;{&quot;message&quot;: &quot;Internal server error&quot;}&quot;,&quot;responseJSON&quot;:{&quot;message&quot;:&quot;Internal server error&quot;},&quot;status&quot;:500,&quot;statusText&quot;:&quot;error&quot;}</p>
  <p>This is my lambda code</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const AWS = require('aws-sdk');
  const dynamodb = new AWS.DynamoDB();
  const SNS = new AWS.SNS();
  const tableName = "#####";  
  let params = {
                  PhoneNumber: 'mynumber####',
                  Message: 'Someone wrote!'
               };
  
  exports.handler = (event, context, callback) =&gt; {
  
      dynamodb.putItem({
          "TableName": tableName,
          "Item" : {
              "Id": {
                  N: event.Id
              },
              "Type": {
                  S: event.Type
              }
          }
      }, function(err, data) {
          if (err) {
              console.log('Error putting item into dynamodb failed: '+err);
          }
          else {
              console.log('Success in writing, now starting to send SMS');
              return new Promise((resolve, reject) =&gt; {
                  SNS.publish(params, function(err, data) {
                      if(err) {
                          console.log("Error in sending sms alarm");
                          reject(err);
                      }
                      else {
                          console.log("SMS alarm sent!");
                          resolve(data);
                      }
                  })
              })
          }
      });
      callback(null, {
          statusCode: 200,
          headers: {
              "Access-Control-Allow-Headers" : "Content-Type",
              "Access-Control-Allow-Origin": "*",
              "Access-Control-Allow-Methods": "OPTIONS,POST,GET"
          },
          body: JSON.stringify('Hello from Lambda!'),
      });
  };
  
      
      </code></pre>
  </div>
  </div>
  </p>
  <p>What am I doing wrong? I don't think permissions on lambda are the problem here, since testing it on console works both on writing on dynamo both sending the sms to my cellphone.</p>
  <p>If I click from here on API Endpoint I get the same error {&quot;message&quot;: &quot;Internal server error&quot;}
  <a href="https://i.stack.imgur.com/KuLWe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KuLWe.png" alt="enter image description here" /></a></p>