Connect AWS Lambda to API Gateway for REST APIs

preview_player
Показать описание
I show how to use API Gateway to expose your Lambda Functions in AWS. I walk through creating (and debugging) a simple calculator app that has a URL endpoint and query string parameters with an http response.
Рекомендации по теме
Комментарии
Автор

Beautifully explained. Thank you. ❤


For the viewers-

import json

def lambda_handler(event, context):
x =
y =
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'] = {}
= '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

hitesh-patil
Автор

For those who want code he used:

import json

def lambda_handler(event, context):



#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'] = {}
= 'application/json'
http_res['body'] = json.dumps(res_body)

return http_res

def add(x, y):
return x+y

satyaschannel
Автор

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

farhadkarimi
Автор

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.

VincentStevenson
Автор

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

sraj
Автор

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

yamenshaheen
Автор

Awesome explanation. To the point. Thanks.

saulsarruff
Автор

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

drazaie
Автор

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?

krishnarao
Автор

{
"message": "Internal server error"
}

defaultchan
Автор

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

fiyatal
Автор

can you please do this using javascript without manual

krishnamadina