Dev Standards
  • Untitled
  • project-standards
    • project-success
      • index
      • production.s
    • taking-over-project
      • index
      • migrate-to-new-ios-certificates.mo
    • technical-agility
      • react-native-test.s
      • code-vocabulary-identical-business-vocabulary.s
      • under-15-minutes-project-installation.s
      • index
      • up-to-date-dependencies.s
  • react-native
    • react
      • get-element-size-or-position-with-onLayout.mo
      • binding-functions-in-react-component.s
      • enable-overflow-android.mo
    • setup
      • setup-code-push.mo
      • setup-facebook-login.mo
      • setup-and-deploy-new-project-to-staging.mo
      • add-cocoapods.mo
      • deploy-script.mo
      • setup-and-deploy-new-project-to-staging-with-hockeyapp.mo
      • add-native-module.mo
      • setup_firebase_multiple_envs.mo
      • remove-unnecessary-android-permissions.mo
      • deploy-to-production-android.mo
      • deploy-project-to-production
      • overriding-existing-app.s
      • patch-react-native-android.mo
      • setup-stripe-dev-standard.mo
    • features
      • icomoon.mo
      • lock-device-orientation.mo
      • clean-logout.s
      • deep-linking
      • offline-redux.mo
      • asyncstorage.mo
      • offline-mobx.mo
    • debugging
      • analyse-bug.mo
      • debug-network-calls.mo
      • debug-javascript-ios-device.mo
      • get-ios-logs.mo
      • debug-javascript.mo
      • handle-gradle-dependencies-clash.mo
      • debug-native-ios.mo
      • debug-two-ios-apps-simultaneously.mo
      • debug-native-android.mo
      • debug-webviews.mo
    • firebase
      • debug-events.mo
    • architecture
      • project-architecture.s
      • default-stack.s
      • file-naming.s
    • update
      • upgrade-react-native.mo
    • tests
      • setup-detox-jest.mo
    • use_http_links_in_react_native.mo
    • react-navigation
      • unmount-compoenent-on-page-change.mo
    • package-dependencies
      • handle-dependencies-with-yarn-override.mo
    • animations
      • react-native-animations.s
  • successful-sprint
    • coding
      • plan-your-ticket-to-improve-efficency.s
  • code-quality
    • components-state-testing.mo
    • mock-with-jest.mo
    • pull-request-template.s
    • pull_request_template
    • test-files-indentation.s
  • security
    • import_certificates_match.mo
    • 2FA.mo
  • contributing
    • contributing.mo
    • mo.s
    • standard.s
  • backend
    • graphql-js
      • getting-started-with-apollo-server-dataloader-knex.mo
    • node-js
      • handle-errors-and-exceptions-in-javascript.s
      • add-multiple-environments-configuration-on-loopback.s
    • django
      • deploy-to-aws.mo
      • create-user-model.mo
      • getting-started.mo
      • create-model-and-api.mo
  • performance
    • backend
      • cache-routes-using-varnish.mo
      • serve-images-as-static-files.mo
      • minimize-number-sql-queries.mo
      • python-investigation-tools.mo
      • how-to-investigate-performance.mo
      • output-sql-alchemy-orm-query.mo
    • front
      • how-to-investigate-performance.mo
      • table-and-chart-with-good-performance.mo
      • react-native-performance.s
      • simulate-network-iphone.mo
    • performance-decision-flow.s
  • git
    • merge-or-rebase-a-branch
  • editors
    • vscode
      • setup-vscode.mo
  • ops
    • docker
      • deploy-with-https.mo
  • templates
    • mo
  • react
    • redux
      • custom-redux-form-field.mo
      • pass-props-to-container.mo
    • component.s
    • lifecycle
      • trigger-action-on-props-update.mo
  • flowtype
    • flowtype.s
  • LICENCE
  • README
  • scrum
    • timebox.s
  • SUMMARY
