将 GetFunction 与 Amazon SDK 或 CLI 配合使用 - Amazon Lambda
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

GetFunction 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 GetFunction

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

/// <summary> /// Gets information about a Lambda function. /// </summary> /// <param name="functionName">The name of the Lambda function for /// which to retrieve information.</param> /// <returns>Async Task.</returns> public async Task<FunctionConfiguration> GetFunctionAsync(string functionName) { var functionRequest = new GetFunctionRequest { FunctionName = functionName, }; var response = await _lambdaService.GetFunctionAsync(functionRequest); return response.Configuration; }
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 GetFunction

C++
适用于 C++ 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region in which the bucket was created (overrides config file). // clientConfig.region = "us-east-1"; Aws::Lambda::LambdaClient client(clientConfig); Aws::Lambda::Model::GetFunctionRequest request; request.SetFunctionName(functionName); Aws::Lambda::Model::GetFunctionOutcome outcome = client.GetFunction(request); if (outcome.IsSuccess()) { std::cout << "Function retrieve.\n" << outcome.GetResult().GetConfiguration().Jsonize().View().WriteReadable() << std::endl; } else { std::cerr << "Error with Lambda::GetFunction. " << outcome.GetError().GetMessage() << std::endl; }
  • 有关 API 详细信息,请参阅《Amazon SDK for C++ API 参考》中的 GetFunction

CLI
Amazon CLI

检索有关函数的信息

以下 get-function 示例显示有关 my-function 函数的信息:

aws lambda get-function \ --function-name my-function

输出:

{ "Concurrency": { "ReservedConcurrentExecutions": 100 }, "Code": { "RepositoryType": "S3", "Location": "https://awslambda-us-west-2-tasks.s3.us-west-2.amazonaws.com/snapshots/123456789012/my-function..." }, "Configuration": { "TracingConfig": { "Mode": "PassThrough" }, "Version": "$LATEST", "CodeSha256": "5tT2qgzYUHoqwR616pZ2dpkn/0J1FrzJmlKidWaaCgk=", "FunctionName": "my-function", "VpcConfig": { "SubnetIds": [], "VpcId": "", "SecurityGroupIds": [] }, "MemorySize": 128, "RevisionId": "28f0fb31-5c5c-43d3-8955-03e76c5c1075", "CodeSize": 304, "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-function", "Handler": "index.handler", "Role": "arn:aws:iam::123456789012:role/service-role/helloWorldPython-role-uy3l9qyq", "Timeout": 3, "LastModified": "2019-09-24T18:20:35.054+0000", "Runtime": "nodejs10.x", "Description": "" } }

有关更多信息,请参阅《Amazon Lambda 开发人员指南》中的配置 Amazon Lambda 函数选项

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 GetFunction

Go
适用于 Go V2 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

// FunctionWrapper encapsulates function actions used in the examples. // It contains an AWS Lambda service client that is used to perform user actions. type FunctionWrapper struct { LambdaClient *lambda.Client } // GetFunction gets data about the Lambda function specified by functionName. func (wrapper FunctionWrapper) GetFunction(functionName string) types.State { var state types.State funcOutput, err := wrapper.LambdaClient.GetFunction(context.TODO(), &lambda.GetFunctionInput{ FunctionName: aws.String(functionName), }) if err != nil { log.Panicf("Couldn't get function %v. Here's why: %v\n", functionName, err) } else { state = funcOutput.Configuration.State } return state }
  • 有关 API 详细信息,请参阅《Amazon SDK for Go API 参考》中的 GetFunction

JavaScript
适用于 JavaScript 的 SDK(v3)
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

const getFunction = (funcName) => { const client = new LambdaClient({}); const command = new GetFunctionCommand({ FunctionName: funcName }); return client.send(command); };
  • 有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 GetFunction

PHP
适用于 PHP 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

public function getFunction($functionName) { return $this->lambdaClient->getFunction([ 'FunctionName' => $functionName, ]); }
  • 有关 API 详细信息,请参阅《Amazon SDK for PHP API 参考》中的 GetFunction

Python
SDK for Python(Boto3)
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

class LambdaWrapper: def __init__(self, lambda_client, iam_resource): self.lambda_client = lambda_client self.iam_resource = iam_resource def get_function(self, function_name): """ Gets data about a Lambda function. :param function_name: The name of the function. :return: The function data. """ response = None try: response = self.lambda_client.get_function(FunctionName=function_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceNotFoundException": logger.info("Function %s does not exist.", function_name) else: logger.error( "Couldn't get function %s. Here's why: %s: %s", function_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise return response
  • 有关 API 详细信息,请参阅《Amazon SDK for Python (Boto3) API 参考》中的 GetFunction

Ruby
适用于 Ruby 的 SDK
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

class LambdaWrapper attr_accessor :lambda_client def initialize @lambda_client = Aws::Lambda::Client.new @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Gets data about a Lambda function. # # @param function_name: The name of the function. # @return response: The function data, or nil if no such function exists. def get_function(function_name) @lambda_client.get_function( { function_name: function_name } ) rescue Aws::Lambda::Errors::ResourceNotFoundException => e @logger.debug("Could not find function: #{function_name}:\n #{e.message}") nil end
  • 有关 API 详细信息,请参阅《Amazon SDK for Ruby API 参考》中的 GetFunction

Rust
适用于 Rust 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

/** Get the Lambda function with this Manager's name. */ pub async fn get_function(&self) -> Result<GetFunctionOutput, anyhow::Error> { info!("Getting lambda function"); self.lambda_client .get_function() .function_name(self.lambda_name.clone()) .send() .await .map_err(anyhow::Error::from) }
  • 有关 API 详细信息,请参阅《Amazon SDK for Rust API 参考》中的 GetFunction

SAP ABAP
SDK for SAP ABAP
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

TRY. oo_result = lo_lmd->getfunction( iv_functionname = iv_function_name ). " oo_result is returned for testing purposes. " MESSAGE 'Lambda function information retrieved.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdserviceexception. MESSAGE 'An internal problem was encountered by the AWS Lambda service.' TYPE 'E'. CATCH /aws1/cx_lmdtoomanyrequestsex. MESSAGE 'The maximum request throughput was reached.' TYPE 'E'. ENDTRY.
  • 有关 API 详细信息,请参阅《Amazon SDK for SAP ABAP API 参考》中的 GetFunction

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 Lambda 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。