def ec2_ssh(ip_address,<EOL>keypem_file,<EOL>username='<STR_LIT>',<EOL>raiseonfail=False):
c = paramiko.client.SSHClient()<EOL>c.load_system_host_keys()<EOL>c.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)<EOL>privatekey = paramiko.RSAKey.from_private_key_file(keypem_file)<EOL>try:<EOL><INDENT>c.connect(ip_address,<EOL>pkey=privatekey,<EOL>username='<STR_LIT>')<EOL>return c<EOL><DEDENT>except Exception as e:<EOL><INDENT>LOGEXCEPTION('<STR_LIT>'<EOL>'<STR_LIT>' %<EOL>(ip_address, keypem_file, username))<EOL>if raiseonfail:<EOL><INDENT>raise<EOL><DEDENT>return None<EOL><DEDENT>
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.