AWS - JSON To PDF - Invoice Tutorial

 

This tutorial shows you how to run ReportLab in an Amazon Web Services (AWS) instance, deployed via Elastic Beanstalk (EB). This is a small app that includes a web form that will produce a sample PDF.

Prerequisites
Part 1 - Local machine
Part 2 - Setup Amazon Elastic Beanstalk
Part 3 - Deploy

Prerequisites

Prior to starting, make sure that your local python has a version that matches the AWS EB pythons. As at March 2020, AWS EB supports 2.7, 3.4 and 3.6.

It is assumed that you have an AWS acount already and that you have acess to the account credentials (access key id & secret acess key).

Part 1- On you local machine

  • Create a Python Virtual Envornment:

python -mvirtualenv sample-aws 

  • Change diretory to sample-aws and activate the virtual environment:

cd sample-aws
. bin/activate

  • For Python 2 download this zip file. For Python 3 download this zip file
  • Unzip the zip file
  • If you don't already have an account, sign up for one.
  • Edit requirements.txt - put in your username & password in the last line (the pypi url) 
  • Install the the Python packages with pip (put your path to requirements.txt):

pip install -r requirements.txt

  • You will also need to install  AWS command lin interface package, eith via pip (pip install awsebcli) or as a system wide package (awscli)
  • Check that json2pdf/settings.py has 'localhost' in ALLOWED_HOSTS, if it is missing then add it.
  • If running Python 2.7, change directory to json2pdf
  • Start the local runserver:

python manage.py runserver

  • To check the application runs locally, point your browser at localhost:8000
  • You should see JSON data in a form.
    web form

 

  • Click 'submit' and make sure you see the sample invoice before proceeding further
    pdf output

 

  • Shutdown the local server

 

Part2 - Set up Amazon Elastic Beanstalk

  • Initialise the app, in doing so AWS will ask you what region to create the instance in.

eb init -p python-{2.7,3.4,3.6} sample-aws
eb init #OPTIONAL
eb create sample-aws-env

  • To get the remote name, run the following command:

eb status

  • Add the name (the CNAME field) to allowed hosts in joson2pdf/json2pdf/settings.py
  • Create directory:

mkdir .ebextensions

  • edit the file .ebextensions/django.config and create something like this (where WSGIPATH is the correct path for our app's wsgi.py file). Take care to indent accordingly;

option_settings:
  aws:elasticbeanstalk:container:python:
   WSGIPath: json2pdf/wsgi.py

Part 3 - Deploy

  • Deploy, then optionaly terminate/deactivate when done

 eb deploy

The following command will show data including the CNAME field (it will most likley end in *.elasticbeanstalk.com). 

eb status

terminal eb status

Put the CNAME field into your browser. You should see your sample invoice app as per Part 1 of this tutorial. If you no longer want to run this app anymore on AWS EB, you can terminate it

eb terminate

deactivate