In the world of cloud computing, automating the deployment and management of applications is crucial for efficiency and scalability. Amazon Web Services (AWS) provides various tools for these purposes, among which CloudFormation stands out. CloudFormation allows developers to describe the infrastructure they want in a template file, using JSON or YAML, which can then be provisioned within AWS. In this blog post, we’ll create a CloudFormation template that provisions an EC2 (Elastic Compute Cloud) instance, installs Docker, and deploys a Django application backed by a PostgreSQL database.
AWS CloudFormation is a service that allows you to model and set up your Amazon Web Services resources so that you can spend less time managing those resources and more time focusing on your applications. This enables you to automate the deployment process of your application stack.
Before we dive into the CloudFormation template, let’s briefly understand the key components we will be working with:
Here is a sample CloudFormation template written in YAML that provisions the required resources:
AWSTemplateFormatVersion: '2010-09-09'
Description: A simple AWS CloudFormation Template for deploying a Django Blog with PostgreSQL
Parameters:
InstanceType:
Description: EC2 instance type
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- t2.small
ConstraintDescription: Must be a valid EC2 instance type.
Resources:
DjangoBlogInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
ImageId: ami-0c55b159cbfafe16a # Change this to your region's AMI
KeyName: my-key-pair
UserData:
Fn::Base64: !Sub |
#!/bin/bash
yum update -y
amazon-linux-extras install docker
service docker start
usermod -aG docker ec2-user
docker run -d -p 8000:8000 my-django-app
DjangoBlogSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP and SSH access
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 8000
ToPort: 8000
CidrIp: 0.0.0.0/0
PostgreSQLDB:
Type: AWS::RDS::DBInstance
Properties:
DBInstanceClass: db.t2.micro
Engine: postgres
MasterUsername: admin
MasterUserPassword: MySuperSecretPassword
DBName: mydatabase
AllocatedStorage: '20'
VPCSecurityGroups:
- !GetAtt DjangoBlogSecurityGroup.GroupId
Outputs:
BlogURL:
Description: "URL for your newly created Django Blog"
Value: !Sub "http://${DjangoBlogInstance.PublicIp}:8000"
Let’s break down this CloudFormation template:
1. AWSTemplateFormatVersion: Specifies the version of the template format you are using.
2. Parameters: Define the inputs needed when launching the stack. In this case, we’re allowing the user to specify the instance type.
3. Resources: This section contains all the AWS resources that CloudFormation will create. In our example, we create:
4. Outputs: This section defines the outputs that are displayed post-creation. Here, we output the URL where the Django application can be accessed.
To deploy the template, follow these steps:
Wait until the stack creates successfully, and you should see the output with the blog URL.
Using AWS CloudFormation to provision infrastructure for your applications greatly simplifies the deployment process and ensures consistency across environments. In this blog, we provisioned an EC2 instance, installed Docker, and deployed a Django application using PostgreSQL—all through a simple CloudFormation template. As you become more familiar with AWS services and CloudFormation syntax, you can expand and customize your templates to incorporate other services such as Amazon S3 for file storage, AWS Lambda for serverless functions, or even adding Elastic Load Balancing for higher availability.
By leveraging these technologies, you can make your Django application robust, scalable, and capable of handling production workloads effectively.