title: Updating Custom Resource backed lambda in Cloudformation Template from 6.10 to 8.10 no longer sending request
body: <p>We provide a testdrive of our software to our customers using a cloudformation template to spin up an AWS AMI.  Recently I added a custom resource to this template that generates a trial license key to use during the test drive and saves a one of the cloudformation outputs. The license is generated by running a lambda zip file stored on S3.  This has been working first in node4.10, then in node6.10 but  now that I've moved to node8.10 the step to wait on a response from the parsedUrl is ending before it receives the response.  As there is no response returned to cloudformation, the creation if this resource waits for 60mins then signals the delete stack command.</p>
  
  <p>I believe the problem is with the async nature of nodejs and my lack of understanding on how to convert the code.</p>
  
  <pre><code>exports.handler = (event, context) =&gt; {
  
  </code></pre>
  
  <pre><code>return genKey("./lic_wrp.sh", [arg1]).then(function(resp) {
      responseStatus = "SUCCESS";
      responseData["trialkey"] = resp;
      console.log ('Here is the response', responseData);
  
      sendResponse(event, context, responseStatus, responseData);
    }
  );
  </code></pre>
  
  <p>That snippet was the original node6.10 working version.
  I have changed to:</p>
  
  <pre><code>exports.handler = async (event,context) =&gt; {
  </code></pre>
  
  <p>and</p>
  
  <pre><code>try {
    var resp = await helpers.genKey("./lic_wrp.sh", [arg1]);
    responseStatus = "SUCCESS";
    responseData["trialkey"] = resp;
    console.log ('Here is the response', responseData);
    try {
      const rest2 = await sendResponse(event,  context, responseStatus, responseData)
      return rest2;
    } catch (e){
      console.log(e);
    }
  } catch (e){
    console.log(e);
  }
  </code></pre>
  
  <p>The sendResponse code has remained the same and I have used the example from the AWS site:</p>
  
  <pre><code>function sendResponse(event, context, responseStatus, responseData) {
  
      var responseBody = JSON.stringify({
          StackId: event.StackId,
          RequestId: event.RequestId,
          LogicalResourceId: event.LogicalResourceId,
          PhysicalResourceId: context.logStreamName,
          Status: responseStatus,
          Reason: "See the details in CloudWatch Log " + context.logStreamName,
          Data: responseData
      });
  
      console.log("RESPONSE BODY:\n", responseBody);
  
      var https = require("https");
      var url = require("url");
  
      var parsedUrl = url.parse(event.ResponseURL);
      var options = {
          hostname: parsedUrl.hostname,
          port: 443,
          path: parsedUrl.path,
          method: "PUT",
          headers: {
              "content-type": "",
              "content-length": responseBody.length
          }
      };
  
      console.log("SENDING RESPONSE...\n");
  
      var request = https.request(options, function(response) {
          console.log("STATUS: " + response.statusCode);
          console.log("HEADERS: " + JSON.stringify(response.headers));
          // Tell AWS Lambda that the function execution is done
          context.done();
      });
  
      request.on("error", function(error) {
          console.log("sendResponse Error:\n", error);
          // Tell AWS Lambda that the function execution is done
          context.done(error);
      });
  
    request.write(responseBody);
    request.end();
  }
  </code></pre>
  
  <p>In cloudwatch I see the responseBody remains fine.</p>
  
  <pre><code>2019-05-21T05:02:50.450Z    ba8542ed-66cf-4ada-9ae6-fd4f1920faee    RESPONSE BODY:
  {
      "StackId": "arn:aws:cloudformation:ap-southeast-2:267176467350:stack/laatest52/9c35f740-7b85-11e9-9ba3-0217b90f9ac8",
      "RequestId": "1eafeb08-fadc-4bc5-a476-19330599ad6d",
      "LogicalResourceId": "LicenseKey",
      "PhysicalResourceId": "2019/05/21/[$LATEST]e2cc667849d44285b452af5fa31084b2",
      "Status": "SUCCESS",
      "Reason": "See the details in CloudWatch Log 2019/05/21/[$LATEST]e2cc667849d44285b452af5fa31084b2",
      "Data": {
          "trialkey": "xxxx-xxxx-xxxx-xxxx-xxxx" -- Our key gen is working
      }
  }
  </code></pre>
  
  <p>Then I see sending response.
  But then the next line is:
  <code>END RequestId: ba8542ed-66cf-4ada-9ae6-fd4f1920faee</code></p>
  
  <p>In the previous version that worked fine.
  We see, the response body , then sending response then:
  <code>2018-11-16T18:03:06.435Z   dd879acf-e9c9-11e8-afaf-bfffb4a386b6    STATUS: 200</code></p>
  
  <p>And the resource is created.</p>