0

I have Below lambda function which is invoked by API gateway

import sys
import logging
import pymysql
import json
rds_host="rds.amazonaws.com"
name="name"
password="pass"
db_name="DB"
port = 3306
def save_events(event):
result = []
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, 
connect_timeout=30)
Bid=pymysql.escape_string("3")
with conn.cursor(pymysql.cursors.DictCursor) as cur:
 cur.execute("select exid,exercise_name,image from exercise where bid = 3")
 result = cur.fetchall()
cur.close()
print ("Data from RDS...")
print (result)
workout = json.dumps(result)
workouts=(workout.replace("\"", "'"))

def lambda_handler(event, context):
 save_events(event)
return workouts

Now in Integration Request of get method in api gateway what should i add in mapping template to get data in json format from the user and how can i pass that user value in the query(eg :select exid,exercise_name,image from exercise where bid = "user supplied value"). AM a beginner to AWS and backend development. Thanks in advance

mohith
  • 45
  • 8

1 Answers1

0

you can pass query argument. for that you have to mention querystringparameter in integration request and in mapping templet add json/application to map parameter in lambda code. cur.execute("select exid,exercise_name,image from exercise where bid = 3") replace this line with

cur.execute("select exid,exercise_name,image from exercise where bid = event['params']['querystring']['parameter_name']")

for more information check this pass parameter to lambda from api gateway

Primit
  • 825
  • 7
  • 13