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

Question:

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.

Asked By: Mickey Cheong

||

Answers:

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

Answered By: Jeff Bauer

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
Answered By: gak

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

Answered By: yixu34
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.