Friday 22 June 2012

Setting up a remote git repository on Amazon EC2

This is a brief guide on how to setup your EC2 instance to act as a remote git repository server. It uses ssh to provide the server and authentication access and you will need an existing EC2 instance and the associated ssh keys (.pem file).

1. Install git on your EC2 instance

> ssh -i amazonkey.pem ec2-user@example.amazonaws.com
> sudo yum install git
amazonkey.pem should be the identity file provided when you setup your amazon EC2 instance.
example.amazonaws.com will be your EC2 instance hostname, something like ec2-111-111-111-111.ap-northeast-1.compute.amazonaws.com

2. Setup a git repository on your EC2 instance

From the directory /home/ec2-user
> mkdir test.git
> cd test.git
> git init --bare

3. Configure ssh so git can access your amazonkey.pem

On your local machine edit ~/.ssh/config and add the following :
Host amazon
Hostname example.amazonaws.com
User ec2-user
IdentityFile amazonkey.pem
Replace example.amazonaws.com with your EC2 instance name and replace amazonkey.pem with the location and name of your key file from amazon.
You can test it is working by sshing onto the instance with the command
> ssh amazon
It should successfully log you in to your EC2 instance at this point.

4. Create a local git repository and push it to your remote repository.

One the local machine
> mkdir test
> cd test
> git init
> echo "here is some text">text.txt
This creates a file called test.txt with some text in it so you have something to commit.
> git add *
> git commit -m "initial commit"
> git remote add origin amazon:/home/ec2-user/test.git
> git push -u origin master
At this point you should see some messages about git counting, compressing and writing objects. If there are any fatal errors at this point check that the git remote add origin path after amazon: matches where you created the empty git repository in step 2.

5. Checkout the changes to make sure they worked

On your local machine in an empty directory.
> git clone amazon:/home/ec2-user/test.git
> cd test
> cat test.txt
This should display the text you entered into the test.txt file in step 4 that has gone via the EC2 instance and git all the way back to your local machine.

References

http://stackoverflow.com/questions/4632749/how-to-push-to-git-on-ec2
http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/
http://dracoblue.net/dev/custom-identity-file-idrsapub-with-git-client/193/

6 comments:

  1. The post that helped me solve the problem setting a git repo on EC2. Very complete instructions! Thanks a lot!

    ReplyDelete
  2. Nice one. I found your post writing post explaining ssh config file in bit more details.

    ReplyDelete
  3. This a pretty good step by step tutorial. Thanks for that. Now just need to figure out how to add more users so our team can use the repo. :)

    ReplyDelete
  4. This tutorial was incomplete for me. When trying to push, authentication failed. I had to add my pem file to my ssh list, like so:
    ssh-add /path/to/amazonkey.pem

    ReplyDelete