Skip to content

doritoes/iploc8.com

Repository files navigation

iploc8.com

Building a container-based geolocation API using Flask and MySQL running on Amazon ECS.

Docker Pulls License MIT

Why one container for Flask and MySQL? Isn't the current pattern to use docker compose with separate containers for each service?

  • I wanted portable container that could easily run on ECS
  • The container loads the most recent geo data at instantiation in the database for lookup, but not updates are made
  • The container "ages out" after a configurable amount of runtime, trigging another container to load with fresh data

Please be forewarned that this lab is fairly expensive. AWS promises to save you up to 50% on compute costs by utilizing autonomous scaling, provisioning, and usage-based pricing. However the costs of even a lab environment add up quickly. Here the top costs:

  • VPC costs 💵
  • ELB costs 💵
    • you pay for AWS resources to run the load balancer(s)
    • per application load balancer-hour
    • per LCU-hour (load-based)
    • redue the number of regions
  • ECS costs 💵
    • you pay for the memory and vCPU resourcs the containers use
    • reduce costs by reducing the minimum required CPU and memory in the Task definition
    • reduce scaling - use step scaling and reduce the maximum number of tasks
    • reduce the number of regions
  • Route53 - monthly cost
  • EC2 costs / EC2 - Other
    • Idle Elastic IPs incur a small fee
    • ELB usage is charged a small fee

Learn more at https://www.appsdevpro.com/blog/aws-fargate-pricing/

Overview and Genesis

After building a few IP address look up sites on different technologies, I wanted to create my own custom geo-lookup API that I can leverage on my other sites.

The goal is to leverage repositories with geo-location data together with published IP address ranges to provide some unique perspective. There are many other API services that do enriched geo-location for a fee. However, the goal is this project is to develop the concept of what is possible independent of these services.

This demonstration site has the following features:

  • Serverless computing on Fargate
  • Python Flask with MySQL back-end database
  • API design examples
    • unrestricted
    • JWT with API keys
    • CloudFront WAP protections
  • Demonstrate global autoscaling container applications without breaking the bank (don't want to cost too much for this free site)

Project Goals

Here are the goals I had for this project. This is meant to be a step-by-step Lab exercise that you can follow along to.

IMPORTANT The web site https://iploc8.com is being discontinued. The cost of AWS outweighs the value to me.

In Scope

Completed

  • Register domain name
  • Bootstrap repo and Dock hub image
  • first geo API with country and ASN data
  • second geo API with city data, secured with API key and JWT tokens
  • third and fourth geo API integrating with ip-api.com
  • CORS testing
  • one region built in ECS with CloudFront and WAF protection
  • multi-region
  • Zscaler and Broadcom SWG address spaces

Main Concerns

  • Reducing costs of operation

Missing Features

  • MS published address spaces
  • MS Azure customer space
  • GCP customer space
  • AWC customer space
  • Tor published address spaces
  • Threat Intel enrichment
    • Reputation/blacklists

Out of Scope

  • Amazon Elastic Kubernetes Service (EKS) doesn't meet my criteria for integrating a new technology for me. I have done other Kubernetes labs (here and here).

Quick Start

⚠️ If you are using Windows, you need to do this in a WSL (Windows Subsystem for Linux) window. Otherwise, you will have line ending issues with the scripts and other text files.

  1. git clone https://github.com/doritoes/iploc8.com
  2. cd iploc8.com
  3. docker build -t my-flask-mysql .
  4. docker run -p 5000:5000 -e MYSQL_ROOT_PASSWORD=your_password my-flask-mysql
    • you will see "MySQL is unavailable -sleeping" for while
    • Or run detached: docker run -d -p 5000:5000 -e MYSQL_ROOT_PASSWORD=your_password my-flask-mysql
  5. test

Step-by-Step

  1. Prerequisites
  2. Flask Application Design
  3. Building Docker Container
  4. Configuring ECS
  5. Configuring CloudFront with WAF
  6. Testing and Monitoring
  7. Adding Regions
  8. Next Steps

Data Sources

Find Out More

About

Geolocation api

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published