One of the common web architecture types is called the multi-tier architecture – of which the three tier is the best known. In this series we will look at building a AWS Three Tier Architecture via a Python application.
The following depicts the general layout of a this type of design:
In my design we will have three tier design with a slight variation:
- Load Balancer, which distributes load across multiple dynamically scaled up/down EC2 instances
- Multiple EC2 instances all having the same Web Server/Application Server
- DynamoDB NoSQL Database which we write an item to on each web page view
Here we will build a Python application using the AWS boto3 module for our API calls which can be viewed HERE. For anyone new to the boto3 module – have a look at my previous post on using it to make AWS API calls in python HERE.
If you want to dive right in to the code, or just view the complete version whilst reading the notes, a copy of the completed python script can be found at the end of the last post HERE.
The steps we will go through to create this are as follows:
- [via GUI] Build Linux EC2, Connect to DynamoDB, Create Security Group, Create Key Pair, have Default VPC created
- [via APIs]Create AMI from instance
- Create Launch Template
- Create Target Group
- Create Auto Scaling group
- Create Load Balancer
Firstly a few things need to be setup. This application is about creating a three tier architecture from an existing EC2 instance with access to Dynamo DB (not creating an EC2 instance of setting up a VPC). So the following is needed first:
- Create/already have an EC2 instance which does something (e.g. shows web pages – or in my case connects to DynamoDB and writes a item and shows a web page). We will use the EC2 instance as a the AMI master which gets created/shutdown as per the auto scaling rules. I setup DynamoDB as well here since my web server talks to it (see HERE for the AWS PHP DynamoDB docs)
- Have a VPC setup which is setup as a ‘Default’ VPC
- Create a Security Group in the Default VPC which allows all traffic from TCP 80 and 22
- Have the key pair for the EC2 instance
- Have 2 subnets (each in a different AZ of the Default VPC) setup
You will need the ID for each of the above items to start with the application. To setup my Linux EC2 instance (using Ubuntu16) I added the following modules to enable the below web page to work via [apt-get install]:
- Apache2
- php
- python
- mod-php
- php-simplexml
- php-xml
Each of these was needed to setup the web server and be able to talk to the DynamoDB instance. After editing the php.ini file to support “<?” I used the following code to create a file aws_demo.php (in the default web folder /var/www/html) which is what I later call when testing the load balancer and NoSQL writes (a copy of this file is at the end of the next post HERE):
This simple PHP script connects to my DynamoDB instance and writes an item to it (time and private IP of EC2 instance) – which we can later check to see that writes are getting done to the DB from different IPs (as we expect with the Load Balancer working)
So lets get on and build the APP ! In the next post we will build the APP and look at the result of creating our own automated three tier build. Click HERE to move to the next page.
Pingback:AWS Three Tier Architecture with Python - part2 - Cloud Networking and Automation