Deploying Django project on Elastic Beanstalk2022-07-19
This section focuses on how to setup and deploy a Django application to Amazon Web Services(AWS).
Amazon Elastic Beanstalk, EC2, and S3
NOTE: If you’re new to Elastic Beanstalk, check out their tutorial to help you get a Django 1.4 site up on AWS quite quickly.
lastic Beanstalk is a Platform As A Service (PaaS) that streamlines the setup, deployment, and maintenance of your app on Amazon AWS. It’s a managed service, coupling the server (EC2), database (RDS), and your static files (S3).
You can quickly deploy and manage your application, which automatically scales as your site grows. Check out the official documentation for more information here.
To actually start using Elastic Beanstalk you will need an account with AWS.
Elastic Beanstalk needs the following information to deploy an application:
1. AWS access key ID - Your amazon access key ID.
2. AWS secret key - Your amazon secret Key.
3. Service region - the region where you want to deploy an application.
Ex: US West (Oregon).
4. Application Name - The name of the application. Ex: HelloWorld
5. S3 Bucket - The bucket that beanstalk apps will be stored in.
6. Environment name - A unique name for the deployment environment.
Ex: Defaults to HelloWorld-env
7. Solution stack - The solution stack specifies the operating system, architecture, and application server for a configuration template.
Procedure for deploying a Django Application :
import boto from boto.beanstalk import * connection = connect_to_region(region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
2. Create an application that has one configuration template named default and no application versions.
a. application_name: The name of the application.
Constraint: This name must be unique within your account.
b. description: Describes the application.
import boto from boto.beanstalk import * response = connection.create_application(application_name="first-app", description="This is my first Elastic beanstalk Application")
3. Connect to S3:
from boto.s3.connection import S3Connection s3_connection = S3Connection("aws_access_key_id", “aws_secret_access_key")
4. Upload an application zip file (archive version) to the s3 bucket.
from boto.s3.key import Key bucket = s3_connection.get_bucket(bucket_name="beanstalk") k = Key(bucket) k.key = key response = k.set_contents_from_filename("firstapp.zip")
a. application_name: The name of the application.
b. version_label: A label identifying this version.
Constraint: Must be unique per application.
c. description: Describes this version.
d. s3_bucket: The Amazon S3 bucket where the data is located.
e. s3_key: The Amazon S3 key where the data is located. Both s3_bucket and s3_key must be specified in order to use a specific source bundle.
NOTE: If both of these values are not specified the sample application will be used.
f. auto_create_application: true: Automatically creates the specified application for this version if it does not already exist.
response = connection.create_application_version(application_name="first-app", s3_bucket="beanstalk", version_label="firstapp Release 0.0", s3_key="firstapp Release 0.0", description="Release 0.0-Elastic BeanstalkApp")
NOTE: With Elastic Beanstalk, an application can have multiple environments development, testing, staging, production.
a. application_name: The name of the application that contains the version to be deployed.
b. environment_name: A unique name for the deployment environment. Used in the application URL.
c. version_label: The name of the application version to deploy. If not specified, AWS
Elastic Beanstalk attempts to launch the most recently created application version.
d. template_name: The name of the configuration template to use in deployment.
NOTE: You must specify either this (template_name) parameter or a SolutionStackName, but not both.
e. solution_stack_name: This is an alternative to specifying a configuration name.
f. cname_prefix: If specified, the environment attempts to use this value as the prefix for the CNAME. If not specified, the environment uses the environment name.
NOTE : When you deploy an app to Elastic Beanstalk you will automatically get a domain name like
xxx.elasticbeanstalk.com. DNS CNAME prefix is what you want to be used in place of xxx. Just go with the default.
g. description: Describes this environment.
h. option_settings : Each element in the list is a tuple of (Namespace, OptionName, Value), for example: [('aws:autoscaling:launchconfiguration', 'Ec2KeyName', 'mykeypair')]
i. tier_name: The name of the tier. Valid values are "WebServer" and "Worker". Defaults to "WebServer".
The possible combinations are:
* "WebServer" and "Standard" (the default)
* "Worker" and "SQS/HTTP"
j. tier_type: The type of the tier. Valid values are "Standard" and "SQS/HTTP"
k. tier_version: The version of the tier. Valid values currently are "1.0". Defaults to "1.0".
solution_stack_name = "64bit Amazon Linux 2014.09 v1.2.0 running Python 2.7" cname_prefix = "stage" env_description = "Staging Environment" # Each element in the list is a tuple of (Namespace, OptionName, Value) option_settings = [ ("aws:elasticbeanstalk:application:environment", "DJANGO_SETTINGS_MODULE", "Django_Fabric_ElasticBeanstalk.settings"), ("aws:elasticbeanstalk:application:environment", "application_stage", "staging"), ("aws:elasticbeanstalk:container:python", "WSGIPath", "Django_Fabric_ElasticBeanstalk/wsgi.py"), ("aws:elasticbeanstalk:container:python", "NumProcesses", 3), ("aws:elasticbeanstalk:container:python", "NumThreads", 20), ("aws:elasticbeanstalk:container:python:staticfiles", "/static/", "static/"), ] tier_type = "Standard" tier_name = "WebServer" tier_version = "1.0" response = connection.create_environment(application_name=app_name, environment_name="stage-env", version_label="firstapp Release 0.0", solution_stack_name=solution_stack_name, cname_prefix=cname_prefix, description=env_description, option_settings=option_settings, tier_type=tier_type, tier_name=tier_name, tier_version=tier_version )
response = connection.update_environment(environment_name="stage-env", version_label="firstapp Release 0.0")
That's it, I guess now you can manage to deploy django project on Elastic Beanstalk. If require any support regarding AWS Services then contact our AWS Consulting services to grow your business.