-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-stepfn
45 lines (33 loc) · 1.11 KB
/
ws-stepfn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
import logging
import os
import boto3
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel("INFO")
def lambda_handler(event, context):
print(json.dumps(event))
endpoint_url = "https://" + "/".join([event['domain'], event['stage']])
apigateway_client = boto3.client('apigatewaymanagementapi', endpoint_url=endpoint_url)
try:
send_message(apigateway_client, event['connectionId'], event['message'])
except Exception as e:
logger.error(f"Failed to send message: {str(e)}")
return {
'statusCode': 200
}
def send_message(apigateway_client, connection_id, message):
try:
response = apigateway_client.post_to_connection(
ConnectionId=connection_id,
Data=message.encode('utf-8')
)
logger.info("Message successfully sent: %s", response)
except ClientError as err:
logger.error("couldnt send message: %s", str(e))
raise
{
"domain": "api.execute-api.us-east-1.amazonaws.com",
"stage": "dev",
"message": "hello, from Step Functions!"
}