Demonstration files to build and deploy a sample Python Flask web application to Docker Hub and Amazon Web Services (AWS).
- You will need a Docker Hub account.
- You will need to create a Docker Access Token and add it as a secret to your GitHub repository. Please see the reference
Build and push Docker images
in theREFERENCES.md
file. - You will need an Amazon Web Services Account.
- All AWS resources are used in this repository fall under the AWS Free Tier, however if you do deploy this to your AWS account, you are responsible for any costs incurred.
- You already have the default Amazon S3 Bucket used by AWS CodePipeline.
- The Amazon EC2 web server will be deployed in the default VPC of your AWS account. If you are deploying this for a production environment, it is recommended to create a new VPC.
- All AWS resources are deployed in the Asia Pacific (Sydney) ap-southeast-2 Region.
- You will need to have setup the connector between GitHub and AWS. More information can be found here.
Please see the table below for an overview of the implemented GitHub actions, these files can be found the following directory: .github/workflows/
GitHub Action File Name | Description | Affected Branches | Event Type |
---|---|---|---|
push_to_docker_hub.yml | Runs unit tests, build and pushes the Docker container to Docker Hub | main | Push to single branch |
python_linting.yml | Run Python PEP 8 formatting over the codebase and creates an new pull request if the linting fails. An example can be seen here: #17 | main and develop | Push to multiple branches |
python_unit_tests.yml | Runs pytest unit tests against the codebase | main and develop | New Pull Requests |
A pipeline has been created that runs unit tests of the codebase and deploys the Python Flask web application to an Amazon EC2 instance. The pipeline is executed when changes are made to the main
branch.
-
Login to the AWS Management Console.
-
Change your Region to
ap-southeast-2 (Sydney)
(if you haven't already). -
Once logged in, navigate to the
AWS CloudFormation Console
. -
Create a new Stack with new resources.
-
Choose the
Upload a template file
option and upload the following filepipeline/pipeline.yml
, then pressNext
. -
Enter the following for the
Stack name
: UWA-Git-Good-Presentation-Pipeline -
Enter the GitHub connector ARN for the
GitHubConnectionArn
parameter. -
Enter the name of the Amazon S3 bucket used by AWS CodePipeline for the
PipelineAmazonS3Bucket
parameter. -
Press
Next
. -
Press
Next
again. -
Scroll down to the bottom of the page and select the
I acknowledge that AWS CloudFormation might create IAM resources
checkbox, then pressCreate stack
. -
Once the pipeline stack has been deployed, it will automatically deploy the Python Flask web application to AWS.
Run the following commands to download and install the required Python Dependencies:
sudo apt install python3-flask -y
sudo pip3 install -r requirements.txt
To run linting over the code, run the following commands in Visual Studio Code:
-
Press
F1
. -
Search for
Python: Run Linting
. -
Press
CTRL + ~
to open the inbuilt Terminal. -
Select
PROBLEMS
and address the identified linting issue(s).
To run all unit tests, run the following command:
python3.10 -m pytest tests/
If you want to print the tests standard output to the screen, run the following command:
python3.10 -m pytest -s tests/
Complete this section if you want to develop and run the code locally.
Run the following command to start the web application:
flask run
Complete this section if you want to build and run a Docker container
To build the Python web application Docker Container, run the following command:
docker build -t uwagitgoodwebapp .
To run the Python web application Docker Container, run the following command:
docker run -it --rm -p 80:5000 uwagitgoodwebapp:latest
The usage commands are slightly different when running the code locally or using a Docker container. Follow the instructions based on how you plan to run the code.
Follow this section if you are going to run the Python code on your local machine and not use a Docker container:
Open a web browser and navigate to:
http://127.0.0.1:5000/
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1:5000/
Open a web browser and navigate to:
http://127.0.0.1:5000/greeter
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1:5000/greeter
Open a web browser and navigate to:
http://127.0.0.1:5000/greeter?name=test
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1:5000/greeter?name=test
You can change the value of name
to any string value of your choosing.
Follow this section if you are going to run the Python code as a Docker container:
Open a web browser and navigate to:
http://127.0.0.1/
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1/
Open a web browser and navigate to:
http://127.0.0.1/greeter
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1/greeter
Open a web browser and navigate to:
http://127.0.0.1/greeter?name=test
or open a Terminal and run the following command:
curl -X GET http://127.0.0.1/greeter?name=test
You can change the value of name
to any string value of your choosing.
If you have deployed the AWS CodePipeline pipeline in your AWS account, follow the instructions to delete and cleanup your AWS account:
-
Login to the AWS Management Console.
-
Change your Region to
ap-southeast-2 (Sydney)
(if you haven't already). -
Once logged in, navigate to the
AWS CloudFormation Console
. -
Select the
UWA-Git-Good-Presentation-Webserver
stack, pressDelete
thenDelete stack
. -
Once the
UWA-Git-Good-Presentation-Webserver
stack has been deleted, select theUWA-Git-Good-Presentation-Pipeline
stack, pressDelete
thenDelete stack
. -
Navigate to the
Amazon S3 Console
. -
Empty the default Amazon S3 Buckets used by
AWS CloudFormation
andAWS CodePipeline
.