Deploy Django using CloudFormation Template

In this blog post, You will get to know how to deploy Django using CloudFormation Template. If require any support then contact our Development Services

CloudFormation:

Using JSON templates we describe the resources needed from aws. With this approach, we don't have to repeat the same manual configuration every time.

Consider a Django(python based web development framework) website has to be deployed using aws then the basic things we need to do is: 

1. Setup IAM policies and roles
2. Setup EC2 instance
3. Setup Security groups
4. Configure EC2 like updating packages, setting up Nginx, uwsgi etc..

Steps 1, 2, and 3 are concerned with aws resources. CloudFormation is helpful to automate these 3 steps, so instead of configuring manually each and every time we can automate to grab the aws resources by providing a JSON template.

Note:

CloudFormation is different from Elastic Beanstalk, elastic beanstalk automates step 4 like shell scripts, ansible scripts etc..CloudFormation takes care of aws resources like how many instances we need, security groups etc..

Creating CloudFormation Template:

CloudForm

Manually creating a cloud formation template is complex, so CloudForm is used to simplify this task.

Before using cloud form make sure that the resources that you need are running, it means if I am going to create a template to deploy a web application then I need have my ec2 instance up and running, a security group, vpc, subnet already being created for my ec2 instance and also an IAM policy that will use for my app.

CloudForm is thus a template creating tool from existing resources.

Creating Template with CloudForm:

To run cloud form go to aws console and get into CloudFormation, here at the bottom select CloudForm.

Note:

CloudForm template creation tool runs on t2.medium instance so be careful about cost while running cloud form. Remember to stop CloudForm(t2.medium) after creating your template.

Launching CloudForm starts running the CloudForm template, the sample CloudForm template looks like:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS CloudFormer Beta - template creation prototype application. This tool allows you to create an AWS CloudFormation template from the AWS resources in your AWS account. **Warning** This template creates a single EC2 instance in your account to run the application - you will be billed for the instance at normal AWS EC2 rates.",
  "Parameters": {
    "Username": {
      "Description": "Username to log in to CloudFormer",
      "Type": "String"
    },
    "Password": {
      "Description": "Password to log in to CloudFormer",
      "Type": "String",
      "NoEcho": "true"
    },
    "VPCSelection": {
      "Description": "This setting will control if the Cloud formed Web server will launch in the default VPC or if a new VPC will be created, or if you wish to launch into an existing non-default VPC.",
      "Type": "String",
      "Default": "CreateNewVPC",
      "AllowedValues": [
        "Default",
        "CreateNewVPC"
      ],
      "ConstraintDescription": "must be either Default or CreateNewVPC"
    }
  },
  "Conditions": {
    "DefaultVPC": {
      "Fn::Equals": [
        {
          "Ref": "VPCSelection"
        },
        "Default"
      ]
    },
    "CreateNewVPC": {
      "Fn::Equals": [
        {
          "Ref": "VPCSelection"
        },
        "CreateNewVPC"
      ]
    }
  },
  "Mappings": {
    "Region2Examples": {
      "us-east-1": {
        "Examples": "https://s3.amazonaws.com/cloudformation-examples-us-east-1"
      },
      "us-west-2": {
        "Examples": "https://s3-us-west-2.amazonaws.com/cloudformation-examples-us-west-2"
      },
      "us-west-1": {
        "Examples": "https://s3-us-west-1.amazonaws.com/cloudformation-examples-us-west-1"
      },
    },
}

So When you run CloudForm you will be directed through the steps for selecting required available resources, this includes storage, compute, security etc..Since we need EC2 t2.micro instance and a simple security group which allows ssh and http on their default ports, we are going to make sure that we select only these.

1. Select Region
2. Network Interfaces, select vpc and subnet, make sure that these two are in same regions.
4. Compute: select your desired ec2 instance, if nothing is displayed then probably you don't have any instances running.
5. Security groups: A list of available security groups will be listed, check the appropriate one which allows ssh and http.
6. Save the template to s3 for future usage. Just provide the bucket you want to save this template.

