A web application which reads stock data from an upstream API and summarizes stock price data. Deployed into a minikube cluster which runs on an EC2 instance in AWS.
./src/main.rs
contains the source code for the web application./deploy/template.py
is a python script to create the CloudFormation template (using thetroposphere
library)../deploy/stonks.template
is the CloudFormation template which does the following- Creates underlying AWS infrastructure (instance, security group, etc)
- Installs and starts
minikube
- obtains application manifest from github and launches application within K8S
./deploy/userData.sh
is a bash script which is provided to the ec2 instance to run at boot, this does most of the "work" related to bootstrapping the instance at launch../deploy/stonks.yaml
is the k8s manifest which is pulled by the ec2 instance and created withinminikube
./deploy/deploy.py
is a python CLI application used to automate deploying this application (both aws infra and k8s).
The application is designed to be deployed using CloudFormation. Since additional steps are required to ensure the correct API key is provided to the application, a python CLI app has been provided to coordinate gathering this API key as well as deploying the CFN stack automatically.
Please ensure you have an AWS account to launch into and have credentials configured in the standard locations as described here: Using the Default Credential Provider Chain
The following steps were developed with a linux system in mind, these should also be applicable to MacOS, a process for Windows systems has not been provided.
Python 3 is required for the CLI app.
Follow these steps to configure your local environment for deployment. These steps should be run from the base of this repository (the same location as this README.md
file.)
$ cd ./deploy
# create and use a python virtual environment (assuming BASH shell)
$ virtualenv -p $(which python3) venv
$ source ./venv/bin/activate
# install dependencies
(venv) $ pip install -r requirements.txt
# run the deploy.py application
(venv) $ ./deploy.py
Here is an example of what a deployment should look like:
(venv) $ ./deploy.py
Please provide an API Key:
creating secrets manager secret for API Key
Launching stack 'stonks-application' into us-east-1
waiting on stonks-application stack to complete launch...
stack create completed!
stonks service url: http://<redacted_ip_address>:30007
waiting on service to become available; success expected between 3-6 attempts.
attempt #1: OK?: False
attempt #2: OK?: False
attempt #3: OK?: False
attempt #4: OK?: False
attempt #5: OK?: True
successful response seen from application!
At this point the live application is accessible at the service URL provided by the CLI application.
After the stack has completed launching this service URL can also be obtained at any time by querying the stack outputs:
$ aws cloudformation describe-stacks --stack-name stonks-application --query "Stacks[0].Outputs[?OutputKey=='serviceURL'].OutputValue|[0]"
"http://<redacted_ip_address>:30007"
To build the web application (rust application) run the following command from the base of this repository.
$ cargo build --release
The following steps were used to build the docker container which has been pushed to docker hub: dockerhub/epequeno/stonks.
$ docker login
$ docker build -t stonks .
$ docker tag stonks epequeno/stonks:latest
$ docker push epequeno/stonks
The deployment script creates an AWS secret to hold the API key used in the application. Deleting this secret through the web console forces a 7 day wait before fully removing it. The secret can be forcefully deleted through the cli using the following command:
$ aws secretsmanager delete-secret --secret-id stonks-api-key --force-delete-without-recovery
The ec2 instance was launched WITHOUT an SSH key. All remote access is intended to be done through SSM session manager as described here: Starting a session (AWS CLI).
Please ensure you have the session manager plugin installed in order to start remote sessions from your local terminal: (Optional) Install the Session Manager plugin for the AWS CLI . Otherwise, a session can be started through a web-browser console session using the following procedure: Starting a session (Systems Manager console)
# get the instance ID from the stack outputs
$ aws cloudformation describe-stacks --stack-name stonks-application --query "Stacks[0].Outputs[?OutputKey=='clusterInstanceId'].OutputValue|[0]"
i-example
# use that instance ID to connect to start a remote session
$ aws ssm start-session --target i-example
sh-4.2$ sudo -i
[root@ip-x-x-x-x ~]$ kubectl get po
NAME READY STATUS RESTARTS AGE
stonks-66cd84868c-qzqp9 1/1 Running 0 26m