McDermottroe.com

Posts tagged "laziness"

SFTP to EC2 with Python and Boto

Monday, November 14th, 2011

Today I wanted to automate the upload of some code to a new Amazon EC2 instance. I’ve been scripting the rest of the job using Boto but when I was lazily looking for an example of how to do SFTP with Boto there wasn’t anything obvious in the first few pages of Google’s results. So, here’s the snippet for any other lazy coders out there:

import boto.manage.cmdshell
 
def upload_file(instance, key, username, local_filepath, remote_filepath):
    """
    Upload a file to a remote directory using SFTP. All parameters except for
    "instance" are strings. The instance parameter should be a
    boto.ec2.instance.Instance object.
 
    instance        An EC2 instance to upload the files to.
    key             The file path for a valid SSH key which can be used to log
                    in to the EC2 machine.
    username        The username to log in as.
    local_filepath  The path to the file to upload.
    remote_filepath The path where the file should be uploaded to.
    """
    ssh_client = boto.manage.cmdshell.sshclient_from_instance(
        instance,
        key,
        user_name=username
    )
    ssh_client.put_file(local_filepath, remote_filepath)

Boto depends on paramiko to handle the SSH parts, so you’ll need that installed too.

Blogging

Tuesday, March 10th, 2009

According to Larry Wall, there are three great virtues of a programmer: laziness, impatience and hubris. I seem to have a surplus of the first and so have resisted blogging for a long time. Maybe my impatience and hubris have overtaken the laziness.

This may be the first of many blog posts or I may give up after a few. Either way, enjoy it while it lasts.