title: How to run python script on EC2 instance by boto3
body: <p>I read this question before <a href="https://stackoverflow.com/questions/42645196/how-to-ssh-and-run-commands-in-ec2-using-boto3">How to SSH and run commands in EC2 using boto3?</a>. Many answers just said users don't have to use <code>ssh</code> to connect to EC2 and run command. However, I still don't have a clue how to run a python script by boto3. In boto2, this is a function <code>run_instances</code> which user can pass their script into EC2 node and run it, just like the code list as below</p>
  
  <pre><code>def run(self, **kwargs):
      ec2 = boto.connect_ec2(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET)
      sqs = boto.connect_sqs(settings.PDF_AWS_KEY, settings.PDF_AWS_SECRET)
  
      queue = sqs.create_queue(REQUEST_QUEUE)
      num = queue.count()
      launched = 0
      icount = 0
  
      reservations = ec2.get_all_instances()
      for reservation in reservations:
          for instance in reservation.instances:
              if instance.state == "running" and instance.image_id == AMI_ID:
                  icount += 1
      to_boot = min(num - icount, MAX_INSTANCES)
  
      if to_boot &gt; 0:
          startup = BOOTSTRAP_SCRIPT % {
              'KEY': settings.PDF_AWS_KEY,
              'SECRET': settings.PDF_AWS_SECRET,
              'RESPONSE_QUEUE': RESPONSE_QUEUE,
              'REQUEST_QUEUE': REQUEST_QUEUE}
          r = ec2.run_instances(
              image_id=AMI_ID,
              min_count=to_boot,
              max_count=to_boot,
              key_name=KEYPAIR,
              security_groups=SECURITY_GROUPS,
              user_data=startup)
          launched = len(r.instances)
      return launched
  </code></pre>
  
  <p><code>BOOTSTRAP_SCRIPT is a python script</code></p>
  
  <p>I write some code with boto3:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  SCRIPT_TORUN = """
  
  import boto3
  
  bucket = random_str()
  image_name = random_str()
  s3 = boto3.client('s3')
  Somedata = 'hello,update'
  
  upload_path = 'test/' + image_name
  s3.put_object(Body=Somedata, Bucket='cloudcomputing.assignment.storage', Key=upload_path)
  
  """
  
  import boto3
  running_instance = []
  ec2 = boto3.resource('ec2')
  
  for instance in ec2.instances.all():
      if instance.state['Name'] == 'running':         # Choose running instance and save their instance_id
          running_instance.append(instance.id)
          print instance.id, instance.state
  print running_instance
  </code></pre>
  
  <p>I can get the details of running instances, can anybody tell me is there a function like <code>run_instances</code> in <code>boto3</code>, which I can use to run the script <code>SCRIPT_TORUN</code> in one of my running EC2 instances.</p>