code: def ec2_ssh(ip_address,
              keypem_file,
              username='ec2-user',
              raiseonfail=False):
      
  
      c = paramiko.client.SSHClient()
      c.load_system_host_keys()
      c.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
  
      # load the private key from the AWS keypair pem
      privatekey = paramiko.RSAKey.from_private_key_file(keypem_file)
  
      # connect to the server
      try:
  
          c.connect(ip_address,
                    pkey=privatekey,
                    username='ec2-user')
  
          return c
  
      except Exception as e:
          LOGEXCEPTION('could not connect to EC2 instance at %s '
                       'using keyfile: %s and user: %s' %
                       (ip_address, keypem_file, username))
          if raiseonfail:
              raise
  
          return None
docs: This opens an SSH connection to the EC2 instance at `ip_address`.
  
      Parameters
      ----------
  
      ip_address : str
          IP address of the AWS EC2 instance to connect to.
  
      keypem_file : str
          The path to the keypair PEM file generated by AWS to allow SSH
          connections.
  
      username : str
          The username to use to login to the EC2 instance.
  
      raiseonfail : bool
          If True, will re-raise whatever Exception caused the operation to fail
          and break out immediately.
  
      Returns
      -------
  
      paramiko.SSHClient
          This has all the usual `paramiko` functionality:
  
          - Use `SSHClient.exec_command(command, environment=None)` to exec a
            shell command.
  
          - Use `SSHClient.open_sftp()` to get a `SFTPClient` for the server. Then
            call SFTPClient.get() and .put() to copy files from and to the server.