Skip to content

Instantly share code, notes, and snippets.

View serverlessunicorn's full-sized avatar
💭
Perf testing Lambda networking...

Tim Wagner serverlessunicorn

💭
Perf testing Lambda networking...
View GitHub Profile
@serverlessunicorn
serverlessunicorn / APIGW_websocket_IAM_Auth_HowTo
Last active June 22, 2023 08:49
How to client-side IAM auth an Amazon API Gateway WebSocket connection
async def connect():
# Create a version of the websocket client class that handles AWS sigv4
# authorization by overriding the 'write_http_request' method with the
# logic to construct an x-amzn-auth header at the last possible moment.
def class WebSocketSigv4ClientProtocol(WebSocketClientProtocol):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
def write_http_request(self, path: str, headers) -> None:
# Intercept the GET that initiates the websocket protocol at the point where
# all of its 'real' headers have been constructed. Add in the sigv4 header AWS needs.