Skip to content

Instantly share code, notes, and snippets.

@jcolemorrison
Created January 16, 2020 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcolemorrison/ca732e73629726bad92adf0943a7546c to your computer and use it in GitHub Desktop.
Save jcolemorrison/ca732e73629726bad92adf0943a7546c to your computer and use it in GitHub Desktop.
EC2 Fundamentals - Automated Node.js Server Launch Custom User Data Script and Examples
# If your given node application has a different start command, replace the line in the script...
# rest of the userdata.sh script above....
# Start the server
node . > stdout.log 2> stderr.log
# with...
# rest of the userdata.sh script above....
# Start the server
yourcommand > stdout.log 2> stderr.log
### FULL EXAMPLE BELOW
### IF YOU USE THE BELOW, DO NOT INCLUDE ANYTHING FROM LINE 12 and ABOVE!!
#!/usr/bin/env bash
# Install Node and Git
yum update -y
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs git
# Make a directory to clone the application code to
mkdir -p /home/ec2-user/app && cd /home/ec2-user/app
git clone https://github.com/jcolemorrison/ec2-lb-api.git .
# Install Dependencies
npm install
# Redirect Port 3000 to Port 80 Traffic
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000
# Start the server
npm start > stdout.log 2> stderr.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment