lony
lony

Reputation: 7819

AWS API Gateway custom URL parameter?

Setting up API Gateway with web-sockets I want embedded hardware clients reporting to this web-socket (aka the WebSocket URL). Sadly this clients are preconfigured and add their "ID" as part of the URL (see example below).

Is there any way I can extract the ID and manage it within the consecutive processing? I need the URL to identify my different clients and make appropriate responses.

WebSocket URL: wss://12dxxxxx.execute-api.eu-central-1.amazonaws.com/dev/<THIS_IS_THE_ID_OF_MY_EMBEDDED_HARDWARE>

Upvotes: 2

Views: 781

Answers (1)

Mitayshh Dhaggai
Mitayshh Dhaggai

Reputation: 89

A workaround would be to use CloudFront distribution in front of the websocket API and serve the requests via CloudFront distribution. Meaning custom domain name can be created for websocket API that will be served via CloudFront dist. The origin of the CloudFront dist would be the websocket API endpoint

https://aws.amazon.com/about-aws/whats-new/2018/11/amazon-cloudfront-announces-support-for-the-websocket-protocol/

Lambda@Edge function can be triggered on 'Origin Request' event and the function can strip the hardware id from the URL and pass it as queryString or header etc to the websocket API. The backend of the websocket API can access queryString/header and eventually get hardware id from the incoming event data

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-cloudfront-trigger-events.html

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-redirecting-examples

Note: DNS record(CNAME) is required to map the Domain Name to CloudFront dist

Sample L@E nodeJs code below:

'use strict';

exports.handler = (event, context, callback) => {
    console.log(event)
    const request = event.Records[0].cf.request;
    var hardwareId = request.uri.substr(1);

    request.origin = {
                 custom: {
                     domainName: 'xxxxxxxxxx.execute-api.us-east-1.amazonaws.com',
                     port: 443,
                     protocol: 'https',
                     path: '/dev',
                     sslProtocols: ['TLSv1', 'TLSv1.1','TLSv1.2'],
                     readTimeout: 60,
                     keepaliveTimeout: 5,
                     customHeaders: {}
                }
    };

    request.uri = '/';

    request.querystring = 'hardwareId='+hardwareId;

    callback(null, request);
};

Upvotes: 3

Related Questions