"Parser must be a string or character stream, not datetime" lambda aws 函数错误 - 不知道如何修复它
"Parser must be a string or character stream, not datetime" error in lambda aws function - can't figure out how to fix it
我在尝试 save/test 这个 Lambda Boto3 函数时遇到一个非常烦人的错误。在这个问题上还有其他线程,但我花了大约 2 个小时试图调试它,但无法弄清楚我做错了什么(这可能是显而易见的)。如有任何帮助,我们将不胜感激!
{
"errorMessage": "Parser must be a string or character stream, not datetime",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 35, in lambda_handler\n a = dateutil.parser.parse(instance.launch_time)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 1358, in parse\n return DEFAULTPARSER.parse(timestr, **kwargs)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 646, in parse\n res, skipped_tokens = self._parse(timestr, **kwargs)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 722, in _parse\n l = _timelex.split(timestr) # Splits the timestr into tokens\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 207, in split\n return list(cls(s))\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 76, in __init__\n '{itype}'.format(itype=instream.__class__.__name__))\n"
]
}
import json
import boto3
import time
import datetime
import dateutil
from dateutil.parser import parse
def lambda_handler(event, context):
detailDict = event["detail"]
ec2 = boto3.resource('ec2')
instanceId = str(detailDict["instance-id"])
instance = ec2.Instance(instanceId)
instanceState = instance.state
a = dateutil.parser.parse(instance.launch_time)
b = current_time = datetime.datetime.now(launch_time.tzinfo)
# returns a timedelta object
c = a-b
print('Difference: ', c)
minutes = c.seconds / 60
print('Difference in minutes: ', minutes)
Message=str(instanceId)+" is "+str(instanceState["Name"])
return {
'statusCode': 200,
'body': Message
}
launch-time
属性 已经是日期时间 属性。你不需要解析它。
参考:
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html
我在尝试 save/test 这个 Lambda Boto3 函数时遇到一个非常烦人的错误。在这个问题上还有其他线程,但我花了大约 2 个小时试图调试它,但无法弄清楚我做错了什么(这可能是显而易见的)。如有任何帮助,我们将不胜感激!
{
"errorMessage": "Parser must be a string or character stream, not datetime",
"errorType": "TypeError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 35, in lambda_handler\n a = dateutil.parser.parse(instance.launch_time)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 1358, in parse\n return DEFAULTPARSER.parse(timestr, **kwargs)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 646, in parse\n res, skipped_tokens = self._parse(timestr, **kwargs)\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 722, in _parse\n l = _timelex.split(timestr) # Splits the timestr into tokens\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 207, in split\n return list(cls(s))\n",
" File \"/var/runtime/dateutil/parser/_parser.py\", line 76, in __init__\n '{itype}'.format(itype=instream.__class__.__name__))\n"
]
}
import json
import boto3
import time
import datetime
import dateutil
from dateutil.parser import parse
def lambda_handler(event, context):
detailDict = event["detail"]
ec2 = boto3.resource('ec2')
instanceId = str(detailDict["instance-id"])
instance = ec2.Instance(instanceId)
instanceState = instance.state
a = dateutil.parser.parse(instance.launch_time)
b = current_time = datetime.datetime.now(launch_time.tzinfo)
# returns a timedelta object
c = a-b
print('Difference: ', c)
minutes = c.seconds / 60
print('Difference in minutes: ', minutes)
Message=str(instanceId)+" is "+str(instanceState["Name"])
return {
'statusCode': 200,
'body': Message
}
launch-time
属性 已经是日期时间 属性。你不需要解析它。
参考: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html