The goal of this project is to demonstrate how to deploy a a simple application with three components:
- A Python program using
requeststo collect data. - The redis database to store data
- A Flask server launched via gunicorn to server up the data.
The data used in this example is the current temperature in Bethlehem using data from the WeatherAPI.
The "application" modeled is intentionally minimal: Every 15 minutes, the collector obtains the current count and stores it in the redis database. The Flask server has a single end point that allows a user to fetch this data.
To use this application you must signup for a free WeatherAPI key.
After you have signed up and confirmed your email, go to the WeatherAPI Account page and copy your API key (near the top of the page). To verify your API key, replace <API_KEY> in the following command:
curl "http://api.weatherapi.com/v1/current.json?q=18018&key=<API_KEY>"
If it succeeds, the output should be something like:
{"location":{"name":"Bethlehem","region":"Pennsylvania","country":"USA","lat":40.62,"lon":-75.41,"tz_id":"America/New_York","localtime_epoch":1710293264,"localtime":"2024-03-12 21:27"},"current":{"last_updated_epoch":1710292500,"last_updated":"2024-03-12 21:15","temp_c":12.2,"temp_f":54.0,"is_day":0,"condition":{"text":"Clear","icon":"//cdn.weatherapi.com/weather/64x64/night/113.png","code":1000},"wind_mph":5.6,"wind_kph":9.0,"wind_degree":240,"wind_dir":"WSW","pressure_mb":1010.0,"pressure_in":29.83,"precip_mm":0.0,"precip_in":0.0,"humidity":45,"cloud":0,"feelslike_c":11.7,"feelslike_f":53.1,"vis_km":16.0,"vis_miles":9.0,"uv":1.0,"gust_mph":9.4,"gust_kph":15.1}}
We will deploy this system on an EC2 instance. To deploy on AWS, we have to do following:
- Install
gitand clone the repository
sudo yum install -y git
git clone https://github.com/cs220s24/dockerized_weather_app.git
- Install docker, start it, and make it available to the
ec2-user. All steps are embedded inaws_deploy.sh
cd dockerized_weather_app
./aws_deploy.sh
NOTE: After this step you need to log out and log back in to the EC2 instance. The last step in the script is to add the ec2-user to the docker group so it can run docker commands, but the shell only reads group membership at login.
- We will mount
collector.envas.envin thecollectorcontainer andserver.envas.envin theservercontainer. Make those files:
Create colector/collector.env as:
REDIS_HOST=redisdb
REDIS_PORT=6379
API_KEY=<api key>
Create server/server.env as:
REDIS_HOST=redisdb
REDIS_PORT=6379
- Build the container images. These steps are embedded in
build.sh
./build.sh
- Launch the system. There are three containers to start (
redis,collector, andserver), and all three need to be started in the same Docker network. In addition, therediscontainer needs to mount a volume to ensure thedump.rdbfile is saved, and thecollectorandservercontainers need to have their.envfiles mounted. Theupscript contains all the necessary commands
./up
If everything worked, you will be able to get the current temperature in Bethlehem by accessing the system.
curl localhost
If this works, you can verify that your EC2 instance is configured correctly by connecting to the system using a web browser
http://<EC2 IP address>
If you need to stop the system, use the down script:
./down
