CLOUD/Public Cloud

[AWS] API Gateway : POST - 학생 이름 입력 > 나이 반환

alsruds 2023. 3. 17. 04:48

》 post 방식 사용

》 db 연결

》 zip 파일로 업로드 한 함수 생성

 

 

Python 파일 생성 (pycharm - lambda_function.py)

import pymysql

def lambda_handler(event, context):
    # DB 연동
    conn = pymysql.connect(
        host='[AWS:RDS 엔드포인트]',
        user='[계정명]',
        password='[비밀번호]',
        db='[연결된db]',
        charset='utf8'
    )

    # 학생 이름 입력
    student = event['body-json']

    sql = "select * from student"
    
    # DB 불러오기
    with conn:
        with conn.cursor() as cur:
            cur.execute(sql)
            result = cur.fetchall()
            for data in result:
                # 입력받은 이름이 DB에 저장된 이름과 같을 때
                if data[0] == student['sname']:
                    tmp = data[1]

    # 나이 반환
    return {
        'statusCode': 200,
        'body': tmp
    }

 

압축 후 업로드 : 2023.03.17 - [분류 전체보기] - [Cloud] Lambda : 함수 생성하기 (.zip)

 

[Cloud] Lambda : 함수 생성하기 (.zip)

- AWS Lambda : 코드를 등록하면 실행해줌 》 AWS 》 Lambda 》 함수 생성 》 런타임 : python3.9 》 사용자의 입력은 event 변수에 들어감 》 코드 변경 시 deploy [ .zip 파일에서 업로드 ] ● 프로젝트 생성 (pyc

alsrudalsrudalsrud.tistory.com

 

API Gateway 연결 & 배포 : 2023.03.17 - [분류 전체보기] - [Cloud] API Gateway : POST

 

[Cloud] API Gateway : POST

[ POST 방식으로 Lambda 함수 불러오기 ] ● lambda 함수 생성 》 AWS 》 Lambda ● API Gateway 생성 》 AWS 》 API Gateway 》 API 생성 》 REST API ● Resource 생성 》 Resources 》 Actions 》 Create Resources 》 Create Method : P

alsrudalsrudalsrud.tistory.com

 

 확인하기

'park' 의 나이 20 출력~

 

'CLOUD > Public Cloud' 카테고리의 다른 글

[AWS] CloudWatch : EC2 모니터링  (2) 2023.03.17
[AWS] IAM : 새로운 사용자 권한 설정  (0) 2023.03.17
[AWS] API Gateway : POST  (0) 2023.03.17
[AWS] API Gateway : GET  (0) 2023.03.17
[AWS] Lambda : 함수 생성하기 (.zip)  (0) 2023.03.17