title: Invoke an SSM document from Lambda - NodeJS - Invalid Instance ID error
body: <p>I am trying to invoke an SSM document from AWS Lambda. Here is the lambda function for the same:</p>
  <pre><code>const AWS = require('aws-sdk');
  const ssm = new AWS.SSM();
  const SSM_DOCUMENT_NAME = process.env.SSM_DOCUMENT_NAME;
  AWS.config.update({region: 'ap-south-1'});
  
  const sendCommand = (instance_id) =&gt; {
   
      var params = {
          DocumentName: SSM_DOCUMENT_NAME,
          InstanceIds: [instance_id],
          TimeoutSeconds: 300
      };
      console.log(&quot;before call:&quot;+params.InstanceIds);
      return ssm.sendCommand(params).promise();
   
  }
  exports.handler = async (event) =&gt; {
      console.log('event ', JSON.stringify(event));
      try {
          
              const instance_id = event.detail.EC2InstanceId;
              console.log(&quot;processing autoscaling event for &quot;+instance_id);
          
              await sendCommand(instance_id);
              console.log('sent command');
          
      } catch (error) {
          throw error;
      }
  }
  </code></pre>
  <p>This is throwing the following exception after logging the line &quot;before call: &quot;. The instance already exists for a long time and is not recently created, so no consistency issues.</p>
  <pre><code>Invoke Error    
  {
      &quot;errorType&quot;: &quot;InvalidInstanceId&quot;,
      &quot;errorMessage&quot;: null,
      &quot;code&quot;: &quot;InvalidInstanceId&quot;,
      &quot;message&quot;: null,
      &quot;time&quot;: &quot;2020-06-19T09:43:10.348Z&quot;,
      &quot;requestId&quot;: &quot;150decf1-be1d-407f-9a86-a4f986c04807&quot;,
      &quot;statusCode&quot;: 400,
      &quot;retryable&quot;: false,
      &quot;retryDelay&quot;: 20.23844834854607,
      &quot;stack&quot;: [
          &quot;InvalidInstanceId: null&quot;,
          &quot;    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)&quot;,
          &quot;    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)&quot;,
          &quot;    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)&quot;,
          &quot;    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)&quot;,
          &quot;    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)&quot;,
          &quot;    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)&quot;,
          &quot;    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10&quot;,
          &quot;    at Request.&lt;anonymous&gt; (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)&quot;,
          &quot;    at Request.&lt;anonymous&gt; (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)&quot;,
          &quot;    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)&quot;
      ]
  }
  </code></pre>
  <p>The function works fine while testing from lambda. The error occurs when I run the complete setup. Here is the flow of what I am trying to achieve:</p>
  <p>Instance termination &gt; Lifecycle hook &gt; Cloudwatch event &gt; Lambda &gt; SSM run Command. Based on my understanding the only difference between testing from lambda and the real time setup is that while testing the instance is in running state and the final run it is in termination:wait state. I have also increased the wait time to 600 seconds but no luck.</p>
  <p>Additional observation: In auto scaling groups &gt; activity - I can a message saying waiting for lifecycle event. However as soon as i terminate the instance it vanishes from the list of manages instances in Systems Manager tab. Hence by the time lambda is triggered, the instanceid is not available to Systems Manager.</p>
  <p>Observation/solution 2: I was testing it wrong. The life-cycle hook works only if the instance is deemed unhealthy by the scaling policy. terminating the instance manually does not work. Now that I write it down it seems obvious.</p>