28

Anyone has any Fabric recipe that shows how to connect to EC2 using the pem file?

I tried writing it with this manner: Python Fabric run command returns "binascii.Error: Incorrect padding"

But I'm faced with some encoding issue, when I execute the run() function.

Community
  • 1
  • 1
Mickey Cheong
  • 2,980
  • 6
  • 38
  • 50

3 Answers3

42

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
gak
  • 32,061
  • 28
  • 119
  • 154
35

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

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
11

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

Community
  • 1
  • 1
yixu34
  • 119
  • 1
  • 3