How to deploy your local project to a remote AWS EC2 server instance via a post-update githook
Often when working on a non-production project locally I will decide I want to use it remotely on an EC2 instance. Maybe it’s a daily cron job, python scraping scripts, or even backtests running on a model that I would like to be hosted remotely. Whatever the purpose, I usually want a quick way to set up a remote .git repository on my EC2 instance that I can push to, and the following are the steps you can take to get that up and running too. It’s not as scalable as Heroku’s or Elastic Beanstalk’s version control, but it’s great for smaller projects not in production and it’s easier to set up.
1. SSH into your EC2 instance
SSH into your EC2 instance using the standard ssh terminal command with your
.pem file location, username, and server’s
ssh -i /path/my-key-pair.pem my-instance-user-name@my-instance-public-dns-name
2. Create a directory for your project and
cd into it
Use the same directory name as your local
project_name . This is where your project will live on your EC2 instance.
mkdir project_namecd project_name
3. Create a subdirectory,
cd into it, and initialize a bare git repository
This is the
.git repository which will live in a subdirectory of your
project_name directory and contains the githook that we’ll be using to tie in to our local git repository.
mkdir project_name.gitcd project_name.gitgit init --bare
cd into the
hooks/ directory and activate the
post-update.sample githook file by renaming it, truncating the
You’ll see lots of other files in the bare git repository as well, but we only need to rename the
post-update.sample file for our purposes.
cd hooks/mv post-update.sample post-update
5. Modify the contents of the renamed post-update file
Next we’ll tell the githook where to save the files that are pushed to it from our local repository in a variable named
and replace the uncommented line so that the final result is:
# An example hook script to prepare a packed repository for use over
# dumb transports.
# To enable this hook, rename this file to "post-update".
GIT_WORK_TREE=/home/ec2-user/project_name/ git checkout -f
Save this file and log out of your EC2 instance with
6. Add the EC2 git repository as a git remote to your local project
I usually have my project already set up with my Github repo as the push/pull remote, so all that’s needed is to add the EC2 instance as another push remote. I usually just use the standard origin/master branch.
cd ~/path_to_your_projectgit remote set-url --add origin ec2-user@ec2-XX-XXX-XXX-XXX.us-west-2.compute.amazonaws.com:/home/ec2-user/project_name/project_name.git
So now if you enter the terminal command
git remote -v it should return something like this:
origin https://github.com/username/project_name (fetch)
origin https://github.com/username/project_name (push)
origin email@example.com:/home/ec2-user/project_name/project_name.git (push)
7. All done!
Now try pushing your project to your EC2 instance with a quick
git push so you can easily use and update your local project on your remote server. You should be able to
ssh into your instance again to verify the project is now hosted there so you can run its contents remotely.
If you liked this, follow me on Twitter @elitwilliams.