carolinanonato / 2-tier-web-app

This application serve a 2-tiered application with web and db components running as containers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Building docker images and deploying docker container on AWS

architecture

This projects consists in:

  • A Flask web application that connects to a MySQL database and performs various operations such as adding and retrieving employees information from the database.
  • A Terraform code that provisions an EC2 instance with the latest Amazon Linux AMI, adds an SSH key to the EC2 instance, creates a security group that allows SSH and certain inbound traffic, creates an elastic IP, and creates ECR repositories.
  • A GitHub Actions that build, test, tag, and push images to Amazon ECR. This workflow is triggered by any push to the main branch.

Running the application

  1. Terraform
  • Clone this repo
  • Create a SSH key named A1-dev
  • Run Terraform (plan, apply)
  1. GitHub Actions
  • The Terraform code will create the ERC webapp and db. Make a pull request to the main branch and trigger the GitHub Actions. This will push the images to the ECR.
  1. EC2
  • Connect to the EC2 instance
  • Update yum and install docker
  • Insert your AWS credentials, if needed.
  • Login to ECR
  • Pull both images (db and webapp) to your local environment.
  1. Running the Containers
  • Create a custom bridge network and use any name you want.

  • Run bd (MySql) container.

  • Use the following variables:

    export DBHOST=(your container IP)
    export DBPORT=3306
    export DBUSER=root
    export DATABASE=employees
    export DBPWD=your password)
    

    Example:

    docker run -d -e MYSQL_ROOT_PASSWORD=123 --network A1-network 123456789.dkr.ecr.us-east-1.amazonaws.com/db:latest         
    
  • Run the webapp containers and change the variable APP_COLOR to see a different background Exemple:

    docker run --name blue -p 8081:8080 -e DBHOST=$DBHOST -e DBPORT=$DBPORT -e  DBUSER=$DBUSER -e DBPWD=$DBPWD -e APP_COLOR=blue --network A1-network 12345678.dkr.ecr.us-east-1.amazonaws.com/webapp:latest
    

    Ran as many webapps containers you want, just remember to change the ports (the Terraform code already opens the connection to 8081, 8082 and 8083).

About

This application serve a 2-tiered application with web and db components running as containers.


Languages

Language:HCL 33.8%Language:Python 33.8%Language:HTML 29.3%Language:Dockerfile 3.2%