Lambda 运行时即将弃用时,Lambda 会通过电子邮件予以提醒,并在 AWS Health Dashboard 和 Trusted Advisor 中提供通知。这些电子邮件和通知将列出使用运行时的函数的 $LATEST 版本。要列出所有使用特定运行时的函数版本,可以利用 AWS Command Line Interface(AWS CLI)或一种 AWS SDK。
如果有大量函数使用即将弃用的运行时,还可以借助 AWS CLI 或 AWS SDK 来确定优先更新最常调用函数的顺序。
有关如何使用 AWS CLI 和 AWS SDK 收集使用特定运行时的函数的相关数据,请参阅以下各节的内容。
列出使用特定运行时的函数版本
要使用 AWS CLI 列出使用特定运行时的所有函数版本,请运行以下命令。将 RUNTIME_IDENTIFIER
替换为要弃用的运行时系统名称,然后选择自己的 AWS 区域 名称。要仅列出 $LATEST 函数版本,请在命令中省略 --function-version ALL
。
aws lambda list-functions --function-version ALL --region
us-east-1
--output text --query "Functions[?Runtime=='RUNTIME_IDENTIFIER
'].FunctionArn"
提示
该示例命令列出了特定 AWS 账户 在 us-east-1
区域中的函数。您需要为您的账户拥有函数的每个区域以及每个 AWS 账户 重复此命令。
您还可以使用一种 AWS SDK 列出使用特定运行时的函数。以下示例代码使用 V3 适用于 JavaScript 的 AWS SDK 和 适用于 Python (Boto3) 的 AWS SDK 来返回使用特定运行时的函数的函数 ARN 列表。该示例代码还会返回列出的各函数的 CloudWatch 日志组。您可以使用该日志组来查找该函数的上次调用日期。有关更多信息,请参阅下文的识别最常用和最近调用的函数小节。
例 用于列出使用特定运行时的函数的 JavaScript 代码
import { LambdaClient, ListFunctionsCommand } from "@aws-sdk/client-lambda";
const lambdaClient = new LambdaClient();
const command = new ListFunctionsCommand({
FunctionVersion: "ALL",
MaxItems: 50
});
const response = await lambdaClient.send(command);
for (const f of response.Functions){
if (f.Runtime == '<your_runtime>
'){ // Use the runtime id, e.g. 'nodejs18.x' or 'python3.9'
console.log(f.FunctionArn);
// get the CloudWatch log group of the function to
// use later for finding the last invocation date
console.log(f.LoggingConfig.LogGroup);
}
}
// If your account has more functions than the specified
// MaxItems, use the returned pagination token in the
// next request with the 'Marker' parameter
if ('NextMarker' in response){
let paginationToken = response.NextMarker;
}
要了解有关使用 AWS SDK 通过 ListFunctions 操作列出函数的更多信息,请参阅您首选编程语言的 SDK 文档
您还可以使用 AWS Config 高级查询功能列出使用受影响运行时系统的所有函数。此查询仅返回函数的 $LATEST 版本,但您可以使用单个命令聚合查询以列出所有区域和多个 AWS 账户 中的函数。要了解更多信息,请参阅《AWS Config 开发人员指南》中的查询 AWS Auto Scaling 资源的当前配置状态。
识别最常用和最近调用的函数
如果 AWS 账户 包含的函数使用即将弃用的运行时,则可能需要优先更新经常调用的函数或最近调用的函数。
如果只有几个函数,则可以使用 CloudWatch Logs 控制台通过查看函数的日志流来收集这些信息。有关更多信息,请参阅 View log data sent to CloudWatch Logs。
要查看最近调用函数的次数,您还可以利用 Lambda 控制台中显示的 CloudWatch 指标信息。要查看此信息,请执行以下操作:
-
打开 Lamba 控制台的函数页面
。 -
选择您想要查看调用统计信息的函数。
-
选择监控选项卡。
-
使用日期范围选择器设置要查看统计数据的时间段。最近调用显示在调用窗格中。
对于有大量函数的账户,借助 AWS CLI 或一种 AWS SDK,通过 DescribeLogStreams 和 GetMetricStatistics API 操作以编程方式收集这些数据可能更高效。
以下示例提供了使用 V3 适用于 JavaScript 的 AWS SDK 和 适用于 Python (Boto3) 的 AWS SDK 的代码片段,用于识别特定函数的上次调用日期,并确定过去 14 天内特定函数的调用次数。
例 用于查找函数上次调用时间的 JavaScript 代码
import { CloudWatchLogsClient, DescribeLogStreamsCommand } from "@aws-sdk/client-cloudwatch-logs";
const cloudWatchLogsClient = new CloudWatchLogsClient();
const command = new DescribeLogStreamsCommand({
logGroupName: '<your_log_group_name>
',
orderBy: 'LastEventTime',
descending: true,
limit: 1
});
try {
const response = await cloudWatchLogsClient.send(command);
const lastEventTimestamp = response.logStreams.length > 0 ?
response.logStreams[0].lastEventTimestamp : null;
// Convert the UNIX timestamp to a human-readable format for display
const date = new Date(lastEventTimestamp).toLocaleDateString();
const time = new Date(lastEventTimestamp).toLocaleTimeString();
console.log(`${date} ${time}`);
} catch (e){
console.error('Log group not found.')
}
提示
您可以使用 ListFunctions API 操作来查找函数的日志组名称。有关如何执行此操作的示例,请参阅列出使用特定运行时的函数版本中的代码。
例 用于查找过去 14 天内调用次数的 JavaScript 代码
import { CloudWatchClient, GetMetricStatisticsCommand } from "@aws-sdk/client-cloudwatch";
const cloudWatchClient = new CloudWatchClient();
const command = new GetMetricStatisticsCommand({
Namespace: 'AWS/Lambda',
MetricName: 'Invocations',
StartTime: new Date(Date.now()-86400*1000*14), // 14 days ago
EndTime: new Date(Date.now()),
Period: 86400 * 14, // 14 days.
Statistics: ['Sum'],
Dimensions: [{
Name: 'FunctionName',
Value: '<your_function_name>
'
}]
});
const response = await cloudWatchClient.send(command);
const invokesInLast14Days = response.Datapoints.length > 0 ?
response.Datapoints[0].Sum : 0;
console.log('Number of invocations: ' + invokesInLast14Days);