英文:
Lambda Function exits unsuccessfully
问题
以下是您提供的内容的翻译部分:
我在创建一个主要功能为JWT令牌验证的Java Lambda函数时遇到了一个奇怪的问题。我正在使用terraform创建lambda,并将我的Jar文件上传到s3。以下是令牌验证代码块:
public boolean validate(String token) throws Exception {
try {
Jws<Claims> claims = Jwts.parser().setSigningKey("mysigningkey").parseClaimsJws(token);
if (claims.getBody().getExpiration().before(new Date())) {
throw new Exception("Token expired");
}
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new InvalidJwtAuthenticationException("Some exception");
}
}
问题出在以下这行代码上:
Jws<Claims> claims = Jwts.parser().setSigningKey("mysigningkey").parseClaimsJws(token);
我已经将异常和错误放在了catch块中,但没有打印出任何消息。然而,这行代码无法执行。我尝试在AWS控制台中手动创建带有相同Jar文件的函数,相同的代码可以正常工作。
对于这种行为有任何想法吗?或者有任何调试策略吗?在CloudWatch日志中,它只打印了执行未成功,没有其他具体的错误消息。
以下消息被写入CloudWatch日志:
REPORT RequestId: ####### Duration: 1835.00 ms Billed Duration: 1900 ms Memory Size: 512 MB Max Memory Used: 93 MB Init Duration: 349.72 ms
超时设置为3秒
英文:
I am having a weird problem while creating a Java Lambda function which has a primary function of JWT token validation. I am using terraform for the creation of the lambda and uploading my Jar file to the s3 for now. The below is the token validation block
public boolean validate(String token) throws Exception {
try {
Jws<Claims> claims = Jwts.parser().setSigningKey("mysigningkey").parseClaimsJws(token);
if (claims.getBody().getExpiration().before(new Date())) {
throw new Exception("Token expired");
}
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new InvalidJwtAuthenticationException("Some exception");
}
}
This line is causing the problem
Jws<Claims> claims = Jwts.parser().setSigningKey("mysigningkey").parseClaimsJws(token);
I have put the Exception and Error in the catch block but it's not printing any message. But this line fails to execute. I tried to manually create the function in the AWS console with the same Jar file and the same code worked fine.
Any idea of this behavior? Or any debugging strategies? On the cloud watch logs it just printed execution was not successful and no other specific error messages.
The following message is written to the Cloudwatch logs:
REPORT RequestId: ####### Duration: 1835.00 ms Billed Duration: 1900 ms Memory Size: 512 MB Max Memory Used: 93 MB Init Duration: 349.72 ms
Timeout set as 3 Seconds
答案1
得分: 0
这是由于超时问题引起的。在创建 Lambda 时,默认的超时时间为 3 秒,因为令牌验证需要更多时间,Lambda 在达到时间限制时退出。将超时时间增加到 15 秒,现在正常运行。但是在日志中我没有看到任何错误消息,这暗示着问题是由于超时引起的。
英文:
This was due to a timeout issue. The default timeout on Lambda when creating is 3 seconds because the token validation takes more time Lambda was exiting reaching the time limit. Increased the timeout to 15 sec and it is working fine. But I have not seen any error message in the logs which is hinting me that it is because of the timeout
专注分享java语言的经验与见解,让所有开发者获益!
评论