DeleteFunction 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DeleteFunction 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DeleteFunction

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
AWS SDK for .NET
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// <summary> /// Delete an AWS Lambda function. /// </summary> /// <param name="functionName">The name of the Lambda function to /// delete.</param> /// <returns>A Boolean value that indicates the success of the action.</returns> public async Task<bool> DeleteFunctionAsync(string functionName) { var request = new DeleteFunctionRequest { FunctionName = functionName, }; var response = await _lambdaService.DeleteFunctionAsync(request); // A return value of NoContent means that the request was processed. // In this case, the function was deleted, and the return value // is intentionally blank. return response.HttpStatusCode == System.Net.HttpStatusCode.NoContent; }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for .NET 參考中的 API

C++
C++ 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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::DeleteFunctionRequest request; request.SetFunctionName(LAMBDA_NAME); Aws::Lambda::Model::DeleteFunctionOutcome outcome = client.DeleteFunction( request); if (outcome.IsSuccess()) { std::cout << "The lambda function was successfully deleted." << std::endl; } else { std::cerr << "Error with Lambda::DeleteFunction. " << outcome.GetError().GetMessage() << std::endl; }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for C++ 參考中的 API

CLI
AWS CLI

範例 1:若要依函數名稱刪除 Lambda 函數

下列 delete-function 範例會透過指定函數的名稱來刪除名為 my-function 的 Lambda 函數。

aws lambda delete-function \ --function-name my-function

此命令不會產生輸出。

範例 2:依函數 ARN 刪除 Lambda 函數

下列delete-function範例my-function會透過指定函數的 ARN 來刪除名為 的 Lambda 函數。

aws lambda delete-function \ --function-name arn:aws:lambda:us-west-2:123456789012:function:my-function

此命令不會產生輸出。

範例 3:依部分函數 ARN 刪除 Lambda 函數

下列delete-function範例my-function會透過指定函數的部分 ARN 來刪除名為 的 Lambda 函數。

aws lambda delete-function \ --function-name 123456789012:function:my-function

此命令不會產生輸出。

如需詳細資訊,請參閱《AWS Lambda 開發人員指南》中的 AWS Lambda 函數組態

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteFunction

Go
SDK for Go V2
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import ( "bytes" "context" "encoding/json" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/lambda" "github.com/aws/aws-sdk-go-v2/service/lambda/types" ) // 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 } // DeleteFunction deletes the Lambda function specified by functionName. func (wrapper FunctionWrapper) DeleteFunction(ctx context.Context, functionName string) { _, err := wrapper.LambdaClient.DeleteFunction(ctx, &lambda.DeleteFunctionInput{ FunctionName: aws.String(functionName), }) if err != nil { log.Panicf("Couldn't delete function %v. Here's why: %v\n", functionName, err) } }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for Go 參考中的 API

Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Deletes an AWS Lambda function. * * @param awsLambda an instance of the {@link LambdaClient} class, which is used to interact with the AWS Lambda service * @param functionName the name of the Lambda function to be deleted * * @throws LambdaException if an error occurs while deleting the Lambda function */ public static void deleteLambdaFunction(LambdaClient awsLambda, String functionName) { try { DeleteFunctionRequest request = DeleteFunctionRequest.builder() .functionName(functionName) .build(); awsLambda.deleteFunction(request); System.out.println("The " + functionName + " function was deleted"); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for Java 2.x 參考中的 API

JavaScript
SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * @param {string} funcName */ const deleteFunction = (funcName) => { const client = new LambdaClient({}); const command = new DeleteFunctionCommand({ FunctionName: funcName }); return client.send(command); };
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for JavaScript 參考中的 API

Kotlin
Kotlin 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun delLambdaFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS for Kotlin SDK 參考中的 API

PHP
適用於 PHP 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

public function deleteFunction($functionName) { return $this->lambdaClient->deleteFunction([ 'FunctionName' => $functionName, ]); }
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for PHP 參考中的 API

PowerShell
for PowerShell 工具

範例 1:此範例會刪除 Lambda 函數的特定版本

Remove-LMFunction -FunctionName "MylambdaFunction123" -Qualifier '3'
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DeleteFunction

Python
SDK for Python (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class LambdaWrapper: def __init__(self, lambda_client, iam_resource): self.lambda_client = lambda_client self.iam_resource = iam_resource def delete_function(self, function_name): """ Deletes a Lambda function. :param function_name: The name of the function to delete. """ try: self.lambda_client.delete_function(FunctionName=function_name) except ClientError: logger.exception("Couldn't delete function %s.", function_name) raise
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for Python (Boto3) Word 參考中的 API

Ruby
Ruby 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class LambdaWrapper attr_accessor :lambda_client, :cloudwatch_client, :iam_client def initialize @lambda_client = Aws::Lambda::Client.new @cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1') @iam_client = Aws::IAM::Client.new(region: 'us-east-1') @logger = Logger.new($stdout) @logger.level = Logger::WARN end # Deletes a Lambda function. # @param function_name: The name of the function to delete. def delete_function(function_name) print "Deleting function: #{function_name}..." @lambda_client.delete_function( function_name: function_name ) print 'Done!'.green rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error deleting #{function_name}:\n #{e.message}") end
  • 如需 API 詳細資訊,請參閱 DeleteFunction AWS SDK for Ruby 參考中的 API

Rust
Rust 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** Delete a function and its role, and if possible or necessary, its associated code object and bucket. */ pub async fn delete_function( &self, location: Option<String>, ) -> ( Result<DeleteFunctionOutput, anyhow::Error>, Result<DeleteRoleOutput, anyhow::Error>, Option<Result<DeleteObjectOutput, anyhow::Error>>, ) { info!("Deleting lambda function {}", self.lambda_name); let delete_function = self .lambda_client .delete_function() .function_name(self.lambda_name.clone()) .send() .await .map_err(anyhow::Error::from); info!("Deleting iam role {}", self.role_name); let delete_role = self .iam_client .delete_role() .role_name(self.role_name.clone()) .send() .await .map_err(anyhow::Error::from); let delete_object: Option<Result<DeleteObjectOutput, anyhow::Error>> = if let Some(location) = location { info!("Deleting object {location}"); Some( self.s3_client .delete_object() .bucket(self.bucket.clone()) .key(location) .send() .await .map_err(anyhow::Error::from), ) } else { info!(?location, "Skipping delete object"); None }; (delete_function, delete_role, delete_object) }
SAP ABAP
SDK for SAP ABAP
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

TRY. lo_lmd->deletefunction( iv_functionname = iv_function_name ). MESSAGE 'Lambda function deleted.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourceconflictex. MESSAGE 'Resource already exists or another operation is in progress.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' 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 詳細資訊,請參閱 DeleteFunction for Word Word 參考中的 Word。 AWS SDK SAP ABAP API