[英]lambda triggered by sqs test event not showing any logs only 200
這個 lambda 由 sqs 觸發並從 sqs 獲取消息並更新 dynamodb 表。 2 個策略附加到 lambda - 由 sqs(獲取隊列)調用並將項目放入 dynamodb 表。
import boto3
import json
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
queue = boto3.resource('sqs', region_name='us-east-1').get_queue_by_name(QueueName="erjan")
table = boto3.resource('dynamodb', region_name='us-east-1').Table('Votes')
def process_message(message):
logging.info('----------process_message----------------------')
logging.info('-------------SQS auto genereated msg------------------------')
logging.info(type(message))
try:
logging.info('----------process_message----------------------')
payload = message.message_attributes
voter = payload['voter']['StringValue']
vote = payload['vote']['StringValue']
logging.info("Voter: %s, Vote: %s", voter, vote)
update_count(vote)
message.delete()
except Exception as e:
print('-----EXCEPTION-----')
def update_count(vote):
logging.info('update count....')
cur_count = 0
if vote == 'b':
logging.info('vote is b - update...')
response = table.get_item(Key = {'voter':'count'})
item = response['Item']
item['b'] +=1
table.put_item(Item = item)
elif vote == 'a':
logging.info('vote is a - update...')
table.update_item(
Key={'voter':'count'},
UpdateExpression="ADD a :incr",
ExpressionAttributeValues={':incr': 1})
def lambda_handler(event,context):
logging.info('--------inside main-------')
try:
logging.info('--------------------------------------')
logging.info(event)
logging.info('------------------------inside try - queue.receive_messages-------------')
messages = queue.receive_messages(MessageAttributeNames=['vote','voter'])
logging.info(messages)
logging.info('--------------------------------------')
for message in messages:
logging.info('----------every msg -------------')
print('----------every msg -------------')
process_message(message)
return {'statusCode': 200, 'body': '{"status": "success"}'}
except Exception as e:
logging.error(e)
return {'statusCode': 500, 'body': '{"status": "error"}'}
測試事件:
{
"Records": [
{
"messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78",
"receiptHandle": "MessageReceiptHandle",
"body": "",
"attributes": {
"ApproximateReceiveCount": "1"
},
"messageAttributes": {
"vote": {
"Type": "String",
"StringValue": "b"
},
"voter": {
"Type": "String",
"StringValue": "count"
}
},
"awsRegion": "us-east-1"
}
]
}
運行測試事件僅返回結果 200 成功。 但它根本不顯示 lambda_handler() function 的日志和打印。
我檢查了 cloudwatch 日志和相同的 output - 只有 3 行“開始 request_id,結束 request_id,報告”。 lambda 實際上只檢查 sqs 隊列(它確實存在)並且不使用事件上下文。
但它甚至不打印基本的 logging.info('--------inside main--------') 或嘗試下的其他日志:
logging.info('--------------------------------------')
logging.info(event)
logging.info('------------------------inside try - queue.receive_messages-------------')
負載 object 正在調用錯誤的 object。
payload = message.message_attributes
它應該是:
payload = record['messageAttributes']
那一件事,也有幾點。 就像您需要解析 json object 才能調用 json 屬性一樣。 當 sqs 事件擴展時,您可能只有一個事件,您可能需要使用以下方法處理它們:
for record in event['Records']:
payload = loads(record['body'], parse_float=str)
voter = record['messageAttributes']['Voter']['stringValue']
我發現此博客對您的案例很有用,尤其是 python 中給出的示例,第 2步 https://hevodata.com/learn/sqs-to-dynamodb/
由於某種原因,僅在 aws 控制台中添加 sqs 觸發器不起作用。 我必須在 aws 配置中添加基於資源的策略聲明。
然后您可以從 sqs 控制台發送測試消息(事件)-“發送和接收消息”按鈕。
觸發器下應該是 2 個東西——sqs 和 lambda。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.