Launch the stack(template):

Select the template and launch it. You can view the stats for debugging. A CREATE_COMPLETE message will be displayed if everything goes well

Finally, your aws environment is setup, now you can run your scripts to deploy the app.

Next time when we have to configure the same setup or a little-modified one we can simply reuse this template and run it. Within minutes we will have our resources ready.

Final Note:

Beware of the cost of using CloudForm, also CloudForm isn't yet completely ready, it is in its beta state.

Posted On 27 June 2016 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
AWS Lambda - Best Practices

After Years of Developing Lambda Scripts from creating Serverless Applications to Pipelining your Tasks, Here are the Best Practices that we follow.

Continue Reading...
Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Continue Reading...
How to Build and verify an application using aws codepipeline and creating custom events with lambda

How to build and verify an application using aws codepipeline and creating custom events with lambda.

Continue Reading...
AWS tips and tricks to optimize cost and performance for better ROI

Best Practices of AWS cost and Performance Optimization

Continue Reading...
How to process message queuing system by amazon SQS

How to process attributes of message queuing system by amazon SQS using Boto3

Continue Reading...
Easy and Fast way to implement AWS Lambda service

We are going to use a simple application called Gordan to prevent creating a lambda function and triggering actions which involves time taking and repetitive …

Continue Reading...
How To Send And Receive Email With Django And Amazon SES

django-ses-gateway a pluggable Django application is used for sending mails from your verified domains and verified emails. We can also use django-ses-gateway for receiving messages …

Continue Reading...
Deploy Django using CloudFormation Template

CloudFormation helps in Using JSON templates to describe the resources needed from aws. With this approach, we don't have to repeat the same manual configuration …

Continue Reading...
How to Mount S3 Bucket on Local Disk

It all starts with FUSE, FUSE is File System User Space. Operating Systems have Kernel Space and User Space. Kernel Space is where low level …

Continue Reading...
Using AWS Lambda with S3 and DynamoDB

AWS lambda is handy tool for event driven computation, here we will learn how to configure and setup lambda function so to run our function …

Continue Reading...
How to access EC2 instance even if pem file is lost

Accessing the EC2 instance even if you loose the pem file is rather easy.

1. First, create a new instance by creating new access …

Continue Reading...
Deploying Django project on Elastic Beanstalk

Here You can learn about how to setup and deploy a Django application to Amazon Web Services (AWS).

Tools/technologies used:
Python v2.7
Django v1.7
Amazon …

Continue Reading...
how to setup custom domain for amazon cloudfront

We all want our own domain name to be setup for cloud front instead of amazon default cloud front domain name. We need two things …

Continue Reading...
Paginating S3 objects using boto3

When using Boto you can only List 1000 objects per request. So to obtain all the objects in the bucket. You can use s3's paginator.

Continue Reading...
Creating Elastic Search Cluster (EC2, cloud-aws Plugin)

While handling Large amounts of data with elasticsearch, you may run out of server capacity or compute power, Forming a Elasticsearch cluster will reduce the …

Continue Reading...
Configuring and Testing Load Balancer in AWS EC2

When You have an application that is serving Huge Customer Base, so will be your Traffic. Sometimes The Application simply stops responding. We can use …

Continue Reading...
Django Hosting on Amazon EC2 with wordpress on same domain

Configuring the Wordpress as subdirectory can be tricky. In this tutorial we will Setup a Django Website alongside a wordpress blog.

Continue Reading...
Amazon SES - Handling Bounces and Complaints

In general while sending emails, we will prepare some recipient addresses as our mailing list, which are valid and our recipients want and expect our …

Continue Reading...
Amazon AWS IAM Roles and Policies

When You want to Provide access to Amazon Web Services Console or if you're planning to provide REST API Keys to your Developers of a …

Continue Reading...
CORS with Amazon S3 and CloudFront

We struggle to load fonts from CloudFront because of CORS.

CORS - Cross Origin Resource Sharing is a security measure to block macious scripts or …

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
*We don't provide your email contact details to any third parties