Reputation: 7819
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
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
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
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