title: How to Stop and Start EC2 Instance using boto3 and lambda Function
body: <p>I want to start and stop EC2 instances using Lambda Function</p>
  <p>I am able to start and stop EC2 instances using the Instance ID but How can I do the same for Instance name, I am trying to do this because my end-user doesn't know what is instance-id they are only aware of Instance name</p>
  <p>below is my code which is working fine for instance ID</p>
  <pre><code>import json
  import boto3
  
  region = 'us-east-1'
  ec2 = boto3.client('ec2', region_name=region)
  
  def lambda_handler(event, context):
      instances = event[&quot;instances&quot;].split(',')
      action = event[&quot;action&quot;]
      
      if action == 'Start':
          print(&quot;STARTing your instances: &quot; + str(instances))
          ec2.start_instances(InstanceIds=instances)
          response = &quot;Successfully started instances: &quot; + str(instances)
      elif action == 'Stop':
          print(&quot;STOPping your instances: &quot; + str(instances))
          ec2.stop_instances(InstanceIds=instances)
          response = &quot;Successfully stopped instances: &quot; + str(instances)
      
      return {
          'statusCode': 200,
          'body': json.dumps(response)
      }
  </code></pre>
  <p>Events I am passing for stopping</p>
  <pre><code>{
    &quot;instances&quot;: &quot;i-0edb625f45fd4ae5e,i-0818263a2152a23bd,i-0cd2e17ba6f62f651&quot;,
    &quot;action&quot;: &quot;Stop&quot;
  }
  </code></pre>
  <p>Events I am passing for starting</p>
  <pre><code>{
    &quot;instances&quot;: &quot;i-0edb625f45fd4ae5e,i-0818263a2152a23bd,i-0cd2e17ba6f62f651&quot;,
    &quot;action&quot;: &quot;Start&quot;
  }
  </code></pre>