Connect AWS Lambda to API Gateway for REST APIs

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ต.ค. 2024

ความคิดเห็น • 22

  • @hitesh-patil
    @hitesh-patil ปีที่แล้ว +3

    Beautifully explained. Thank you. ❤
    For the viewers-
    import json
    def lambda_handler(event, context):
    x = event['queryStringParameters']['x']
    y = event['queryStringParameters']['y']
    op = event['queryStringParameters']['op']
    print(f"x:{x}, y:{y}, op:{op}")
    res_body = {}
    res_body['x'] = int(x)
    res_body['y'] = int(y)
    res_body['op'] = op
    res_body['ans'] = add(x,y)
    http_res = {}
    http_res['statusCode'] = 200
    http_res['headers'] = {}
    http_res['headers']['Content-Type'] = 'application/json'
    http_res['body'] = json.dumps(res_body)
    return http_res
    def add(x,y):
    return x+y
    ?x=2&y=10&op=add

  • @satyaschannel4391
    @satyaschannel4391 8 หลายเดือนก่อน +5

    For those who want code he used:
    import json
    def lambda_handler(event, context):
    x=event['queryStringParameters']['x']
    y=event['queryStringParameters']['y']
    op=event['queryStringParameters']['op']
    #Log inputs
    print(f"x:{x}, y: {y}, op: {op}")
    #prepare the response_body
    res_body = {}
    res_body['x'] = int(x)
    res_body['y'] = int(y)
    res_body['op'] = op
    res_body['ans'] = add(int(x),int(y))
    #prepare http response
    http_res = {}
    http_res['statusCode'] = 200
    http_res['headers'] = {}
    http_res['headers']['Content-Type'] = 'application/json'
    http_res['body'] = json.dumps(res_body)
    return http_res
    def add(x,y):
    return x+y

    • @Slayersoft
      @Slayersoft 6 หลายเดือนก่อน

      The problem he was having in the video (I think) was that because he stored the queryStringParameters into x and y and then directly passed them into add(x, y) but they were strings. The code here in the comments converts them to int before passing them to add, but I would convert them right away as soon as you assign them to x and y because at no point in lambda_handler you need them to be strings and that saves from converting them 2 times

  • @farhadkarimi
    @farhadkarimi 2 ปีที่แล้ว +2

    Thank you this was one of the only tutorials that worked for me. I'm an swe intern at amazon btw so thanks again

  • @yamenshaheen
    @yamenshaheen 8 หลายเดือนก่อน

    Use "parseInt()" function instead of "int()". Thanks for the tutorial.

  • @yaminiraju1658
    @yaminiraju1658 หลายเดือนก่อน

    Thank you so much. This helped me a lot.

  • @VincentStevenson
    @VincentStevenson  2 ปีที่แล้ว +4

    An even better bug fix for my lambda function would have been to change lines 4 and 5 to int(event[…]) for x and y, or float() if I wanted my calculator to handle decimal numbers.

  • @sraj7284
    @sraj7284 11 หลายเดือนก่อน +1

    This was very well explained. What I'm not getting, is what are all these specific json keys in the response that you are constructing? Where are these defined?
    'statusCode', 'headers', etc?
    What if I just wanted to return a random hard coded JSON, why can't the lambda work so:
    import json
    def lambda_handler(event, context):
    print(event)
    return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!'),
    'SudentID': '100'
    'StudentName' : ''Vincent'
    'Age': '25'
    }
    Thanks

  • @westcott2204
    @westcott2204 8 หลายเดือนก่อน

    that was fun, thank you, Vincent.

  • @saulsarruff
    @saulsarruff ปีที่แล้ว

    Awesome explanation. To the point. Thanks.

  • @Ksr3740
    @Ksr3740 3 หลายเดือนก่อน

    when i try to deploy the api and access the link to display the result its throwing a message of "Missing Authentication Token" any idea what's causing this and how to solve it?

  • @adrianhenning3399
    @adrianhenning3399 ปีที่แล้ว

    Thanks for the great demo!

  • @calmingcoffeerain6217
    @calmingcoffeerain6217 9 หลายเดือนก่อน

    Thanks nice little intro.

  • @drazaie
    @drazaie ปีที่แล้ว

    Great video! Looks like queryStringParameters doesnt work in AWS with the new Python 3.10. Any ideas how to correct this?

  • @fiyatal
    @fiyatal ปีที่แล้ว

    hi i want to change path and send query string by javascript so i can filter my data. how can i do it?

  • @cyberborgman2000
    @cyberborgman2000 ปีที่แล้ว

    Great video, well explained, thanks

  • @defaultchan2115
    @defaultchan2115 2 ปีที่แล้ว +4

    {
    "message": "Internal server error"
    }

  • @krishnamadina
    @krishnamadina ปีที่แล้ว

    can you please do this using javascript without manual

  • @georgejetson1754
    @georgejetson1754 2 ปีที่แล้ว

    I mostly agree!

  • @MohanDas-ne4ci
    @MohanDas-ne4ci ปีที่แล้ว

    Thank you!

  • @viniciusodev
    @viniciusodev 8 หลายเดือนก่อน

    Thanks

  • @tomasguozden5060
    @tomasguozden5060 ปีที่แล้ว

    thanks!