This is a step by step look at using the Elastic Beanstalk Flask Baseline repository to get up and running on AWS Elastic Beanstalk with Flask in a very rapid fashion.
As mentioned in the Readme of the baseline repository, there are a few things you’ll need:
I’ll cover the details of how to setup Virtualenv, Flask and Elastic Beanstalk to work together in a future post. For now, it’s not strictly necessary, as we’ll be deploying straight to Elastic Beanstalk.
Now that you’re all setup with the software required, the first thing we’re goign to do is clone the baseline repository. This sets you up with a shell application that is deployable to Elastic Beanstalk without any changes. It’s useful to clone, and then deploy, to verify your configuration and setup is correct, before jumping into any issues with code/APIs/etc.
To clone the git repository, open a shell and run this command: $git clone https://github.com/adamcrosby/elastic-flask-baseline.git which should produce a bit of output, and create a new directory called ‘elastic-flask-baseline’.
Now that you have the baseline repository cloned into a local directory, you need to setup the Elastic Beanstalk CLI tools from AWS. For my Mac, I run the following command: $export PATH=$PATH:/~Downloads/AWS-ElasticBeanstalk-CLI-2.1/eb/macosx/python2.7 which places the python2.7 version of the ‘eb’ script in my path. For Windows or Linux, add the appropriate exectuable/script to your path.
Next, we initialize the repository as an Elastic Beanstalk repository. This creates a
.elasticbeanstalk directory, which holds the configuration files for the deployment.
This command runs you through a wizard to generate the proper configuration for the repository.
$eb init To get your AWS Access Key ID and Secret Access Key, visit "https://aws-portal.amazon.com/gp/aws/securityCredentials". Enter your AWS Access Key ID: mysecretkey Enter your AWS Secret Access Key: lolright Select an AWS Elastic Beanstalk service region. Available service regions are: 1) US East (Virginia) 2) US West (Oregon) 3) US West (North California) 4) EU West (Ireland) 5) Asia Pacific (Tokyo) Select: (1 to 5): 2 Enter an AWS Elastic Beanstalk application name (auto-generated value is "elastic-flask-baseline"): Enter an AWS Elastic Beanstalk environment name (auto-generated value is "elastic-flask-baseline-"): Select a solution stack. Available solution stacks are: 1) 32bit Amazon Linux running PHP 5.3 2) 64bit Amazon Linux running PHP 5.3 3) 64bit Windows Server 2008 R2 running IIS 7.5 4) 32bit Amazon Linux running Tomcat 7 5) 64bit Amazon Linux running Tomcat 7 6) 32bit Amazon Linux running Tomcat 6 7) 64bit Amazon Linux running Tomcat 6 8) 32bit Amazon Linux running Python 9) 64bit Amazon Linux running Python Select: (1 to 9): 8 Create an RDS DB Instance? [y/n]: n Updated AWS Credential file at "/Users/adam/.elasticbeanstalk/aws_credential_file".
Once this wizard is complete, you can run
eb start to bring the entire Elastic Beanstalk environment online.
$eb start Starting application "elastic-flask-baseline". Waiting for environment "env-baseline" to launch. 2012-08-31 02:55:13 INFO createEnvironment is starting. 2012-08-31 02:55:19 INFO Using elasticbeanstalk-us-west-2-735087252067 as Amazon S3 storage bucket for environment data. 2012-08-31 02:56:00 INFO Created load balancer named: awseb-e-u-AWSEBLoa-1QIBH39BDHRUO .... Omitting lots more lines .... Application is available at "env-baseline-qfkmndpypd.elasticbeanstalk.com".
Now that the Elastic Beanstalk environment is configured and started, it is running on the URL listed in the last line of output from
eb start, in this case
http://env-baseline-qfkmndpypd.elasticbeanstalk.com. If you go to this page, you’ll see a ‘Congratulations’ message from AWS, showing that the environment has been properly configured.
Now, lets get the Flask application running in that environment!
To deploy your source code to the newly created Elastic Beanstalk environment is extremely simple:
This command produces a tiny bit of output, and the returns your to your command line. In the background, AWS is reconfiguring your Elastic Beanstalk environment to properly support your newly deployed Flask application.
$git aws.push Counting objects: 16, done. Delta compression using up to 4 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (16/16), 1.93 KiB, done. Total 16 (delta 1), reused 3 (delta 0) remote: To https://apikey:firstname.lastname@example.org/repos/656c6173742d666c61736b2d/env-baseline * [new branch] HEAD -> master
Once you’ve done the
git aws.push, it will take a few minutes to fully reconfigure (almost as long as the initial standup took). To check if your baseline application is ready to be served yet, use the
eb status command within the repository:
$ eb status URL : env-baseline-qfkmndpypd.elasticbeanstalk.com Status : Updating Health : Grey
The ‘Health’ and ‘Status’ items will say ‘Updating’ and ‘Grey’ respectively for a few minutes. When they switch to ‘Ready’ and ‘Green’, your application is up and running.
$eb status URL : env-baseline-qfkmndpypd.elasticbeanstalk.com Status : Ready Health : Green