Skip to content

Instantly share code, notes, and snippets.

@ivarprudnikov
Last active August 19, 2022 19:49
Show Gist options
  • Save ivarprudnikov/c3cc44e8f0f4165e5f4c to your computer and use it in GitHub Desktop.
Save ivarprudnikov/c3cc44e8f0f4165e5f4c to your computer and use it in GitHub Desktop.
Script to deploy node to elastic beanstalk
#!/bin/bash
# boto is missing dependency when using AWS tools to push to eb
# http://stackoverflow.com/questions/23365374/aws-aws-push-importerror-no-module-named-boto-in-ubuntu
sudo pip install boto
rm -rf eb_deployment
mkdir -p eb_deployment
cd eb_deployment
git init
git config --global user.email "[email protected]"
git config --global user.name "x y"
#git update-ref -d refs/heads/master
#git reset --hard
# Copy over prod files
rsync -av \
--exclude='eb_deployment' \
--exclude='test' \
--exclude='node_modules' \
--exclude='.git' \
--exclude='.idea' \
--exclude='.gitignore' \
.. .
# Change devDependencies to smth else so that aws does not install those
sed -i 's/devDependencies/customDependencies/' package.json
# Track and commit the working copy to the local repo
git add -A .
git commit -m "....."
# download AWS tools
curl -#O "https://s3.amazonaws.com/elasticbeanstalk/cli/AWS-ElasticBeanstalk-CLI-2.6.2.zip"
unzip "AWS-ElasticBeanstalk-CLI-2.6.2.zip"
# run "eb init"
AWS-ElasticBeanstalk-CLI-2.6.2/AWSDevTools/Linux/AWSDevTools-RepositorySetup.sh
# change AWS to support relative files in config
## find all files responsible for config parsing
## http://sebgoo.blogspot.ie/2013/09/elastic-beanstalk-deploy-from-different.html
## also note that sed required new line after "a\" command that is why I use -e $''
find . -type f -name "configfile_parser.py" -print0 | \
xargs -0 sed -i '' -e $'/read(self, pathfilename):/a\\\n'"pathfilename = os.path.realpath(os.path.expanduser(pathfilename))"
# overwrite generated .elasticbeanstalk folder
rsync -av ../.elasticbeanstalk .
# push to configured env (details in .elasticbeanstalk)
git aws.push
# Remove used files
cd ..
rm -rf eb_deployment
echo "****************************************************"
echo "******************* Deployed ***********************"
echo "****************************************************"
files:
/etc/nginx/conf.d/gzip.conf:
content: |
gzip_types text/html application/xhtml+xml text/xml application/xml text/plain text/json application/json text/css application/x-javascript text/javascript;
option_settings:
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: NodeCommand
value: npm start
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: NodeVersion
value: 0.10.10
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: GzipCompression
value: true
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: ProxyServer
value: nginx
- option_name: NODE_ENV
value: production
option_settings:
- namespace: aws:elasticbeanstalk:container:nodejs:staticfiles
option_name: client
value: /client
@ivarprudnikov
Copy link
Author

nginx.config, node.config, static.config should live in .ebextensions directory

@chonz0
Copy link

chonz0 commented Jul 15, 2020

If we want to just run npm start, EB should do it automatically.

But, if we want to run another command to start our Node app

    option_name: NodeCommand
    value: npm start

is the legacy way. I think the recommended way now would be using a Procfile in the project root with the following content:

web: npm run start

@ivarprudnikov
Copy link
Author

Yeah, I'd say it is out of date for sure.

@chonz0
Copy link

chonz0 commented Jul 15, 2020

Yeah, I'd say it is out of date for sure.

Still, great stuff, thanks!

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