{"id":1835,"date":"2019-03-25T07:21:15","date_gmt":"2019-03-25T07:21:15","guid":{"rendered":"https:\/\/www.codeastar.com\/?p=1835"},"modified":"2019-09-06T09:41:20","modified_gmt":"2019-09-06T09:41:20","slug":"elastic-beanstalk-with-react-frontend-and-flask-backend-part-3","status":"publish","type":"post","link":"https:\/\/www.codeastar.com\/elastic-beanstalk-with-react-frontend-and-flask-backend-part-3\/","title":{"rendered":"Elastic Beanstalk with React Frontend and Flask Backend \u2013 Part 3"},"content":{"rendered":"\n
We have our Flask backend<\/a> at the back to handle magic (weather forecast logic) and we have our React frontend<\/a> dealing with user interfaces. So, what are we missing here? Every great team needs a leader to bring teammates working together. Thus we need a service to align both backend and frontend. Here she comes, the cloud service. There are three major cloud service providers nowadays, Amazon Web Services (AWS)<\/a>, Microsoft Azure<\/a> and Google Cloud<\/a>. Since we have used AWS in our projects<\/a> before, and AWS is still the biggest market leader and having the vastest toolsets. So we use AWS Elastic Beanstalk (EB<\/a>) to bring our EZ Weather Forecast (EZW) app to public.<\/p>\n\n\n\n\n\n\n\n From Flask official web site, the author has already stated<\/a> that “Flask\u2019s built-in server is not suitable for production<\/em>“. But don’t worry, that is the reason why we need AWS. Since Flask is just a lightweight web framework without typical web server functions, like load balancing and access control. AWS Elastic Beanstalk (EB) is the service which helps us on handling those missing functions. By using EB, we only need to focus on Flask coding and let EB handles the rest. And most importantly, we can do it FREE<\/strong>. So what are we waiting for? Let’s climb the beanstalk!<\/p>\n\n\n\n First things first, make sure you have an valid AWS account (go to register one if you don’t have, again, it is FREE<\/strong>). Then go to GitHub to clone or download our Flask backend source<\/a>. Please note that this version of Flask backend is slightly different then the original one in previous post<\/a> (we will discuss the details later).<\/p>\n\n\n\n Go to the directory with the downloaded Flask backend source, you may find that there is a “requirements.txt” file. As EB is using good old “pip<\/a>” to install packages, instead of the way we do with “pipenv<\/a>“. Now, let’ start our virtual environment using: <\/p>\n\n\n\n Then get the AWS EB Command Line Interface<\/a> (EB CLI), it is the tool we use to connect our Flask source to AWS EB. <\/p>\n\n\n\n We can start all the magic from here. But wait, according to AWS Best Practices<\/a>, we should create an individual user for handling each different task. We did it before in our Amazon ECS Docker project<\/a>. Let’s do it one more time. <\/p>\n\n\n\n Login to AWS management console, select “IAM” from “Services” menu. <\/p>\n\n\n\n In the IAM interface, click “Users” on the left, then click “Add user”.<\/p>\n\n\n\n Type a user name here. Since we are using CLI on EB operation, remember to click the “Programmatic<\/em> access<\/em>” access type. <\/p>\n\n\n\n After adding the user name, let’s create an Elastic Beanstalk group for this user.<\/p>\n\n\n\n Search the group policies with keyword, “elasticbeanstalk<\/em>“, then click “AWSElasticBeanstalkFullAccess<\/em>“. It provides all EB access rights for us to do EB deployment and other configurations.<\/p>\n\n\n\n After that, press a series of clicks until we finish the user creation. Don’t forget to save the AWS access id and secret key at the confirmation page, as we will use them in following steps.<\/p>\n\n\n\n Let’s go back to our Flask directory and start the EB deployment (Yeah! Finally!). The magic words are: <\/p>\n\n\n\n Then you will see following lines, asking which region you would like to deploy to.<\/p>\n\n\n\n Once you have entered a region number, the program will ask your credentials. That is why we ask you to save the access id and secret key earlier. :]]<\/p>\n\n\n\n Then the system will ask you the application name and your Python version:<\/p>\n\n\n\n Actually , we can use an one liner to do above things: <\/p>\n\n\n\n But I would suggest running it step-by-step for the first time AWS EB user. When all the settings are done, it is the time to deploy our EZW Flask app.<\/p>\n\n\n\n We can use all default setting and let the program deploy our app. After a successful deployment, the program will create an EB url for us with following format: http:\/\/<DNS CNAME prefix>.<region>.elasticbeanstalk.com <\/em>. For above case, it is http:\/\/ebs-ezw-dev.us-west-2.elasticbeanstalk.com<\/em> . That’s the end? No. Do you remember there are environment variables in our EZW Flask app? Other than the DARK_SKY_KEY, we have added another variable, REF_WHITE_LIST, for this version.<\/p>\n\n\n\n REF_WHITE_LIST is a referrer white list for access control. So our EZW backend will only handle requests from our white listed domains. We can set those environment variables on EB using:<\/p>\n\n\n\n From the above setting, we accept incoming API requests from “elasticbeanstalk.com” and “abc.com”. And now our EZW Flask backend is on AWS and open to public! Besides using EB CLI, we can use GUI to do all the above things as well. Let’s login to AWS management console, select “Elastic Beanstalk” from “Services” menu. <\/p>\n\n\n\n By clicking the application name, we can check the application’s health and logs, and modify version and configuration.<\/p>\n\n\n\n When we can do it all from GUI, why do we do it the CLI way? As we expect there are ongoing updates on the EZW backend, then we can use script language to automate the deployment process. Otherwise, we have to login the management console and deploy the app manually.<\/p>\n\n\n\n We have deployed our Flask backend on AWS. It is time we do the same on our React frontend. <\/p>\n\n\n\nFlask on AWS<\/h3>\n\n\n\n
Setup your Flask and EB environments<\/h3>\n\n\n\n
$pipenv shell<\/code><\/pre>\n\n\n\n
$pip install awsebcli<\/code><\/pre>\n\n\n\n
Create IAM user for Elastic Beanstalk<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
<\/figure><\/div>\n\n\n\n
<\/figure><\/div>\n\n\n\n
<\/figure><\/div>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Deploy Flask on Elastic Beanstalk<\/h3>\n\n\n\n
$eb init<\/code><\/pre>\n\n\n\n
Select a default region\n1) us-east-1 : US East (N. Virginia)\n2) us-west-1 : US West (N. California)\n3) us-west-2 : US West (Oregon)\n4) eu-west-1 : EU (Ireland)\n5) eu-central-1 : EU (Frankfurt)\n6) ap-south-1 : Asia Pacific (Mumbai)\n7) ap-southeast-1 : Asia Pacific (Singapore)\n8) ap-southeast-2 : Asia Pacific (Sydney)\n9) ap-northeast-1 : Asia Pacific (Tokyo)\n10) ap-northeast-2 : Asia Pacific (Seoul)\n11) sa-east-1 : South America (Sao Paulo)\n12) cn-north-1 : China (Beijing)\n13) cn-northwest-1 : China (Ningxia)\n14) us-east-2 : US East (Ohio)\n15) ca-central-1 : Canada (Central)\n16) eu-west-2 : EU (London)\n17) eu-west-3 : EU (Paris)\n18) eu-north-1 : EU (Stockholm)\n(default is 3): <\/code><\/pre>\n\n\n\n
You have not yet set up your credentials or your credentials are incorrect \nYou must provide your credentials.\n(aws-access-id): \n(aws-secret-key): <\/code><\/pre>\n\n\n\n
Enter Application Name\n(default is \"ebs_ezw\"): \nApplication ebs_ezw has been created.\n\nIt appears you are using Python. Is this correct?\n(Y\/n): \n\nSelect a platform version.\n1) Python 3.6\n2) Python 3.4\n3) Python 3.4 (Preconfigured - Docker)\n4) Python 2.7\n5) Python\n(default is 1): <\/code><\/pre>\n\n\n\n
$eb init -p python-3.6 ebs_ezw --region us-west-2<\/code><\/pre>\n\n\n\n
$eb create\n\nEnter Environment Name\n(default is ebs-ezw-dev): \nEnter DNS CNAME prefix\n(default is ebs-ezw-dev): <\/code><\/pre>\n\n\n\n
$eb setenv DARK_SKY_KEY=<your Dark Sky API key> REF_WHITE_LIST='elasticbeanstalk.com, abc.com'<\/code><\/pre>\n\n\n\n
<\/p>\n\n\n\nEB on AWS console<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Build the Frontend<\/h3>\n\n\n\n