swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.13k stars 102 forks source link

Problem getting message from SQS in the lambda #179

Closed jimadamsss closed 3 years ago

jimadamsss commented 3 years ago

I have created a lambda to read json requests sent in as the body element. This is something I have done previously using a lava based lambda but I wanted to try this with Swift. My issue is that my lambda fails when the message comes in due to a missing messageId. Once this happens the lambda is in a infinite loop attempting to read the same message over and over.

This is the error from Cloudwatch 2020-12-11T23:20:35+0000 warning Lambda : lifecycleIteration=0 lambda handler returned an error: requestDecoding(Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "messageId", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"messageId\", intValue: nil) (\"messageId\").", underlyingError: nil)))

Expected behavior

What I expect to happen is the lambda to fail with an error that stops the lambda from firing again. Secondly I would like to see the raw string so this can be debugged.

This was run using Swift 5.2 and swift lambda runtime version 0.3.0

SwiftAWSLambdaRuntime version/commit hash

fabianfett commented 3 years ago

Hi @jimadamsss,

are you sure you use an SQS.Event in your function signature? An SQS.Event is the container you receive when listening for SQS.Messages.

This code from the README is false:

// Import the modules
import AWSLambdaRuntime
import AWSLambdaEvents

// In this example we are receiving an SQS Message, with no response (Void).
Lambda.run { (context, message: SQS.Message, callback: @escaping (Result<Void, Error>) -> Void) in
  ...
  callback(.success(Void()))
}

It should be:

// Import the modules
import AWSLambdaRuntime
import AWSLambdaEvents

// In this example we are receiving an SQS Event, with no response (Void).
Lambda.run { (context, message: SQS.Event, callback: @escaping (Result<Void, Error>) -> Void) in
  ...
  callback(.success(Void()))
}

We are happy to receive prs to fix it.

jimadamsss commented 3 years ago

I will give it a try!

jimadamsss commented 3 years ago

I was able to get this to work

mattmassicotte commented 3 years ago

Same thing happened to me, here's PR to address it. Thanks @fabianfett for the explanation - you did all the work here :)

https://github.com/swift-server/swift-aws-lambda-runtime/pull/181

tomerd commented 3 years ago

@mattmassicotte @jimadamsss okay to close since #181 is merged?

jimadamsss commented 3 years ago

Fine with me!

From: tomer doron notifications@github.com Date: Monday, March 8, 2021 at 4:46 PM To: swift-server/swift-aws-lambda-runtime swift-aws-lambda-runtime@noreply.github.com Cc: Jim Adams Jim.Adams@sas.com, Mention mention@noreply.github.com Subject: Re: [swift-server/swift-aws-lambda-runtime] Problem getting message from SQS in the lambda (#179)

EXTERNAL

@mattmassicottehttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmattmassicotte&data=04%7C01%7Cjim.adams%40sas.com%7Cd347be581ebd440f18ef08d8e27b9aa8%7Cb1c14d5c362545b3a4309552373a0c2f%7C0%7C0%7C637508367799765054%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=ItTDGozBgqIIkB7VfWIt1QySf3i4LZH4nuYQo5ZhIZY%3D&reserved=0 @jimadamssshttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjimadamsss&data=04%7C01%7Cjim.adams%40sas.com%7Cd347be581ebd440f18ef08d8e27b9aa8%7Cb1c14d5c362545b3a4309552373a0c2f%7C0%7C0%7C637508367799775049%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=GrmHHp311Fs4SopcfGusmqRU0ZN2JuIhy%2Bdem%2FThgho%3D&reserved=0 okay to close since #181https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fswift-server%2Fswift-aws-lambda-runtime%2Fpull%2F181&data=04%7C01%7Cjim.adams%40sas.com%7Cd347be581ebd440f18ef08d8e27b9aa8%7Cb1c14d5c362545b3a4309552373a0c2f%7C0%7C0%7C637508367799775049%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=a9sr7X%2FObvgJqat%2FZXd3U%2FoHywzKrGujtLiX2dtxOi4%3D&reserved=0 is merged?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fswift-server%2Fswift-aws-lambda-runtime%2Fissues%2F179%23issuecomment-793103084&data=04%7C01%7Cjim.adams%40sas.com%7Cd347be581ebd440f18ef08d8e27b9aa8%7Cb1c14d5c362545b3a4309552373a0c2f%7C0%7C0%7C637508367799785042%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=j5fr5I4Bchsf%2FAHx1UmHVkKpGLmQA8m7D%2FEOGMe2B%2BM%3D&reserved=0, or unsubscribehttps://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAGR6JHRQ53KBWWJB53T5T3TCVASTANCNFSM4UXM4TLQ&data=04%7C01%7Cjim.adams%40sas.com%7Cd347be581ebd440f18ef08d8e27b9aa8%7Cb1c14d5c362545b3a4309552373a0c2f%7C0%7C0%7C637508367799785042%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=wFjL3HR9Vq75Xz4ItCESsTGXiNM4xCfRJa9A29%2Bq0v0%3D&reserved=0.