当使用 AWS Lambda 函数时,可能会遇到“目标列表中没有项目”的错误。这个错误通常表示您尚未添加任何目标以便将 Lambda 函数的输出发送到它们。这个错误可以使用以下示例代码来解决:
import boto3
def add_destination(function_name):
client = boto3.client('lambda')
response = client.list_destinations(
FunctionName=function_name
)
if len(response['Destinations']) == 0:
client.create_event_source_mapping(
EventSourceArn='arn:aws:dynamodb:us-west-2:123456789012:table/example_table/stream/2019-09-19T23:36:23.123',
FunctionName=function_name,
DestinationConfig={
'OnFailure': {
'Destination': 'failure_topic'
}
},
MaximumBatchingWindowInSeconds=123,
MaximumRecordAgeInSeconds=123,
BisectBatchOnFunctionError=True,
MaximumRetryAttempts=123
)
print(add_destination('example_function'))
这个示例会检查给定的 Lambda 函数是否具有至少一个目标。如果函数没有任何目标,则会添加一个新的目标配置。在此示例中,我们将添加一个事件源映射,它会将 Lambda 函数的输出发送到 DynamoDB 流进行处理。您可以根据自己的需求更改目标设置。