Skip to content

Instantly share code, notes, and snippets.

@beabetterdevv
Created September 8, 2021 00:51
Show Gist options
  • Save beabetterdevv/cf61956495a47f44fb72ee8eb6456b3a to your computer and use it in GitHub Desktop.
Save beabetterdevv/cf61956495a47f44fb72ee8eb6456b3a to your computer and use it in GitHub Desktop.
Connect & Disconnect
---
import json
def lambda_handler(event, context):
print(event)
print("****")
print(context)
SendMessage - Needs AmazonAPIGatewayInvokeFullAccess IAM Policy
---
import json
import urllib3
import boto3
client = boto3.client('apigatewaymanagementapi', endpoint_url="xxxxxxxxxx.com/production")
def lambda_handler(event, context):
print(event)
#Extract connectionId from incoming event
connectionId = event["requestContext"]["connectionId"]
#Do something interesting...
responseMessage = "responding..."
#Form response and post back to connectionId
response = client.post_to_connection(ConnectionId=connectionId, Data=json.dumps(responseMessage).encode('utf-8'))
return { "statusCode": 200 }
Broadcast - Needs AmazonAPIGatewayInvokeFullAccess IAM Policy
---
import json
import urllib3
import boto3
client = boto3.client('apigatewaymanagementapi', endpoint_url="xxxxxx.com/production")
def lambda_handler(event, context):
#Extract connectionId and desired message to send from input
connectionId = event["connectionId"]
message = event["message"]
#Form response and post back to provided connectionId
response = client.post_to_connection(ConnectionId=connectionId, Data=json.dumps(message).encode('utf-8'))
print(response)
Broadcast Lambda Input Event Example
---
{
"connectionId": "FUVNdckkIAMCIZw=",
"message": "Anyone out there?"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment