post_db_practice
μ μ΄λ¦μΌλ‘ λλ€ ν¨μλ₯Ό μμ±νμ.pip3 install pymysql -t ./
ν΄λΉ μμΆ νμΌμ Layerλ‘ μΆκ°νμ.
post_db_practice
λλ€ ν¨μμμ Layer μΆκ°
μ½λ μμ±
import pymysql
'''
μμ
event = {}
event['cafe_id'] = 1
event['possible'] = 1
event['star'] = 1
event['consent'] = 1
event['noise'] = 1
event['table'] = 1
event['light'] = 1
event['wifi'] = 1
event['customer'] = 1
event['detail'] = "μνΈ"
event['writer'] = 1
'''
def lambda_handler(event, context):
conn = pymysql.connect(
host="[Hostname]",
user="[Username]",
password="[Password]",
db="[Database]"
)
cursor = conn.cursor()
data = []
data.append(event['cafe_id'])
data.append(event['possible'])
data.append(event['star'])
data.append(event['consent'])
data.append(event['table'])
data.append(event['noise'])
data.append(event['light'])
data.append(event['wifi'])
data.append(event['customer'])
data.append(event['detail'])
data.append(event['writer'])
print(data)
sql = 'INSERT INTO review \
(`cafe_id`, `possible`, `star`, `consent`, `table`, `noise`, `light`, `wifi`, `customer`, `detail`, `writer`) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
cursor.execute(sql, data)
conn.commit()
conn.close()
return {
"success": True
}
API urlμ /cafes/{cafe-id}/reviews
λ‘
idκ° {cafe-id}μΈ μΉ΄νμ λν νκΈ°λ₯Ό λ¨κΈ°λ μλ리μ€λΌκ³ νμ. (μ½λλ₯Ό μ΄ν΄νλ€λ©΄ λ°λ‘ μ΄ν΄ν λ―.)
리μμ€ μΆκ°
μ΄λ κ² 3κ°μ 리μμ€λ₯Ό μΆκ°νλ€.
[POST] λ©μλ μΆκ°
[ν΅ν© μμ²] μ ννκΈ°
URL κ²½λ‘ νλΌλ―Έν°λ‘ cafe-id
μ€μ νκΈ°
[맀ν ν
νλ¦Ώ] μ€μ > λ©μλ μμ² ν¨μ€μ€λ£¨
cafe_id
μ λν μ½λ μμ
import pymysql
def lambda_handler(event, context):
cafe_id = event['params']['path']['cafe-id']
body = event['body-json']
conn = pymysql.connect(
host="[Hostname]",
user="[Username]",
password="[Password]",
db="[Database]"
)
cursor = conn.cursor()
print(event)
data = []
data.append(cafe_id) # cafe_id
data.append(body['possible'])
data.append(body['star'])
data.append(body['consent'])
data.append(body['table'])
data.append(body['noise'])
data.append(body['light'])
data.append(body['wifi'])
data.append(body['customer'])
data.append(body['detail'])
data.append(body['writer'])
print(data)
sql = 'INSERT INTO \
review (`cafe_id`, `possible`, `star`, `consent`, `table`, `noise`, `light`, `wifi`, `customer`, `detail`, `writer`) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
cursor.execute(sql, data)
conn.commit()
conn.close()
return {
"success": True
}
(λ ν¨μ¨μ μΈ μ½λ μκ°ν΄λ³΄μ..)
κ·Έ ν API λ°°ν¬! νλ©΄ μ±κ³΅
++ μΈμ¦λ μ¬μ©μλ§ μ κ·Όν μ μλλ‘ API ν€ μ€μ νκΈ°
μμ² ν μ€νΈν λ
Execution failed due to configuration error: Invalid permissions on Lambda function
μ΄λ° μλ¬ λ©μΈμ§μ ν¨κ» Internal Server Errorκ° λ¬λ€.
[ν΅ν© μμ²](Integration Request)μ μ ννμ¬
μ΄λ° νμ
μ°½μ΄ λμ€λλ‘ νμ
[Lambda ν¨μ] μ νν λ λ¬λ€.