Sunday 24 January 2021

Numbers Game - Part 6 - AWS Lambda

 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:

Another day, another problem solved

Background

Bought new components to assemble:
  • Motherboard: TUF Gaming x570-plus (wi-fi) with BIOS version 2.20.2407 (?)
  • CPU: AMD Ryzen 7 5800X
  • Video card: GeForce RTX 3080
  • other bits and bobs

Problem

When boot up, the screen got stuck at 'press F2 or DEL to enter EFTI BIOS'; however, it does not respond to any keyboard events. It did respond to the keyboard a couple of times but eventually refused to respond and nothing seemed to worked - tried:
  • removed SSD card(s) 
  • reset CMOS by connecting the CLR jumper
  • reset CMOS by removing the battery
  • removed one of the RAM cards
  • fingers crossed 😏
  • toes crossed 😕
  • cursing 😠
  • 😖😡😢😣😩😱
Even after entering BIOS and changed boot sequence, when trying to install Windows 10, it got stuck at Windows logo and would not budge. 

Solution

Out of desperation, I whipped out my 10-year old video card from another computer and put it in instead. Upon boot up, et voila! I could go into the BIOS settings. Suspecting that the default BIOS firmware version did not support either the CPU or the graphics card, the first thing to do was to update the BIOS (into version 2.20.2607). After the update, it was smooth sailing - the Windows installation proceeded smoothly! Then it was a simply matter of swapping out the graphics card and continue with the driver downloads and setups.

Monday 11 January 2021

Freedom