Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Amazon Web Services

Deploy Django Using CloudFormation Template

2022-07-19

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..

aws-mp-banner

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.