Flask API running on AWS
This simple test application allows user to extract linguistic features from a piece of text. The app uses AWS SQS messages to keep a queue of requested jobs; it also sends output reports to GCS in JSON format.
All of this was done for practicing purposes.
Useful for debugging. The Flask application is app.api.py
python -m app.process &
export FLASK_APP=app/api.py
flask runThis execution will expose the API through localhost:5000.
Run the script build.sh to execute the application using Docker and Gunicorn in production-like environments.
The application expects receiving several environment variables from the Docker environment. When using build.sh, you can define those variables from the host using the same names.
PORT: The exposed port from the Docker container and to perform the binding with the host. Already defined inbuild.shas the port5000through the host environment variablePORT.AWS_ACCESS_KEY_ID: AWS access key for managing the SQS queue.AWS_SECRET_ACCESS_KEY: AWS secret key for managing the SQS queue.SQS_REGION_NAME: SQS needs theregion_nameparameter at construction time of the client (boto3).SQS_QUEUE_URL: SQS URL access.SPACY_MODEL_NAME: spaCy model name of the model used to perform the text parsing. Already defined inbuild.shasen_core_web_sm, which the only one available from the Docker environment.GCP_CREDENTIALS: GCP service account info (service account credentials JSON file content). Preferred to avoid storing the file in repo when building on the cloud.GCP_CREDENTIALS_FILE: GCP service account JSON file (useful when working in local). Not used inbuild.sh.GCS_OUTPUT_BUCKET: GCS bucket name for the output report.GCS_OUTPUT_PREFIX: GCS object prefix for the output report.LOG_LEVEL: Logging level of theapp.processtask. Default isERROR(this default is also hardcoded inbuild.sh).
-
/submit- method:
POST- description: Creates a job to process a piece of text, maximum size is
256 KB. - content-type:
x-www-form-urlencodedor through query string parameter. - data:
text: Piece of text to process.- type:
string
- type:
- return:
- type:
JSON - fields:
submitted:trueif the job was successfully placed in queue.- type:
boolean
- type:
id: job ID, which is also the same as the SQS message in queue.- type:
string
- type:
- status:
201if success.
- type:
- description: Creates a job to process a piece of text, maximum size is
- method:
-
/status- method:
GET- description: Checks API status. Doesn't expect parameters.
- returns:
- type:
JSON - fields:
processing_jobs:trueif there are messages in the queue or in flight. type:booleanqueue_attributes: SQS attributes values nowApproximateNumberOfMessagesandApproximateNumberOfMessagesNotVisiblecasted tonumber. type:object
- status:
200if success.
- type:
- method:
/message:- method:
GET- description: Gets a message from the SQS queue.
- returns:
- type:
JSON - fields:
message_received:trueif message received.- type:
boolean
- type:
sent_timestamp: time epoch when the SQS message was sent.- type:
string
- type:
body: SQS message body, includes senttextand atimestamptaken when starting the submit process.- type:
object
- type:
id: SQS message ID.- type:
string
- type:
- cookies:
receiptHandle: receipt handle ID to delete the message form the queue.Path:/messageMax-Age:600
- type:
- status:
200if OK,204if no messages (response empty).
- method:
DELETE- description: Deletes a message received from the SQS queue.
- returns:
- type:
JSON - fields:
message_deleted:trueif message deletion happened successfully.- type:
boolean
- type:
- type:
- method:
test_text_processing.pychecks if the parsing process is working the same way for theparsed_text_sample.json. This test aims to work as a verification step on a CD workflow using GitHub Actions.
- Continuous Deployment to AWS ECS using GitHub Actions and AWS CloudFormation.