My first attempt to serverless API hosting

Here, I’ll demonstrate how to host an HTTP api call on AWS using Lambda and Api Gateway.

Servers are good. But there are a lot of boring tasks when it comes to maintaining a server. If I just want to publish a simple API online, serverless seems a better choice. Let’s try create an HTTP api using AWS Lambda and ApiGateway.

Create a lambda function

Create a simple python function which prints the client’s source IP. Don’t worry when testing the function in lambda console fails.

import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'headers': {'Content-type': 'application/json'},
        'body': json.dumps({'your-ip': event['requestContext']['http']['sourceIp']})
    }

Create an API gateway

On the api gatetway consone, create a simple HTTP API. Give the API a name and then leave everything else default. Once created, copy the URL of the apigateway from console. That is where the API is exposed.

Click create to create the API

Next, create a route for the apigateway. Here I create a route for GET to go to /. This API call will be read only so only GET request is needed.

For authorization, I’ll let it remain open which is the default setting.

Next, create an integration. Choose “ANY /” route, and set the integration target to the above lambda function. Ensure “Grant API Gateway permission to invoke your Lambda function” is enabled.

Moment of truth

Open the apigateway URL and the message is displayed. The actual result has been redacted.

▶ http https://85nd6u7qdi.execute-api.ap-southeast-1.amazonaws.com/  
HTTP/1.1 200 OK
Apigw-Requestid: dhO71jfiSQ0EPPg=
Connection: keep-alive
Content-Length: 29
Content-Type: application/json
Date: Fri, 09 Apr 2021 14:03:30 GMT

{
    "your-ip": "1.2.3.4"
}

Adding a custom domain name

The last step is to put this API on a custom domain name. Initially, I just created a cname on Cloudflare but that didn’t work. I even configured a CORS to allow the custom domain. Turns out I need to use the custom domain setting in api gateway.

First, create a certificate on ACM. Next, create a new custom domain on api gateway. Under API mappings, map the custom domain to my API. Finally, create a CNAME record, pointing the custom domain to the API gateway domain name (which is different from the apigateway URL).

▶ http https://myip.one27.cf
 HTTP/1.1 200 OK
 Apigw-Requestid: dkAVOhMpSQ0EJPg=
 Connection: keep-alive
 Content-Length: 29
 Content-Type: application/json
 Date: Sat, 10 Apr 2021 10:14:31 GMT
 {
     "your-ip": "1.2.3.4"
 }

Load Averages on Linux/UNIX systems

Load Averages on Linux/UNIX systems

When you run uptime or top, 3 load average numbers are displayed. They represent the 1, 5, and 15 minute load averages. In the following example, the 1 minute load average of my computer is 1.11.

▶ uptime
 22:31:24 up 13 days, 14:32,  1 user,  load average: 1.11, 0.56, 0.31

For years, I’ve used it in relative term. When a high number is shown and the system is still responsive, I set that as normal in mind. But that can’t be right. That is got to be a more scientific way of explaining the numbers. Turns out the answer is right in the man page of uptime.

System load averages is the average number of processes that are either in a runnable or uninterruptable state. A process in a runnable state is either using the CPU or waiting to use the CPU. A process in un‐ interruptable state is waiting for some I/O access, eg waiting for disk. The averages are taken over the three time intervals. Load averages are not normalized for the number of CPUs in a system, so a load aver‐ age of 1 means a single CPU system is loaded all the time while on a 4 CPU system it means it was idle 75% of the time.

man uptime


On a generic workload, let’s simplify the calculation and assume load averages is computed based solely on CPU usage. On a single core system, load averages of 1 means it’s completely busy with that 1 process for the past period of time. On a 16-core system, a load average of 16 the performance expectation should be the same. When interpreting the numbers on a monitoring system or setting up thresholds, we can divide the result by number of cores and multiple it with 100%.

Load averages is actually influenced not just by CPU. IO, network busyness, interrupts, or any other busy resource that prevents processes from completing all contribute. A process can be busy waiting for other resources to return. On some VM, even the random number pool can put the CPU on wait.

That said, load averages is a fairly holistic assessment of how busy a system is.

RDP v10.0

Quick comparison of several RDP profiles.

I usually don’t care much about RDP version, as long as I can see the remote desktop clearly. I usually set the resolution to a small one with a lower color depth (15-bit). Recently, I realize RDP has been advanced quite a bit. I can get much better remote display quality. Below is a comparison of several protocols supported by Remmina.

Continue reading “RDP v10.0”

ipfs

Brought to my attention by a friend, I gave ipfs a try and here is what I did to get started:

$ brew install ipfs
$ ipfs init
$ ipfs version
ipfs version 0.4.14
$ hostname > test.txt
$ ipfs add test.txt
added QmNqNJh8Y7s65ioV6mqqmQUeSoy7UK3BemQExzqF8163HY test.txt
$ curl https://ipfs.io/ipfs/QmNqNJh8Y7s65ioV6mqqmQUeSoy7UK3BemQExzqF8163HY
xpk-pro.comme.ca
$ ipfs get -o test.txt QmNqNJh8Y7s65ioV6mqqmQUeSoy7UK3BemQExzqF8163HY
Saving file(s) to test.txt

The file is stored locally under the blocks directory:

$ cat .ipfs/blocks/FL/CIQAOW2LW2ASR4UBAAWTYWS3OPTZQWIGXRVNJ4IR5HK52CCZ6CE6FLY.data

xpk-pro.comme.ca

Once a file is added to ipfs, it cannot be deleted.

References:

  • https://ipfs.io/docs/commands/