Porting the numbers game to AWS Lambda is really simple. I created the Lambda function using AWS Console and added an API as trigger.
The AWS Lambda function for my numbers game is called getExprs. By adding a few lines of code in the generated index.js, and append my Javascript functions in the same file, it was working in no time. The index.js file:
exports.handler = async (event) => {
console.log("event:"+JSON.stringify(event));
var answer="no event";
if(event.body != null) {
var e=JSON.parse(event.body);
var numbers=[e.n1,e.n2,e.n3,e.n4,e.n5,e.n6], target=e.target;
answer=solveAllGames(numbers, target);
//answer=solveGame(numbers, target);
}
const response = {
statusCode: 200,
body: JSON.stringify(answer),
//body: answer,
};
console.log(answer);
return response;
};
// my functions appended here...
Testing it with Postman:Testing it in Python:
import requests
body={
"n1": 75,
"n2": 100,
"n3": 2,
"n4": 5,
"n5": 9,
"n6": 10,
"target": 110
}
try:
response = requests.post('https://xxxxxxxxxx.execute-api.ap-southeast-2.amazonaws.com/default/getExprs', json=body)
response.raise_for_status()
print(response.body.json());
except requests.exceptions.HTTPError as errh:
print(errh)
except requests.exceptions.ConnectionError as errc:
print(errc)
except requests.exceptions.Timeout as errt:
print(errt)
except requests.exceptions.RequestException as err:
print(err)
Results are:
======================= RESTART: C:\pcd_tmpl32\call.py =======================
['100+10', '2*5+100', '(2+9)*10', '(9-2)*5+75', '(75-10)+5*9', '5*9-(10-75)', '(100+5)+10/2', '(10-5)*2+100', '100*2-9*10', '100/10*(2+9)', '(100/5-9)*10', '100/5+9*10', '(5*9+10)*2', '((75+100)+5*9)/2', '(75-5)/(9-2)+100', '(75+9)/2*5-100', '((100-75)*9-5)/2', '75*2/(5+10)+100', '(75*2/10+100)-5', '75*2/10-(5-100)', '(75-5)+(100/2-10)', '(75/5+100)-10/2', '((75+5)/10+100)+2', '75/5-(10/2-100)', '(75+5)/(10-2)+100', '(75*10-100*2)/5', '((100*2-75)-5)-10', '100*2-((75+5)+10)', '(100*2-75)-(5+10)', '(100/2-(5-75))-10', '(75-100)*(5-9)+10', '(75-100)+(5+10)*9', '((75+5)+10)/9+100', '(((100-75)-5)-9)*10', '(100*9/75+10)*5', '(5*9+75)-100/10', '5*9-(100/10-75)', '75*2+(5-9)*10', '75*2-(9-5)*10', '(2-9)*(5-10)+75', '(100*2*5-10)/9', '100/5-10)*(2+9', '(100+9)+2*5/10', '(100+9)+10/2/5', '100+10)*(2*5-9', '(100/10+5*9)*2', '2*5-(9-10)*100', '(75+100)-((2+9)*5+10)', '((75+100)-(2+9)*5)-10', '(75+100)-(2*10+5*9)', '(75-100)+(2*10-5)*9', '((75+100)-(5-10)*9)/2', '(75+100)-((9-2)*10-5)', '((75+100)+(10-5)*9)/2', '(75*2+100*9)/10+5', '75*2+100/10*(5-9)', '75*2-100/10*(9-5)', '(75-5)-(100/2-9*10)', '75/5+(9*10+100)/2', '(75/5-9)*10+100/2', '((75+5)-10)/(9-2)+100', '(75*9-100)/5-10/2', '(((75-9)*2-100)-10)*5', '(((75+10)/5+100)+2)-9', '(75+10)/5-((9-100)-2)', '(100-75)-((2-9)-10)*5', '100*2*5*9/75-10', '100*2*9/75/5-10', '(100/2*9/75+5)*10', '(100*9/75*2+5)*10', '(5*9+75)/(2+10)+100']
>>>
One gripe I have about Lambda functions is that they are supposed to support multiple triggers - even the AWS Console shows so. However, the event that are passed into Lambda can have different structures depending on the channel/triggering type. For example, when integrated with a POST method of a REST API, the payload is the event - i.e. you can get the payload like n1=event.n1; however, when integrated with a POST method of a HTTP API, the payload is in the body - i.e. you can get the payload like n1=event.body.n1. This makes the Lambda function pretty much channel/trigger specific - you have to write another version of the Lambda function if you want to expose it to another trigger/channel.
See also:
No comments:
Post a Comment