Editing
Deploying a Django app on AWS
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:AWS]] [[Category:Django]] [[Category:Python]] [[Category:Web Development]] == Overview == ''All of these steps are performed '''locally'''.'' == Prerequisites == <p class="alert alert-warning">I didn't realize this at first, but these prerequisites are all '''local'''. The first time I tried this out, I thought I had to get on a remote server to install Python, Django, awsebcli, etc. All of these things are installed on the local workstation and `eb deploy` handles moving them over and replicating the app environment.</p> * EB CLI <ref>[https://docs.amazonaws.cn/en_us/elasticbeanstalk/latest/dg/eb-cli3-install-osx.html Install the EB CLI on Mac OS] - AWS Elastic Beanstalk docs</ref> <syntaxhighlight lang="sh"> $ brew install awsebcli </syntaxhighlight> * See [[Amazon_Web_Services_Website_Hosting_Fundamentals#AWS_services|AWS Services]] * See [[Django App Prerequisites for AWS Elastic Beanstalk]] == AWS Elastic Beanstalk == Existing Elastic Beanstalk applications and environments can be found in the [https://us-west-2.console.aws.amazon.com/elasticbeanstalk/home AWS Elastic Beanstalk management console]. Create a new Elastic Beanstalk application from the command line on a local workstation: <syntaxhighlight lang="sh"> $ eb init </syntaxhighlight> This will prompt for region, credentials, application name, python version, ssh availability, etc. These properties are stored in `.elasticbeanstalk/config.yml` which should '''not''' be added to Git. Create a new Elastic Beanstalk environment from the command line on a local workstation. Assuming that the instance is using an independent RDS. <syntaxhighlight lang="sh"> $ eb create </syntaxhighlight> If instead the Elastic Beanstalk environment is creating its own database, it's necessary to specify that option with the `create` command: <syntaxhighlight lang="sh"> $ eb create --database.engine postgres </syntaxhighlight> This will prompt for the environment name, its URL prefix, and a database username and password. == Saving and applying Elastic Beanstalk environment configuration == Elastic Beanstalk environment configuration is controlled through the '''Configuration''' panel of the AWS Elastic Beanstalk console. For example, it's necessary to configure Security Groups and Environment Variables to support [[Using_Elastic_Beanstalk_With_Amazon_RDS#Grant_RDS_access_for_the_Elastic_Beanstalk_environment|connecting to independent RDS instances]]. To save an environment's configuration to apply again later if an environment is created from scratch, save the environment configuration. From the AWS EB console: ''select application'' > ''select environment'' > '''Actions''' button at the upper right > '''Save Configuration''' From the eb cli: `eb save environment-name`. This will save the configuration under `.elasticbeanstalk/saved_configs/env-name-version.cfg.yml` To apply a saved configuration to an Elastic Beanstalk environment: '''AWS EB console''' > ''select application'' > ''select environment'' > '''Actions''' button at the upper right > '''Load Configuration''' == Deploying a Django app == Updates to the app are deployed with `eb deploy`. This looks at `.elasticbeanstalk/config.yml` for the target application and environment. <syntaxhighlight lang="yaml"> branch-defaults: master: environment: nrose-env global: application_name: north-rose default_ec2_keyname: nrosedevs-aws default_platform: 64bit Amazon Linux 2015.09 v2.0.6 running Python 3.4 default_region: us-west-2 profile: eb-cli sc: git </syntaxhighlight> With the `master` Git branch checked out, and the `config.yml` above, `eb deploy` will package up the project file and upload them to the EC2 instance corresponding with the `nrose-env` environment of the `north-rose` app. To update a staging environment, edit `config.yml` with the following: <syntaxhighlight lang="yaml"> branch-defaults: master: environment: nrose-env staging: environment: nrose-staging staging-nodebug: environment: nrose-staging </syntaxhighlight> With the `staging` Git branch checked out, `eb deploy` will now update the `nrose-staging` environment. Alternatively, use `eb deploy alternate-env` to target a non-default EB environment named `alternate-env`. === Installing Django app dependencies === See [[Defining Django App Dependencies]] See also [[Customizing AWS Software With .ebextensions]] == Production environment project settings == Set `DEBUG` to `False`: <syntaxhighlight lang="python"> # myapp/settings.py # ... DEBUG = False </syntaxhighlight> With `DEBUG = False`, `ALLOWED_HOSTS` is required. <ref>[https://docs.djangoproject.com/en/1.9/ref/settings/#allowed-hosts ALLOWED_HOSTS] - Django documentation</ref> Add `localhost`, the domain of the site, Elastic Beanstalk, and AWS's Elastic Load Balancer to `ALLOWED_HOSTS`: <syntaxhighlight lang="python"> # myapp/settings.py # ... DEBUG = False ALLOWED_HOSTS = [ "localhost", "mydomain.com", ".us-west-2.elasticbeanstalk.com", ".compute-1.amazonaws.com" # allow viewing of instances directly ] # Grant access to AWS's Elastic Load Balancer import requests EC2_PRIVATE_IP = None try: EC2_PRIVATE_IP = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4', timeout=0.01).text except requests.exceptions.RequestException: pass if EC2_PRIVATE_IP: ALLOWED_HOSTS.append(EC2_PRIVATE_IP) </syntaxhighlight> It's necessary to install `python-requests` for the above code. (`pip install requests`). == Configuring an independent RDS instance == See [[Using Elastic Beanstalk With Amazon RDS]] Run `makemigration` ''locally''. This prevents potential data loss on the production server. <br />`eb deploy` will move the migrations file to the EC2 instance and have them ready to run a `migrate`. <syntaxhighlight lang="bash"> $ python manage.py makemigrations </syntaxhighlight> * Add a `.config` command to run the migration; it will run as part of the `eb deploy` process. * Deploy the project using `eb create` or `eb deploy`. == Creating an admin account == See [[Creating a Django Admin Account on Elastic Beanstalk]] == Serving static files using Amazon S3 == === Configuration === See [[Configuring Django to Serve Media Files Via S3]] === Deployment === Once configured to use S3 for storage, Django's `collectstatic` command will upload static files to the S3 bucket. Both the AWS servers and local development will reference the files on S3. This means that if a stylesheet is compiled, the `collectstatic` command must be run before the changes are available anywhere. <syntaxhighlight lang="sh"> python manage.py collectstatic --noinput </syntaxhighlight> The `--noinput` flag simply bypasses any confirmation messages. The `static` template tag will correctly reference static resources in the Amazon S3 bucket. Timestamps don't always correspond between S3 and the local filesystem. In some cases a newly edited file won't register as changed based on its timestamp. In these cases log into the AWS Management Console, navigate to the bucket under S3 and manually delete the file before executing `manage.py`. == Managing domain names == The documentation at [http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customdomains.html Your Elastic Beanstalk Environment's Domain Name] (AWS Elastic Beanstalk Developer Guide) is straight forward and easy to follow. See [[Managing Elastic Beanstalk Domain Names]] == Security Certificates == See [[EC2 Security Certificates]] for how to install and manage security certificates for Elastic Beanstalk hosted sites. == Notes == === See also === * [http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html Deploying a Django Application] - AWS Elastic Beanstalk documentation * [https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/ Deploying a Django app to AWS Elastic Beanstalk] - Real Python (Jan. 22, 2015) * [http://racingtadpole.com/blog/django-aws-eb-s3/ Django and Amazon AWS Elastic Beanstalk with S3] - Racing Tadpole, June 15, 2013<br />Some information about using S3 for a file system.) * [https://dryan.com/articles/elb-django-allowed-hosts/ How to Make Django's ALLOWED_HOSTS work with AWS ELB Health Checks] - Dryan, Jan 5, 2014 === References === <references />
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information