У меня есть две лямбда-функции, написанные на python.
1- subscriber.py (подключится к AWS MQ, соберет и отобразит сообщение)
import time
import boto3
import stomp
import json
global message
lambda_client = boto3.client('lambda')
class Listener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
def on_message(self, headers, message):
print('received a message in subscriber : "%s"' % message)
invoke_response = lambda_client.invoke(FunctionName="op_worker",
InvocationType='Event',
Payload=json.dumps(message)
)
#print('invoke_response a message in subscriber : "%s"' % invoke_response)
print("terstesfsff")
print (invoke_response['Payload'].read())
def lambda_handler(event, context):
conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-1.mq.eu-central-1.amazonaws.com', 61614)])
lst = Listener()
conn.set_listener('Listener', lst)
conn.set_ssl(for_hosts=[('b-4714-4441-8166-1.mq.eu-central-1.amazonaws.com', 61614)])
conn.start()
print('CONNECTION Started')
conn.connect(login='test_mq', passcode='test_secure_mq',wait=True)
print('CONNECTION established')
conn.subscribe(destination='/queue/', id=1, ack='auto')
print('CONNECTION Subscribed')
time.sleep(10)
conn.disconnect()
return
2- worker.py (который должен собирать сообщения из функции subscriber.py ипередать это сообщение в Kinesis)
import time
import boto3
import stomp
import json
lambda_client = boto3.client('lambda')
kinesis_client = boto3.client('kinesis')
def lambda_handler(event, context):
#print (invoke_response['Payload'].read())
print('received a message in worker : "%s"' % message)
kinesis_client.put_record(
StreamName='',
Data=b'bytes',
PartitionKey='1'
)
return {
'message' : message
}
Сообщение об ошибке во время выполнения лямбда-функции subscriber.py: -
Response:
null
Request ID:
"009406bc-f334-11e8-950f-fde41035f262"
Function Logs:
START RequestId: 009406bc-f334-11e8-950f-fde41035f262 Version: $LATEST
CONNECTION Started
CONNECTION established
CONNECTION Subscribed
received a message in subscriber : "{
"rateChange":{
"contractCode":"DOPC/DOPC0004/W19",
"sourceMarketGroup":"TNO"
}
}"
terstesfsff
b''
END RequestId: 009406bc-f334-11e8-950f-fde41035f262
REPORT RequestId: 009406bc-f334-11e8-950f-fde41035f262 Duration: 10255.69 ms Billed Duration: 10300 ms Memory Size: 128 MB Max Memory Used: 33 MB
Сообщение об ошибке на worker.py:-
{
"errorMessage": "name 'message' is not defined",
"errorType": "NameError",
"stackTrace": [
[
"/var/task/op_worker.py",
12,
"lambda_handler",
"print('received a message in worker : \"%s\"' % message)"
]
События подписчика Cloudwatch
События рабочего Cloudwatch