This post is just to document what worked for me to get a very basic implementation of a Flask API up and running with Amazon Web Services (AWS) Elastic Beanstalk. For this I was using Flask version 0.10.1.

The main Python code comes from this Flask tutorial.

 

Elastic Beanstalk set up

First create a new Elastic Beanstalk application in AWS dashboard.

Configuration Settings

Environment Type

  • Environment tier: Web Server
  • Predefined configuration: Python (2.7)
  • Environment type: Load balancing, autoscaling

Application Version

  • Application source: Sample application (for now)

Additional Resources

  • Create RDS DB instance? (not needed)
  • Create environment inside a VPC? (not needed)

Configuration Details

  • Instance type: t1.micro (free)
  • EC2 key pair: none for now

Go ahead and create the environment. It takes 5-10 to start it up.

After it starts you’ll see the URL of your sample application near the top of the dashboard (circled yellow). You can visit this link to verify the environment started up correctly.

URL to sample application
URL to sample application

 

application.py

Here is Python code for our basic API:

import flask

base_url = 'flaskapitest-env.elasticbeanstalk.com'

application = flask.Flask(__name__)

#Set application.debug=true to enable tracebacks on Beanstalk log output.
#Make sure to remove this line before deploying to production.
application.debug=True

tasks = [
    {
        'id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
        'done': False
    },
    {
        'id': 2,
        'title': u'Learn Python',
        'description': u'Need to find a good Python tutorial on the web',
        'done': False
    }
]

@application.errorhandler(400)
def not_found(error):
    return flask.make_response(flask.jsonify( { 'error': 'Bad request' } ), 400)

@application.errorhandler(404)
def not_found(error):
    return flask.make_response(flask.jsonify( { 'error': 'Not found' } ), 404)

@application.route('/')
def hello_world():
    return "Hello world!"

@application.route('/todo/api/v1.0/tasks', methods = ['GET'])
def get_tasks():
    return flask.jsonify( { 'tasks': map(make_public_task, tasks) } )

def make_public_task(task):
    new_task = {}
    for field in task:
        if field == 'id':
            new_task['uri'] = base_url + '/todo/api/v1.0/task/' + str(task[field])
        else:
            new_task[field] = task[field]
    return new_task


if __name__ == '__main__':
    application.run(host='0.0.0.0', debug=True)

Make sure to replace the base_url variable to the URL to your application that was pointed out above.
Save this code as “application.py”

 

requirements.txt

You’ll also need a text file called requirements.txt. Save this file in the same directory as application.py.
The contents of requirements.txt (one liner):

Flask==0.10.1

 

Zip, Upload, Deploy

When both these files exist in the same folder, select and zip both of them into the same zip archive.

NOTE: Do not zip the folder itself, zip the files inside of the folder.

Now go into the Elastic Beanstalk dashboard and use the “Upload and Deploy” button to upload the zip archive.

After the dashboard comes up as green, we’re ready for a quick test.

 

Test with cURL

On Windows, open up a command line window. Make sure you have cURL installed and included in the path.

This command should return “Hello World!”:

curl -i http://testflaskapp-env.elasticbeanstalk.com

This command should return the short task list:

curl -i http://testflaskapp-env.elasticbeanstalk.com/todo/api/v1.0/tasks

Leave a reply