Powered by GitBook
On this page
  • Owner: Xavier Lefèvre
  • Creation date: November, 13th 2017
  • Update date: November, 29th 2017
  • Context
  • Prerequisites
  • Steps
  • Create on AWS website an Identity and Access Management for the project (~5min)
  • Set-up your Elastic Beanstalk (EB) environment (~10min)
  • Configure your project for EB (~15min)
  • Deploying the application (~45min)
  • Create a superadmin (~10min)
  • Debug
  • Deployment
  • Sources
  1. backend
  2. django

deploy-to-aws.mo

PreviousdjangoNextcreate-user-model.mo

Last updated 7 years ago

Owner:

Creation date: November, 13th 2017

Update date: November, 29th 2017

Context

  • You want to host your back-end with

Prerequisites

  • Have python3 and pip3 installed

  • Have a Django project,

  • Have an AWS account ready to pay services:

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:

    pip3 install awsebcli
  • Set up your EB environment, in your project root directory:

    eb init
    • Select your server location, close to where your customers will be:

      Select a default region
      1) us-east-1 : US East (N. Virginia)
      2) us-west-1 : US West (N. California)
      3) us-west-2 : US West (Oregon)
      4) eu-west-1 : EU (Ireland)
      5) eu-central-1 : EU (Frankfurt)
      6) ap-southeast-1 : Asia Pacific (Singapore)
      7) ap-southeast-2 : Asia Pacific (Sydney)
      8) ap-northeast-1 : Asia Pacific (Tokyo)
      9) sa-east-1 : South America (Sao Paulo)
      10) cn-north-1 : China (Beijing)
      (default is 3):
    • 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

    git add .
    git commit -m "Created EB project instance"

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

      • In "10_packages.config":

        packages:
          yum:
            git: []
            postgresql95-devel: []
            libjpeg-turbo-devel: []
            libffi: []
            libffi-devel: []
      • 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:

        "aws:elasticbeanstalk:container:python:staticfiles":
          "/static/": "/staticfiles/"
  • To connect to RDS, add a database configuration to your Django config in "base.py" or "production.py":

    # DATABASE CONFIGURATION
    # ------------------------------------------------------------------------------
    
    if 'RDS_DB_NAME' in os.environ:
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.postgresql_psycopg2',
              'NAME': env('RDS_DB_NAME'),
              'USER': env('RDS_USERNAME'),
              'PASSWORD': env('RDS_PASSWORD'),
              'HOST': env('RDS_HOSTNAME'),
              'PORT': env('RDS_PORT'),
          }
      }
    else:
        DATABASES['default'] = env.db('DATABASE_URL')
  • To serve your statics from AWS, update also the Django config in "base.py":

    # STATIC FILE CONFIGURATION
    # ------------------------------------------------------------------------------
    
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/staticfiles/'
  • You can commit your setup

    git add .
    git commit -m "Added python, django and database config and requirements files"

Deploying the application (~45min)

  • For the first time you will create the instance, hence all the necessary servers:

    eb create --scale 1 -db -db.engine postgres -db.i db.t2.micro
    • 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 website

    • On 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:

      DJANGO_ACCOUNT_ALLOW_REGISTRATION, DJANGO_ADMIN_URL, DJANGO_ALLOWED_HOSTS, DJANGO_AWS_ACCESS_KEY_ID, DJANGO_AWS_SECRET_ACCESS_KEY, DJANGO_MAILGUN_API_KEY, DJANGO_SECRET_KEY, DJANGO_SECURE_SSL_REDIRECT, DJANGO_SENTRY_DSN, DJANGO_SERVER_EMAIL, DJANGO_SETTINGS_MODULE, MAILGUN_SENDER_DOMAIN, REDIS_ENDPOINT_ADDRESS, REDIS_PORT

      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:

    cd /opt/python/current
  • Add the python environment:

    source ./env
  • Run the createuser command:

    cd /app
    ./manage.py createsuperuser

    Check: You can access the aws.ip/admin and go through now with your newly created superadmin

Debug

eb logs

Deployment

eb deploy

Sources

Copy the

Xavier Lefèvre
Amazon Web Services
see this if you want to start one
https://portal.aws.amazon.com/billing/signup#/start
EB configuration from cookie cutter repo
Cookie-cutter Django repository
Step by step tutorial to deploy Django to AWS EB
Another tutorial to deploy Django to AWS EB