Connecting to EC2 using keypair (.pem file) via Fabric

To use the pem file I generally add the pem to the ssh agent, then simply refer to the username and host:

ssh-add ~/.ssh/ec2key.pem
fab -H ubuntu@ec2-host deploy

or specify the env information (without the key) like the example you linked to:

env.user = 'ubuntu'
env.hosts = [
    'ec2-host'
]

and run as normal:

fab deploy

Without addressing your encoding issue, you might put your EC2 stuff into an ssh config file:

  • ~/.ssh/config

or, if global:

  • /etc/ssh_config

There you can specify your host, ip address, user, identify file, etc., so it's a simple matter of:

ssh myhost

Example:

Host myhost
  User ubuntu
  HostName 174.129.254.215
  IdentityFile ~/.ssh/mykey.pem

For more details: man ssh_config


Another thing you can do is set the key_filename in the env variable: https://stackoverflow.com/a/5327496/1729558

Tags:

Python

Fabric