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.

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" }