Skip to content

Instantly share code, notes, and snippets.

@jamstooks
Last active May 30, 2023 06:17
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jamstooks/178408072a641381cbb636c3d1de1822 to your computer and use it in GitHub Desktop.
Notes on starting up an AWS Cloud9 Django dev environment with Python3

My AWS Cloud9 Setup for Python/Django and Node

Getting setup

sudo yum -y update

I'm not a big fan of their default bash prompt:

echo "export PS1='[\D{%F %T}]\n\[\e]0;\w\a\]\[\e[32m\]\u:\[\e[33m\]\w\[\e[0m\]\n\$ '" >> ~/.bashrc
source ~/.bashrc

This will look something like:

[2018-06-14 02:56:21]
ec2-user:~/environment/bt-backend
$ 

Open files from the command line

npm install -g c9

Then you can:

$ c9 file.txt

Connect with Github

git config --global user.email "email@example.com"
git config --global user.email

Gihub SSH instructions

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh -T git@github.com

Python/Django Setup

(OPTIONAL: remove alias python=python3 altogether in ~/.bashrc) - I haven't tried this in a while. YMMV

If you're on to Python3 these days, start by changing alias python=python27 to alias python=python3 in ~/.bashrc. Then upgrade pip and install virtualenvwrapper:

sudo pip-3.6 install --upgrade pip
sudo /usr/local/bin/pip install virtualenvwrapper
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc
echo "export WORKON_HOME=\$HOME/environment/Envs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

Now to create your virtualenvs... I usually just create one for my project, but sometimes I create multiple envs for testing, like p2, p3, or p2-dj1-10

mkvirtualenv -p /usr/bin/python3 p3
mkvirtualenv -p /usr/bin/python2.7 p2

I like to create a few Django shortcuts (djr is really the most important because of the env vars):

echo "alias dj='python manage.py'" >> ~/.bashrc
echo "alias djr='dj runserver \$IP:\$PORT'" >> ~/.bashrc
echo "alias djt='dj test'" >> ~/.bashrc
echo "alias djs='dj shell'" >> ~/.bashrc
source ~/.bashrc

Bonus: Code Auto-Formatting

I like the auto-format on save pattern. If you do too, you can:

sudo /usr/local/bin/pip install --upgrade autopep8

and update Preferences > Python Support > Custom Code Formatter to autopep8 --in-place $file

Postgresql setup

I'd recommend reading this blog post. Follow instructions in blog post... OR write the sed command yourself :)

Here are some commands.

sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs
sudo service postgresql initdb
sudo service postgresql start

sudo su - postgres
psql -U postgres

then:

CREATE USER "ec2-user" SUPERUSER;

also consider

ALTER ROLE "ec2-user" SET client_encoding TO 'utf8';
ALTER ROLE "ec2-user" SET default_transaction_isolation TO 'read committed';
ALTER ROLE "ec2-user" SET timezone TO 'UTC';

MongoDB

Just read this. Then you can:

sudo service mongod start

MySQL mysql55 is already installed

sudo service mysqld start

Node setup

I'd recommend using nvm.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
nvm install node
nvm use node

Note: you'll have to run nvm use node everytime you run npm install or yarn install.

and yarn

curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn

Bonus: Code Auto-Formatting

The default is esformatter -i "$file", but I've been finding that using Prettier on my JS code is helpful

npm install -g prettier

In Preferences > Javascript Support > Custom Code Formatter, I use:

prettier es5 --write $file

then check Format Code on Save.

Remote Access

In some cases, I need to make my apps public on the internet. Normally I'd use an auto-deployed dev environment to share with others, but if you're working with API endpoints and AJAX or running multiple servers, you may find you need to use the public IP.

I set up a few environment vars for this:

echo "export INSTANCE_ID=\`curl -s http://169.254.169.254/latest/meta-data/instance-id\`" >> ~/.bashrc
echo "export PUBLIC_IP=\`curl -s http://169.254.169.254/latest/meta-data/public-ipv4\`" >> ~/.bashrc
echo "export PRIVATE_IP=\`ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'\`" >> ~/.bashrc
echo "export SECOND_PORT=8081" >> ~/.bashrc
source ~/.bashrc

Note: if you're using ipv6... ref:

Then you might want a public command alias

echo "alias djrp='printf \"\\nserving publicly on http://\$PUBLIC_IP:\$SECOND_PORT/\\n\\n\";dj runserver \$PRIVATE_IP:\$SECOND_PORT'" >> ~/.bashrc
source ~/.bashrc

to serve on: http://$PUBLIC_IP:8081/.

Using Heroku?

Unfortunately, you have to manually install that... ref

cd ~
curl https://cli-assets.heroku.com/install.sh | sudo sh

Note, I had to temporarily add /usr/local/bin to my root $PATH (unsure why):

sudo su
export PATH=$PATH:/usr/local/bin

You might even add it to your /root/.bashrc file, if you want.

Headless Chrome

I need to test in chrome from time to time. These guys built a quick install script that makes this easy.

curl https://intoli.com/install-google-chrome.sh | bash

Need more space?

It's easy to add more space, simply modify your EBS root volume. In the EC2 console scroll down to "Root device /dev/xvda", clikc on the link and then "modify volume."

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