Menu
blog.headdesk.me
blog.headdesk.me

My first attempt to serverless API hosting

Posted on 2021/04/092021/07/25

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

facebookShare on Facebook
TwitterTweet

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Full text search

Recent Posts

  • Terraform and segregated permissions
  • LVM Compression and Deduplication
  • Edit gpg encrypted file with vim
  • Lelit Elizabeth PL92T Pressure Tuning
  • jq transformation
  • aws (8)
  • coffee (1)
  • headfi (1)
  • linux (6)
  • others (58)
  • security (2)
  • tech (36)
  • wordpress (2)

apache aws awscli azure backup clearlinux cloud coffee docker DOCP ec2 EL8 epyc espresso featured gpg jenkins kernel lelit linux lvm meltdown memory MFA mikrotik php python rdp Redhat RHEL roasting rpm Ryzen site-to-site snapshot spectre tech terraform tuning ubuntu ubuntu upgrade vim vpn wordpress xdotool

©2023 blog.headdesk.me | Powered by SuperbThemes & WordPress