[AWS] Flask application deployment preparation edition that tried to build a Python environment with eb [Elastic Beanstalk]

http://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/create-deploy-python-flask.html I will work along with

Prerequisites

[AWS] I tried to build a Python environment with eb Development environment is at least in an environment where it can be done.

In short, it is OK if you can use the following 4

Set up a virtual environment

Work with my client (I'm a Mac)

variable


VIRTUAL_ENV_NAME='eb-test-Flask'

command


VIRTUAL_ENV_NAME='eb-test-Flask'
cd ~
virtualenv ${VIRTUAL_ENV_NAME}

result


New python executable in /Users/****/eb-test-Flask/bin/python
Installing setuptools, pip, wheel...done.

Running a virtual environment

command


source ~/${VIRTUAL_ENV_NAME}/bin/activate

OK if the environment name is attached at the beginning of the command prompt with ()

Verification


(eb-test-Flask) 

Introducing Flask

command


pip install flask==0.10.1

Verification


pip freeze

result


Flask==0.10.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.9

If the above result is output, it is successful.

Create a Flask application

Next, create an application to deploy using Elastic Beanstalk. Here we will create a RESTful web service called "Hello World".

Verification The environment name must be in parentheses at the beginning of the command prompt. With this procedure (eb-test-Flask)

Creating a project directory

Verification


pwd

result


/Users/****/eb-test-Flask

variable


PJ_DIR_NAME='eb-flask'

command


mkdir ${PJ_DIR_NAME}

Verification


ls | grep ${PJ_DIR_NAME}

result


eb-flask

command


cd ${PJ_DIR_NAME}

Verification


pwd

result


/Users/****/eb-test-Flask/eb-flask

Create a sample application The code is excerpted from the documentation and used as is

application.py


from flask import Flask

# print a nice greeting.
def say_hello(username = "World"):
    return '<p>Hello %s!</p>\n' % username

# some bits of text for the page.
header_text = '''
    <html>\n<head> <title>EB Flask Test</title> </head>\n<body>'''
instructions = '''
    <p><em>Hint</em>: This is a RESTful web service! Append a username
    to the URL (for example: <code>/Thelonious</code>) to say hello to
    someone specific.</p>\n'''
home_link = '<p><a href="/">Back</a></p>\n'
footer_text = '</body>\n</html>'

# EB looks for an 'application' callable by default.
application = Flask(__name__)

# add a rule for the index page.
application.add_url_rule('/', 'index', (lambda: header_text +
    say_hello() + instructions + footer_text))

# add a rule when the page is accessed with a name appended to the site
# URL.
application.add_url_rule('/<username>', 'hello', (lambda username:
    header_text + say_hello(username) + home_link + footer_text))

# run the app.
if __name__ == "__main__":
    # Setting debug to True enables debug output. This line should be
    # removed before deploying a production app.
    application.debug = True
    application.run()

Run application.py

Make sure you are in the working directory under the virtual environment. (If you haven't done anything else from the process so far, it's okay)

Verification


pwd

result


/Users/****/eb-test-Flask/eb-flask

command


python application.py

result


 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 344-661-719

Launch your browser Go to http://127.0.0.1:5000/.

Verification


curl http://127.0.0.1:5000/

result


    <html>
<head> <title>EB Flask Test</title> </head>
<body><p>Hello World!</p>

    <p><em>Hint</em>: This is a RESTful web service! Append a username
    to the URL (for example: <code>/Thelonious</code>) to say hello to
    someone specific.</p>
</body>

If you can confirm it, you can stop it.

command


Control + C

Set up Flask application for eb

Since the application was able to run in my environment, I will implement it in eb

Caution I will continue to work in a virtual environment

command


cd ~/${VIRTUAL_ENV_NAME}/${PJ_DIR_NAME}
pwd

result


/Users/****/eb-test-Flask/eb-flask

command


pip freeze > requirements.txt

Verification


cat requirements.txt

result


Flask==0.10.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.9

Elastic Beanstalk uses requirements.txt to determine which packages to install on the EC2 instance that runs your application.

So this work is necessary. And the file name must also be requirements.txt.

End of virtual environment

command


deactivate

It's okay if the () at the beginning of the prompt disappears

Directory configuration check

Verification


tree ~/${VIRTUAL_ENV_NAME}/${PJ_DIR_NAME}

result


/Users/****/eb-test-Flask/eb-flask/
├── application.py
└── requirements.txt

Continued

Flask application deployment that tried to build Python environment with eb

Recommended Posts

[AWS] Flask application deployment preparation edition that tried to build a Python environment with eb [Elastic Beanstalk]
[AWS] Flask application deployment version that tried to build a Python environment with eb [Elastic Beanstalk]
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
I tried to build a Mac Python development environment with pythonz + direnv
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Build a machine learning application development environment with Python
How to build a python2.7 series development environment with Vagrant
A note that deployed a Python application from Circle CI to Elastic Beanstalk and notified Slack
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
[Python] A memo that I tried to get started with asyncio
I tried to make a 2channel post notification application with Python
I want to build a Python environment
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
A memo that allows you to change Pineapple's Python environment with pyenv
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python
Build a WardPress environment on AWS with pulumi
I tried to make a simple mail sending application with tkinter of Python
Build a python environment with ansible on centos6
[Python] Build a Django development environment with Docker
When I tried to create a virtual environment with Python, it didn't work
Create a python3 build environment with Sublime Text3
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
[No need to build local environment] Deploy Python bottle application made with Cloud9 to Heroku
I tried to build an environment for machine learning with Python (Mac OS X)
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
I tried to draw a route map with Python
Build a python environment for each directory with pyenv-virtualenv
How to build a Django (python) environment on docker
[Go + Gin] I tried to build a Docker environment
A server that echoes data POSTed with flask / python
Build a python virtual environment with virtualenv and virtualenvwrapper
How to build a Python environment on amazon linux 2
[Python] I tried to make an application that calculates salary according to working hours with tkinter
I tried uploading / downloading a file to AWS S3 / Azure BlobStorage / GCP CloudStorage with Python
I tried to make a generator that generates a C # container class from CSV with Python
I tried to build a service that sells machine-learned data at explosive speed with Docker
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
How to build a new python virtual environment on Ubuntu
How to convert an array to a dictionary with Python [Application]
I made a simple book application with python + Flask ~ Introduction ~
From environment construction to deployment for flask + Heroku with Docker
Build a Python environment and transfer data to the server
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
Build python3 environment with ubuntu 16.04
Build python environment with direnv
A story that didn't work when I tried to log in with the Python requests module
Build a Python environment offline
Web application with Python + Flask ② ③
Create a Python3.4 + Nginx + uWSGI + Flask Web application execution environment with haste using pyenv on Ubuntu 12.04
Web application with Python + Flask ④
I tried to create a server environment that runs on Windows 10
I tried to solve the ant book beginner's edition with python
Web application created with Python + Flask (using VScode) # 1-Virtual environment construction-
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7