AWS Lambda, API Gateway, RDS

μ• μ΄μš©Β·2021λ…„ 7μ›” 9일
1

API Gateway πŸ‘‰ AWD Lambda πŸ‘‰ RDS μ—°κ²°

Lambda ν•¨μˆ˜ 생성 & RDS μ—°κ²°

  • 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 Gateway μ„€μ •

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 ν•¨μˆ˜] 선택할 λ•Œ λœ¬λ‹€.

참고 링크

profile
둜그λ₯Ό λ‚¨κΈ°μž 〰️

0개의 λŒ“κΈ€