deploy-to-aws.mo
Owner: Xavier Lefèvre
Creation date: November, 13th 2017
Update date: November, 29th 2017
Context
You want to host your back-end with Amazon Web Services
Prerequisites
Have python3 and pip3 installed
Have a Django project, see this if you want to start one
Have an AWS account ready to pay services: https://portal.aws.amazon.com/billing/signup#/start
Steps
Create on AWS website an Identity and Access Management for the project (~5min)
Find IAM from AWS services
Go to the User tab
"Add user"
Give it a name and all access types
Attach the below "existing policies directly"
AmazonRDSFullAccess
AmazonEC2FullAccess
AmazonElastiCacheFullAccess
AmazonS3FullAccess
AWSElasticBeanstalkFullAccess
Retrieve and store somewhere safe the IAM user access and security key
Set-up your Elastic Beanstalk (EB) environment (~10min)
Install EB CLI:
Set up your EB environment, in your project root directory:
Select your server location, close to where your customers will be:
Connect to your IAM user
Give a general name to your instance, generally the name of the project/company, example: name_of_project-staging
Precise that you are using Python 3+
Say no to "CodeCommit"
Generate a new SSH Keypair that you will share with your teammates
Check: EB should have generated a ".elasticbeanstalk" folder in your directory with your configuration
You can commit finally your setup
Configure your project for EB (~15min)
We use cookiecutter as our base Django project at BAM, so we will use their configuration for EB:
Create a ".ebextensions" in your project root directory
Copy the EB configuration from cookie cutter repo
In "10_packages.config":
Copy "20_elasticcache.config", "30_options.config", "50_apache.config" and "enable_mod_deflate.conf" as is
Copy "40_python.config" but change the last two lines with:
To connect to RDS, add a database configuration to your Django config in "base.py" or "production.py":
To serve your statics from AWS, update also the Django config in "base.py":
You can commit your setup
Deploying the application (~45min)
For the first time you will create the instance, hence all the necessary servers:
Name your environment, like: project-name-staging
Select the default CNAME
Select the classic load balancer option
Choose a username and password to connect to the DB
EB will now generate all your servers and provision your config then launch your project
But you might still have errors
eb logs
, you are missing environment variables that you can directly add to AWS websiteOn AWS website, on the top right, select the right server location (ex: London)
Go to Elastic Beanstalk
Enter your new application
Go to configuration
Modify the software configuration with variables such as:
Check 1: You can verify on the AWS website that each server piece has been created (EB, RDS, S3, EC2) Check 2: You can access the aws.ip/admin
Create a superadmin (~10min)
Now that you have deployed once, on your host you should have the credentials for AWS (cat ~/.aws/config).
To create a super user you will have to connect directly to the machine in order to run ./manage.py createsuperuser.
To ssh to your EC2 machine run eb ssh [PUT YOUR INSTANCE NAME IF NOT BY DEFAULT]
Then on the machine go to the root of the project:
Add the python environment:
Run the createuser command:
Check: You can access the aws.ip/admin and go through now with your newly created superadmin
Debug
Deployment
Sources
Last updated