Skip to content

Instantly share code, notes, and snippets.

@muhammadfaizan
Last active January 15, 2019 13:56
Show Gist options
  • Save muhammadfaizan/eca1c669dc0db901223e2614ec655157 to your computer and use it in GitHub Desktop.
Save muhammadfaizan/eca1c669dc0db901223e2614ec655157 to your computer and use it in GitHub Desktop.
Custom AWS Deployments using EC2

Custom AWS Deployments using EC2

In this talk, Mashhood share how we can configure "git push deployments". Using Git and EC2 we can quickly configure a deployment for our selves with minimal efforts. Then he also shares the deployment for S3 alongside this. Watch it on Youtube

Deployment Flow

  1. Create EC2 instance.
  2. Configure SSH alias
  3. Setup the instance
  4. Setup a bare git
  5. Configure the post receive hook
  6. Add server as a remote
  7. Push code to the server

Get a MEAN Stack app

We are taking a sample MEAN Stack app, its a basic scaffolding. Code available here

The app contains files like this:

MEAN App Screenshot

1. Create an EC2 Instance

Create your EC2 instance on AWS. We are using t3.micro instance for this example.

Creating AWS EC2

2. Configure SSH alias

Run this command and then paste your ssh configuration that you can use to login to your server.

> tail -n 6 ~/.ssh/config
Host aws_demo
	Hostname <EC2 instance IP>
	Port 22
	User ubuntu
	IdentityFile ~/.ssh/<ec2_instance_key>.pem
	IdentitiesOnly yes

3. Setup Instance

Now you can ssh into your server with following command

> ssh aws_demo

4. Setup a bare git

Run following command on your server after ssh into it from command in step 3.

> git init --bare mean.git
> git init --bare <folder_name>

--bare means its a landing part for your code, your code will be pushed here. This repository will have no code at the moment. Naming this git as mean.git

You can ls into this directory and see what it responds. You will see folders like these

> ls mean.git/
> HEAD  branches config  description  hooks  info  objects  refs

Now create a folder to keep your code in. so create a folder for it and it will be configured later.

> mkdir mean
> ls
mean  mean.git

5. Configure the post receive hook

Post receive hooks actually means a script that will run after it has received the code

> cd mean.git/hooks
> nano post-receive

We will be using nano as text editor, you can use your preferred editor. paste the following script in your file.

#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/mean
NPM_PACKAGES="${HOME}/.npm-packages"
PATH="$NPM_PACKAGES/bin:$PATH"
export GIT_WORK_TREE
git checkout -f
cd $GIT_WORK_TREE
./deploy.sh

This file will every time I push code in mean repository. But first we need to give it executable access on linux.

From mean.git/hooks directory run

> chmod +x post-receive

Now go back to home > cd ../../

At this point our repository is setup and our mean repository is empty.

6. Add server as remote

Now you need to push your code to your server for that switch to your development environment or local environment add a remote to your git repository. You can add a remote with following command.

> git remote add deployment aws_demo:/home/ubuntu/mean.git

Now your remote has been added.

> git push deployment +master:refs/heads/master

The above command is special it pushes all the references to the server and creates a master branch, as we already know that it doesn't exist as of now on our EC2 server. After pushing you will notice that it will be doing interesting stuff, it will run > yarn and then > npm run build

These commands are specified in your deploy.sh files in your code. make sure you have commented the command intended to restart forever

7. Push the code to server

After everything is setup from your local environment, uncomment the line by removing #

forever restart 0

Your deployment is done, all you have to do is to run the server, so go back to your server terminal and since now it has installed all the required modules and created a build you can run forever from your server now.

ON SERVER: CD into mean directory

> cd mean

Skip this step if you are already in mean directory

create a .env file on the folder which contains your code, in our example its mean so go ahead and create a file

> nano .env

Paste your environment variables

NODE_ENV=<ENVIRONMENT_HERE>
SERVER_PORT=<PORT>
JWT_SECRET=<JUST_A_SECRET_KEY>
MONGO_HOST=<MONGODB_URL>
MEAN_FRONTEND=angular

Your environment variable can be absolutely different from what I mentioned above. So figure what you need in your code and create environment variable respectively.

Start Node.js server with forever

> forver start server/index.js

Now your server has started you can visit your web on public IP of your server with the port you mentioned in your environment. For this example it was: 18.221.57.216:3000 Visit your web and hoala it will work.

Working example

NOT SO FAST...

It was an automated deployment remember? so change something back in your code, something very visible for testing purpose.

now commit change and push it to deployment server

> git add .
> git commit -m "some visible changes to see everything went well"
> git push deployment master
  • This will push the code to your deployment server.
  • Yarn install every dependency on your server
  • Create a build for your app
  • Restart the server

Just by pushing your code you had your very own automated deployment on EC2 Instance.

Hope it helped. Dont forget to hit ⭐

Special thanks to Mashhood Rastagar @mashhoodr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment