Skip to content

Commit fa1f10d

Browse files
Merge pull request #12 from devdattakulkarni/PaaS
Added PaaS directory
2 parents 76c5d61 + 99dbcd7 commit fa1f10d

11 files changed

Lines changed: 1191 additions & 0 deletions

File tree

PaaS/AWS-Beanstalk/README.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1) Give AWSElasticBeanstalkFullAccess managed policy permission to your IAM User
2+
3+
2) Add following Inline policy:
4+
5+
{
6+
"Version": "2012-10-17",
7+
"Statement": [
8+
{
9+
"Effect": "Allow",
10+
"Action": [
11+
"elasticbeanstalk:*",
12+
"ec2:*",
13+
"elasticloadbalancing:*",
14+
"autoscaling:*",
15+
"cloudwatch:*",
16+
"s3:*",
17+
"sns:*"
18+
],
19+
"Resource": "*"
20+
}
21+
]
22+
}
23+
24+
3) Check steps.txt inside greetings folder
25+
26+
4) Follow similar steps to deploy greetings application in your account
27+
28+
5) Point the browser to the CNAME of the environment
29+
30+

PaaS/AWS-Beanstalk/README.txt~

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1) Give AWSElasticBeanstalkFullAccess permission to the
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
# Elastic Beanstalk Files
3+
.elasticbeanstalk/*
4+
!.elasticbeanstalk/*.cfg.yml
5+
!.elasticbeanstalk/*.global.yml
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import os
2+
import time
3+
4+
from flask import request
5+
from flask import Flask, render_template
6+
import mysql.connector
7+
from mysql.connector import errorcode
8+
9+
application = Flask(__name__)
10+
app = application
11+
12+
13+
def get_db_creds():
14+
db = os.environ.get("RDS_DB_NAME", None)
15+
username = os.environ.get("RDS_USERNAME", None)
16+
password = os.environ.get("RDS_PASSWORD", None)
17+
hostname = os.environ.get("RDS_HOSTNAME", None)
18+
return db, username, password, hostname
19+
20+
21+
def create_table():
22+
# Check if table exists or not. Create and populate it only if it does not exist.
23+
db, username, password, hostname = get_db_creds()
24+
table_ddl = 'CREATE TABLE message(id INT UNSIGNED NOT NULL AUTO_INCREMENT, greeting TEXT, PRIMARY KEY (id))'
25+
26+
cnx = ''
27+
try:
28+
cnx = mysql.connector.connect(user=username, password=password,
29+
host=hostname,
30+
database=db)
31+
except Exception as exp:
32+
print(exp)
33+
34+
cur = cnx.cursor()
35+
36+
try:
37+
cur.execute(table_ddl)
38+
cnx.commit()
39+
populate_data()
40+
except mysql.connector.Error as err:
41+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
42+
print("already exists.")
43+
else:
44+
print(err.msg)
45+
46+
47+
def populate_data():
48+
49+
db, username, password, hostname = get_db_creds()
50+
51+
print("Inside populate_data")
52+
print("DB: %s" % db)
53+
print("Username: %s" % username)
54+
print("Password: %s" % password)
55+
print("Hostname: %s" % hostname)
56+
57+
cnx = ''
58+
try:
59+
cnx = mysql.connector.connect(user=username, password=password,
60+
host=hostname,
61+
database=db)
62+
except Exception as exp:
63+
print(exp)
64+
65+
cur = cnx.cursor()
66+
cur.execute("INSERT INTO message (greeting) values ('Hello, World!')")
67+
cnx.commit()
68+
print("Returning from populate_data")
69+
70+
71+
def query_data():
72+
73+
db, username, password, hostname = get_db_creds()
74+
75+
print("Inside query_data")
76+
print("DB: %s" % db)
77+
print("Username: %s" % username)
78+
print("Password: %s" % password)
79+
print("Hostname: %s" % hostname)
80+
81+
cnx = ''
82+
try:
83+
cnx = mysql.connector.connect(user=username, password=password,
84+
host=hostname,
85+
database=db)
86+
except Exception as exp:
87+
print(exp)
88+
89+
cur = cnx.cursor()
90+
91+
cur.execute("SELECT greeting FROM message")
92+
entries = [dict(greeting=row[0]) for row in cur.fetchall()]
93+
return entries
94+
95+
try:
96+
print("---------" + time.strftime('%a %H:%M:%S'))
97+
print("Before create_table")
98+
create_table()
99+
print("After create_table")
100+
except Exception as exp:
101+
print("Got exception %s" % exp)
102+
conn = None
103+
104+
105+
@app.route('/add_to_db', methods=['POST'])
106+
def add_to_db():
107+
print("Received request.")
108+
print(request.form['message'])
109+
msg = request.form['message']
110+
111+
db, username, password, hostname = get_db_creds()
112+
113+
cnx = ''
114+
try:
115+
cnx = mysql.connector.connect(user=username, password=password,
116+
host=hostname,
117+
database=db)
118+
except Exception as exp:
119+
print(exp)
120+
import MySQLdb
121+
cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
122+
123+
cur = cnx.cursor()
124+
cur.execute("INSERT INTO message (greeting) values ('" + msg + "')")
125+
cnx.commit()
126+
return hello()
127+
128+
129+
@app.route("/")
130+
def hello():
131+
print("Printing available environment variables")
132+
print(os.environ)
133+
print("Before displaying index.html")
134+
entries = query_data()
135+
print("Entries: %s" % entries)
136+
return render_template('index.html', entries=entries)
137+
138+
139+
if __name__ == "__main__":
140+
app.debug = True
141+
app.run(host='0.0.0.0')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1.0.12.tar.gz

0 commit comments

Comments
 (0)