https://github.com/fbukevin/aws-demo
simple web server demo for NCCU class
https://github.com/fbukevin/aws-demo
Last synced: 11 months ago
JSON representation
simple web server demo for NCCU class
- Host: GitHub
- URL: https://github.com/fbukevin/aws-demo
- Owner: fbukevin
- Created: 2017-12-25T03:02:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-25T03:38:20.000Z (over 8 years ago)
- Last Synced: 2025-06-11T10:56:53.348Z (about 1 year ago)
- Language: Python
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Part I - Create Web Server
1. create an AWS accout (no credit card required for register)
2. create an EC2 instance with **Amazon Linux AMI**
* Note
1. Security Group should allow ssh, http, https accessibility
2. Remeber download your PEM key and change its mode to 400 (`chmod 400 xxx.pem`)
3. connect to instance via `ssh -i .ssh/your_key.pem ec2-user@`)
4. install flask: `sudo pip install flask`
5. vim part1.py
```py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
return "Welcome!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
```
6. run `sudo python part1.py`
7. go to your web browser and connect to `` to see if the web server shows `Welcome!` to you
## Part II - Connect to Database
1. Create RDS instance
* remember to allow port 3306 for accessing in Security Group of RDS
2. sudo yum install -y mysql
3. create table
1. connect to RDS instance: `mysql –u nccu –p –h `
* use the "Master username" and "Master password"
2. select database `use nccu_demo`
3. create table
```sql
CREATE TABLE book (
`book_id` int(11) NOT NULL auto_increment,
`book_name` char(35) NOT NULL default '',
`author` char(35) NOT NULL default '',
PRIMARY KEY (`book_id`)
);
```
4. insert data
```sql
insert into book (book_name, author) values ('Introduction to AWS', 'veck');
```
5. query
```sql
select * from book;
```
4. sudo pip install flask-mysql
5. vim app.py
* insert MySQL connection snippet
```python
from flask.ext.mysql import MySQL
# create module instance
app = Flask(__name__)
mysql = MySQL()
# MySQL configuration
app.config['MYSQL_DATABASE_USER'] = '*'
app.config['MYSQL_DATABASE_PASSWORD'] = '*'
app.config['MYSQL_DATABASE_DB'] = '*'
app.config['MYSQL_DATABASE_HOST'] = '*'
mysql.init_app(app)
# Create MySQL connection instance
conn = mysql.connect()
# Create a query cursor
cursor = conn.cursor()
```
* create a new router and query with cursor
```python
@app.route("/books")
def books():
query = "SELECT * FROM book"
cursor.execute(query)
data = cursor.fetchall()
print(data)
from flask import jsonify
return jsonify(data)
```
6. run `sudo python part2.py`
7. go to your web browser and connect to `` to see if the web server shows data to you