Hay más AWS SDK ejemplos disponibles en el GitHub repositorio de AWS Doc SDK Examples
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Conozca los conceptos básicos de Lambda con un AWS SDK
En el siguiente ejemplo de código, se muestra cómo:
Cree un IAM rol y una función Lambda y, a continuación, cargue el código del controlador.
Invocar la función con un único parámetro y obtener resultados.
Actualizar el código de la función y configurar con una variable de entorno.
Invocar la función con un nuevo parámetro y obtener resultados. Mostrar el registro de ejecución devuelto.
Enumerar las funciones de su cuenta y, luego, limpiar los recursos.
Para obtener información, consulte Crear una función de Lambda con la consola.
- .NET
-
- AWS SDK for .NET
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Cree métodos que realicen acciones de Lambda.
namespace LambdaActions; using Amazon.Lambda; using Amazon.Lambda.Model; /// <summary> /// A class that implements AWS Lambda methods. /// </summary> public class LambdaWrapper { private readonly IAmazonLambda _lambdaService; /// <summary> /// Constructor for the LambdaWrapper class. /// </summary> /// <param name="lambdaService">An initialized Lambda service client.</param> public LambdaWrapper(IAmazonLambda lambdaService) { _lambdaService = lambdaService; } /// <summary> /// Creates a new Lambda function. /// </summary> /// <param name="functionName">The name of the function.</param> /// <param name="s3Bucket">The Amazon Simple Storage Service (Amazon S3) /// bucket where the zip file containing the code is located.</param> /// <param name="s3Key">The Amazon S3 key of the zip file.</param> /// <param name="role">The Amazon Resource Name (ARN) of a role with the /// appropriate Lambda permissions.</param> /// <param name="handler">The name of the handler function.</param> /// <returns>The Amazon Resource Name (ARN) of the newly created /// Lambda function.</returns> public async Task<string> CreateLambdaFunctionAsync( string functionName, string s3Bucket, string s3Key, string role, string handler) { // Defines the location for the function code. // S3Bucket - The S3 bucket where the file containing // the source code is stored. // S3Key - The name of the file containing the code. var functionCode = new FunctionCode { S3Bucket = s3Bucket, S3Key = s3Key, }; var createFunctionRequest = new CreateFunctionRequest { FunctionName = functionName, Description = "Created by the Lambda .NET API", Code = functionCode, Handler = handler, Runtime = Runtime.Dotnet6, Role = role, }; var reponse = await _lambdaService.CreateFunctionAsync(createFunctionRequest); return reponse.FunctionArn; } /// <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; } /// <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; } /// <summary> /// Invoke a Lambda function. /// </summary> /// <param name="functionName">The name of the Lambda function to /// invoke.</param /// <param name="parameters">The parameter values that will be passed to the function.</param> /// <returns>A System Threading Task.</returns> public async Task<string> InvokeFunctionAsync( string functionName, string parameters) { var payload = parameters; var request = new InvokeRequest { FunctionName = functionName, Payload = payload, }; var response = await _lambdaService.InvokeAsync(request); MemoryStream stream = response.Payload; string returnValue = System.Text.Encoding.UTF8.GetString(stream.ToArray()); return returnValue; } /// <summary> /// Get a list of Lambda functions. /// </summary> /// <returns>A list of FunctionConfiguration objects.</returns> public async Task<List<FunctionConfiguration>> ListFunctionsAsync() { var functionList = new List<FunctionConfiguration>(); var functionPaginator = _lambdaService.Paginators.ListFunctions(new ListFunctionsRequest()); await foreach (var function in functionPaginator.Functions) { functionList.Add(function); } return functionList; } /// <summary> /// Update an existing Lambda function. /// </summary> /// <param name="functionName">The name of the Lambda function to update.</param> /// <param name="bucketName">The bucket where the zip file containing /// the Lambda function code is stored.</param> /// <param name="key">The key name of the source code file.</param> /// <returns>Async Task.</returns> public async Task UpdateFunctionCodeAsync( string functionName, string bucketName, string key) { var functionCodeRequest = new UpdateFunctionCodeRequest { FunctionName = functionName, Publish = true, S3Bucket = bucketName, S3Key = key, }; var response = await _lambdaService.UpdateFunctionCodeAsync(functionCodeRequest); Console.WriteLine($"The Function was last modified at {response.LastModified}."); } /// <summary> /// Update the code of a Lambda function. /// </summary> /// <param name="functionName">The name of the function to update.</param> /// <param name="functionHandler">The code that performs the function's actions.</param> /// <param name="environmentVariables">A dictionary of environment variables.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> UpdateFunctionConfigurationAsync( string functionName, string functionHandler, Dictionary<string, string> environmentVariables) { var request = new UpdateFunctionConfigurationRequest { Handler = functionHandler, FunctionName = functionName, Environment = new Amazon.Lambda.Model.Environment { Variables = environmentVariables }, }; var response = await _lambdaService.UpdateFunctionConfigurationAsync(request); Console.WriteLine(response.LastModified); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } }
Cree una función que ejecute el escenario.
global using System.Threading.Tasks; global using Amazon.IdentityManagement; global using Amazon.Lambda; global using LambdaActions; global using LambdaScenarioCommon; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging.Console; global using Microsoft.Extensions.Logging.Debug; using Amazon.Lambda.Model; using Microsoft.Extensions.Configuration; namespace LambdaBasics; public class LambdaBasics { private static ILogger logger = null!; static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonLambda>() .AddAWSService<IAmazonIdentityManagementService>() .AddTransient<LambdaWrapper>() .AddTransient<LambdaRoleWrapper>() .AddTransient<UIWrapper>() ) .Build(); var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<LambdaBasics>(); var lambdaWrapper = host.Services.GetRequiredService<LambdaWrapper>(); var lambdaRoleWrapper = host.Services.GetRequiredService<LambdaRoleWrapper>(); var uiWrapper = host.Services.GetRequiredService<UIWrapper>(); string functionName = configuration["FunctionName"]!; string roleName = configuration["RoleName"]!; string policyDocument = "{" + " \"Version\": \"2012-10-17\"," + " \"Statement\": [ " + " {" + " \"Effect\": \"Allow\"," + " \"Principal\": {" + " \"Service\": \"lambda.amazonaws.com\" " + " }," + " \"Action\": \"sts:AssumeRole\" " + " }" + "]" + "}"; var incrementHandler = configuration["IncrementHandler"]; var calculatorHandler = configuration["CalculatorHandler"]; var bucketName = configuration["BucketName"]; var incrementKey = configuration["IncrementKey"]; var calculatorKey = configuration["CalculatorKey"]; var policyArn = configuration["PolicyArn"]; uiWrapper.DisplayLambdaBasicsOverview(); // Create the policy to use with the AWS Lambda functions and then attach the // policy to a new role. var roleArn = await lambdaRoleWrapper.CreateLambdaRoleAsync(roleName, policyDocument); Console.WriteLine("Waiting for role to become active."); uiWrapper.WaitABit(15, "Wait until the role is active before trying to use it."); // Attach the appropriate AWS Identity and Access Management (IAM) role policy to the new role. var success = await lambdaRoleWrapper.AttachLambdaRolePolicyAsync(policyArn, roleName); uiWrapper.WaitABit(10, "Allow time for the IAM policy to be attached to the role."); // Create the Lambda function using a zip file stored in an Amazon Simple Storage Service // (Amazon S3) bucket. uiWrapper.DisplayTitle("Create Lambda Function"); Console.WriteLine($"Creating the AWS Lambda function: {functionName}."); var lambdaArn = await lambdaWrapper.CreateLambdaFunctionAsync( functionName, bucketName, incrementKey, roleArn, incrementHandler); Console.WriteLine("Waiting for the new function to be available."); Console.WriteLine($"The AWS Lambda ARN is {lambdaArn}"); // Get the Lambda function. Console.WriteLine($"Getting the {functionName} AWS Lambda function."); FunctionConfiguration config; do { config = await lambdaWrapper.GetFunctionAsync(functionName); Console.Write("."); } while (config.State != State.Active); Console.WriteLine($"\nThe function, {functionName} has been created."); Console.WriteLine($"The runtime of this Lambda function is {config.Runtime}."); uiWrapper.PressEnter(); // List the Lambda functions. uiWrapper.DisplayTitle("Listing all Lambda functions."); var functions = await lambdaWrapper.ListFunctionsAsync(); DisplayFunctionList(functions); uiWrapper.DisplayTitle("Invoke increment function"); Console.WriteLine("Now that it has been created, invoke the Lambda increment function."); string? value; do { Console.Write("Enter a value to increment: "); value = Console.ReadLine(); } while (string.IsNullOrEmpty(value)); string functionParameters = "{" + "\"action\": \"increment\", " + "\"x\": \"" + value + "\"" + "}"; var answer = await lambdaWrapper.InvokeFunctionAsync(functionName, functionParameters); Console.WriteLine($"{value} + 1 = {answer}."); uiWrapper.DisplayTitle("Update function"); Console.WriteLine("Now update the Lambda function code."); await lambdaWrapper.UpdateFunctionCodeAsync(functionName, bucketName, calculatorKey); do { config = await lambdaWrapper.GetFunctionAsync(functionName); Console.Write("."); } while (config.LastUpdateStatus == LastUpdateStatus.InProgress); await lambdaWrapper.UpdateFunctionConfigurationAsync( functionName, calculatorHandler, new Dictionary<string, string> { { "LOG_LEVEL", "DEBUG" } }); do { config = await lambdaWrapper.GetFunctionAsync(functionName); Console.Write("."); } while (config.LastUpdateStatus == LastUpdateStatus.InProgress); uiWrapper.DisplayTitle("Call updated function"); Console.WriteLine("Now call the updated function..."); bool done = false; do { string? opSelected; Console.WriteLine("Select the operation to perform:"); Console.WriteLine("\t1. add"); Console.WriteLine("\t2. subtract"); Console.WriteLine("\t3. multiply"); Console.WriteLine("\t4. divide"); Console.WriteLine("\tOr enter \"q\" to quit."); Console.WriteLine("Enter the number (1, 2, 3, 4, or q) of the operation you want to perform: "); do { Console.Write("Your choice? "); opSelected = Console.ReadLine(); } while (opSelected == string.Empty); var operation = (opSelected) switch { "1" => "add", "2" => "subtract", "3" => "multiply", "4" => "divide", "q" => "quit", _ => "add", }; if (operation == "quit") { done = true; } else { // Get two numbers and an action from the user. value = string.Empty; do { Console.Write("Enter the first value: "); value = Console.ReadLine(); } while (value == string.Empty); string? value2; do { Console.Write("Enter a second value: "); value2 = Console.ReadLine(); } while (value2 == string.Empty); functionParameters = "{" + "\"action\": \"" + operation + "\", " + "\"x\": \"" + value + "\"," + "\"y\": \"" + value2 + "\"" + "}"; answer = await lambdaWrapper.InvokeFunctionAsync(functionName, functionParameters); Console.WriteLine($"The answer when we {operation} the two numbers is: {answer}."); } uiWrapper.PressEnter(); } while (!done); // Delete the function created earlier. uiWrapper.DisplayTitle("Clean up resources"); // Detach the IAM policy from the IAM role. Console.WriteLine("First detach the IAM policy from the role."); success = await lambdaRoleWrapper.DetachLambdaRolePolicyAsync(policyArn, roleName); uiWrapper.WaitABit(15, "Let's wait for the policy to be fully detached from the role."); Console.WriteLine("Delete the AWS Lambda function."); success = await lambdaWrapper.DeleteFunctionAsync(functionName); if (success) { Console.WriteLine($"The {functionName} function was deleted."); } else { Console.WriteLine($"Could not remove the function {functionName}"); } // Now delete the IAM role created for use with the functions // created by the application. Console.WriteLine("Now we can delete the role that we created."); success = await lambdaRoleWrapper.DeleteLambdaRoleAsync(roleName); if (success) { Console.WriteLine("The role has been successfully removed."); } else { Console.WriteLine("Couldn't delete the role."); } Console.WriteLine("The Lambda Scenario is now complete."); uiWrapper.PressEnter(); // Displays a formatted list of existing functions returned by the // LambdaMethods.ListFunctions. void DisplayFunctionList(List<FunctionConfiguration> functions) { functions.ForEach(functionConfig => { Console.WriteLine($"{functionConfig.FunctionName}\t{functionConfig.Description}"); }); } } } namespace LambdaActions; using Amazon.IdentityManagement; using Amazon.IdentityManagement.Model; public class LambdaRoleWrapper { private readonly IAmazonIdentityManagementService _lambdaRoleService; public LambdaRoleWrapper(IAmazonIdentityManagementService lambdaRoleService) { _lambdaRoleService = lambdaRoleService; } /// <summary> /// Attach an AWS Identity and Access Management (IAM) role policy to the /// IAM role to be assumed by the AWS Lambda functions created for the scenario. /// </summary> /// <param name="policyArn">The Amazon Resource Name (ARN) of the IAM policy.</param> /// <param name="roleName">The name of the IAM role to attach the IAM policy to.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> AttachLambdaRolePolicyAsync(string policyArn, string roleName) { var response = await _lambdaRoleService.AttachRolePolicyAsync(new AttachRolePolicyRequest { PolicyArn = policyArn, RoleName = roleName }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Create a new IAM role. /// </summary> /// <param name="roleName">The name of the IAM role to create.</param> /// <param name="policyDocument">The policy document for the new IAM role.</param> /// <returns>A string representing the ARN for newly created role.</returns> public async Task<string> CreateLambdaRoleAsync(string roleName, string policyDocument) { var request = new CreateRoleRequest { AssumeRolePolicyDocument = policyDocument, RoleName = roleName, }; var response = await _lambdaRoleService.CreateRoleAsync(request); return response.Role.Arn; } /// <summary> /// Deletes an IAM role. /// </summary> /// <param name="roleName">The name of the role to delete.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public async Task<bool> DeleteLambdaRoleAsync(string roleName) { var request = new DeleteRoleRequest { RoleName = roleName, }; var response = await _lambdaRoleService.DeleteRoleAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } public async Task<bool> DetachLambdaRolePolicyAsync(string policyArn, string roleName) { var response = await _lambdaRoleService.DetachRolePolicyAsync(new DetachRolePolicyRequest { PolicyArn = policyArn, RoleName = roleName }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } } namespace LambdaScenarioCommon; public class UIWrapper { public readonly string SepBar = new('-', Console.WindowWidth); /// <summary> /// Show information about the AWS Lambda Basics scenario. /// </summary> public void DisplayLambdaBasicsOverview() { Console.Clear(); DisplayTitle("Welcome to AWS Lambda Basics"); Console.WriteLine("This example application does the following:"); Console.WriteLine("\t1. Creates an AWS Identity and Access Management (IAM) role that will be assumed by the functions we create."); Console.WriteLine("\t2. Attaches an IAM role policy that has Lambda permissions."); Console.WriteLine("\t3. Creates a Lambda function that increments the value passed to it."); Console.WriteLine("\t4. Calls the increment function and passes a value."); Console.WriteLine("\t5. Updates the code so that the function is a simple calculator."); Console.WriteLine("\t6. Calls the calculator function with the values entered."); Console.WriteLine("\t7. Deletes the Lambda function."); Console.WriteLine("\t7. Detaches the IAM role policy."); Console.WriteLine("\t8. Deletes the IAM role."); PressEnter(); } /// <summary> /// Display a message and wait until the user presses enter. /// </summary> public void PressEnter() { Console.Write("\nPress <Enter> to continue. "); _ = Console.ReadLine(); Console.WriteLine(); } /// <summary> /// Pad a string with spaces to center it on the console display. /// </summary> /// <param name="strToCenter">The string to be centered.</param> /// <returns>The padded string.</returns> public string CenterString(string strToCenter) { var padAmount = (Console.WindowWidth - strToCenter.Length) / 2; var leftPad = new string(' ', padAmount); return $"{leftPad}{strToCenter}"; } /// <summary> /// Display a line of hyphens, the centered text of the title and another /// line of hyphens. /// </summary> /// <param name="strTitle">The string to be displayed.</param> public void DisplayTitle(string strTitle) { Console.WriteLine(SepBar); Console.WriteLine(CenterString(strTitle)); Console.WriteLine(SepBar); } /// <summary> /// Display a countdown and wait for a number of seconds. /// </summary> /// <param name="numSeconds">The number of seconds to wait.</param> public void WaitABit(int numSeconds, string msg) { Console.WriteLine(msg); // Wait for the requested number of seconds. for (int i = numSeconds; i > 0; i--) { System.Threading.Thread.Sleep(1000); Console.Write($"{i}..."); } PressEnter(); } }
Defina un controlador de Lambda que aumente un número.
using Amazon.Lambda.Core; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace LambdaIncrement; public class Function { /// <summary> /// A simple function increments the integer parameter. /// </summary> /// <param name="input">A JSON string containing an action, which must be /// "increment" and a string representing the value to increment.</param> /// <param name="context">The context object passed by Lambda containing /// information about invocation, function, and execution environment.</param> /// <returns>A string representing the incremented value of the parameter.</returns> public int FunctionHandler(Dictionary<string, string> input, ILambdaContext context) { if (input["action"] == "increment") { int inputValue = Convert.ToInt32(input["x"]); return inputValue + 1; } else { return 0; } } }
Defina un segundo controlador de Lambda que realice operaciones aritméticas.
using Amazon.Lambda.Core; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace LambdaCalculator; public class Function { /// <summary> /// A simple function that takes two number in string format and performs /// the requested arithmetic function. /// </summary> /// <param name="input">JSON data containing an action, and x and y values. /// Valid actions include: add, subtract, multiply, and divide.</param> /// <param name="context">The context object passed by Lambda containing /// information about invocation, function, and execution environment.</param> /// <returns>A string representing the results of the calculation.</returns> public int FunctionHandler(Dictionary<string, string> input, ILambdaContext context) { var action = input["action"]; int x = Convert.ToInt32(input["x"]); int y = Convert.ToInt32(input["y"]); int result; switch (action) { case "add": result = x + y; break; case "subtract": result = x - y; break; case "multiply": result = x * y; break; case "divide": if (y == 0) { Console.Error.WriteLine("Divide by zero error."); result = 0; } else result = x / y; break; default: Console.Error.WriteLine($"{action} is not a valid operation."); result = 0; break; } return result; } }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for .NET APIReference.
-
- C++
-
- SDKpara C++
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. //! Get started with functions scenario. /*! \param clientConfig: AWS client configuration. \return bool: Successful completion. */ bool AwsDoc::Lambda::getStartedWithFunctionsScenario( const Aws::Client::ClientConfiguration &clientConfig) { Aws::Lambda::LambdaClient client(clientConfig); // 1. Create an AWS Identity and Access Management (IAM) role for Lambda function. Aws::String roleArn; if (!getIamRoleArn(roleArn, clientConfig)) { return false; } // 2. Create a Lambda function. int seconds = 0; do { Aws::Lambda::Model::CreateFunctionRequest request; request.SetFunctionName(LAMBDA_NAME); request.SetDescription(LAMBDA_DESCRIPTION); // Optional. #if USE_CPP_LAMBDA_FUNCTION request.SetRuntime(Aws::Lambda::Model::Runtime::provided_al2); request.SetTimeout(15); request.SetMemorySize(128); // Assume the AWS Lambda function was built in Docker with same architecture // as this code. #if defined(__x86_64__) request.SetArchitectures({Aws::Lambda::Model::Architecture::x86_64}); #elif defined(__aarch64__) request.SetArchitectures({Aws::Lambda::Model::Architecture::arm64}); #else #error "Unimplemented architecture" #endif // defined(architecture) #else request.SetRuntime(Aws::Lambda::Model::Runtime::python3_9); #endif request.SetRole(roleArn); request.SetHandler(LAMBDA_HANDLER_NAME); request.SetPublish(true); Aws::Lambda::Model::FunctionCode code; std::ifstream ifstream(INCREMENT_LAMBDA_CODE.c_str(), std::ios_base::in | std::ios_base::binary); if (!ifstream.is_open()) { std::cerr << "Error opening file " << INCREMENT_LAMBDA_CODE << "." << std::endl; #if USE_CPP_LAMBDA_FUNCTION std::cerr << "The cpp Lambda function must be built following the instructions in the cpp_lambda/README.md file. " << std::endl; #endif deleteIamRole(clientConfig); return false; } Aws::StringStream buffer; buffer << ifstream.rdbuf(); code.SetZipFile(Aws::Utils::ByteBuffer((unsigned char *) buffer.str().c_str(), buffer.str().length())); request.SetCode(code); Aws::Lambda::Model::CreateFunctionOutcome outcome = client.CreateFunction( request); if (outcome.IsSuccess()) { std::cout << "The lambda function was successfully created. " << seconds << " seconds elapsed." << std::endl; break; } else if (outcome.GetError().GetErrorType() == Aws::Lambda::LambdaErrors::INVALID_PARAMETER_VALUE && outcome.GetError().GetMessage().find("role") >= 0) { if ((seconds % 5) == 0) { // Log status every 10 seconds. std::cout << "Waiting for the IAM role to become available as a CreateFunction parameter. " << seconds << " seconds elapsed." << std::endl; std::cout << outcome.GetError().GetMessage() << std::endl; } } else { std::cerr << "Error with CreateFunction. " << outcome.GetError().GetMessage() << std::endl; deleteIamRole(clientConfig); return false; } ++seconds; std::this_thread::sleep_for(std::chrono::seconds(1)); } while (60 > seconds); std::cout << "The current Lambda function increments 1 by an input." << std::endl; // 3. Invoke the Lambda function. { int increment = askQuestionForInt("Enter an increment integer: "); Aws::Lambda::Model::InvokeResult invokeResult; Aws::Utils::Json::JsonValue jsonPayload; jsonPayload.WithString("action", "increment"); jsonPayload.WithInteger("number", increment); if (invokeLambdaFunction(jsonPayload, Aws::Lambda::Model::LogType::Tail, invokeResult, client)) { Aws::Utils::Json::JsonValue jsonValue(invokeResult.GetPayload()); Aws::Map<Aws::String, Aws::Utils::Json::JsonView> values = jsonValue.View().GetAllObjects(); auto iter = values.find("result"); if (iter != values.end() && iter->second.IsIntegerType()) { { std::cout << INCREMENT_RESUlT_PREFIX << iter->second.AsInteger() << std::endl; } } else { std::cout << "There was an error in execution. Here is the log." << std::endl; Aws::Utils::ByteBuffer buffer = Aws::Utils::HashingUtils::Base64Decode( invokeResult.GetLogResult()); std::cout << "With log " << buffer.GetUnderlyingData() << std::endl; } } } std::cout << "The Lambda function will now be updated with new code. Press return to continue, "; Aws::String answer; std::getline(std::cin, answer); // 4. Update the Lambda function code. { Aws::Lambda::Model::UpdateFunctionCodeRequest request; request.SetFunctionName(LAMBDA_NAME); std::ifstream ifstream(CALCULATOR_LAMBDA_CODE.c_str(), std::ios_base::in | std::ios_base::binary); if (!ifstream.is_open()) { std::cerr << "Error opening file " << INCREMENT_LAMBDA_CODE << "." << std::endl; #if USE_CPP_LAMBDA_FUNCTION std::cerr << "The cpp Lambda function must be built following the instructions in the cpp_lambda/README.md file. " << std::endl; #endif deleteLambdaFunction(client); deleteIamRole(clientConfig); return false; } Aws::StringStream buffer; buffer << ifstream.rdbuf(); request.SetZipFile( Aws::Utils::ByteBuffer((unsigned char *) buffer.str().c_str(), buffer.str().length())); request.SetPublish(true); Aws::Lambda::Model::UpdateFunctionCodeOutcome outcome = client.UpdateFunctionCode( request); if (outcome.IsSuccess()) { std::cout << "The lambda code was successfully updated." << std::endl; } else { std::cerr << "Error with Lambda::UpdateFunctionCode. " << outcome.GetError().GetMessage() << std::endl; } } std::cout << "This function uses an environment variable to control the logging level." << std::endl; std::cout << "UpdateFunctionConfiguration will be used to set the LOG_LEVEL to DEBUG." << std::endl; seconds = 0; // 5. Update the Lambda function configuration. do { ++seconds; std::this_thread::sleep_for(std::chrono::seconds(1)); Aws::Lambda::Model::UpdateFunctionConfigurationRequest request; request.SetFunctionName(LAMBDA_NAME); Aws::Lambda::Model::Environment environment; environment.AddVariables("LOG_LEVEL", "DEBUG"); request.SetEnvironment(environment); Aws::Lambda::Model::UpdateFunctionConfigurationOutcome outcome = client.UpdateFunctionConfiguration( request); if (outcome.IsSuccess()) { std::cout << "The lambda configuration was successfully updated." << std::endl; break; } // RESOURCE_IN_USE: function code update not completed. else if (outcome.GetError().GetErrorType() != Aws::Lambda::LambdaErrors::RESOURCE_IN_USE) { if ((seconds % 10) == 0) { // Log status every 10 seconds. std::cout << "Lambda function update in progress . After " << seconds << " seconds elapsed." << std::endl; } } else { std::cerr << "Error with Lambda::UpdateFunctionConfiguration. " << outcome.GetError().GetMessage() << std::endl; } } while (0 < seconds); if (0 > seconds) { std::cerr << "Function failed to become active." << std::endl; } else { std::cout << "Updated function active after " << seconds << " seconds." << std::endl; } std::cout << "\nThe new code applies an arithmetic operator to two variables, x an y." << std::endl; std::vector<Aws::String> operators = {"plus", "minus", "times", "divided-by"}; for (size_t i = 0; i < operators.size(); ++i) { std::cout << " " << i + 1 << " " << operators[i] << std::endl; } // 6. Invoke the updated Lambda function. do { int operatorIndex = askQuestionForIntRange("Select an operator index 1 - 4 ", 1, 4); int x = askQuestionForInt("Enter an integer for the x value "); int y = askQuestionForInt("Enter an integer for the y value "); Aws::Utils::Json::JsonValue calculateJsonPayload; calculateJsonPayload.WithString("action", operators[operatorIndex - 1]); calculateJsonPayload.WithInteger("x", x); calculateJsonPayload.WithInteger("y", y); Aws::Lambda::Model::InvokeResult calculatedResult; if (invokeLambdaFunction(calculateJsonPayload, Aws::Lambda::Model::LogType::Tail, calculatedResult, client)) { Aws::Utils::Json::JsonValue jsonValue(calculatedResult.GetPayload()); Aws::Map<Aws::String, Aws::Utils::Json::JsonView> values = jsonValue.View().GetAllObjects(); auto iter = values.find("result"); if (iter != values.end() && iter->second.IsIntegerType()) { std::cout << ARITHMETIC_RESUlT_PREFIX << x << " " << operators[operatorIndex - 1] << " " << y << " is " << iter->second.AsInteger() << std::endl; } else if (iter != values.end() && iter->second.IsFloatingPointType()) { std::cout << ARITHMETIC_RESUlT_PREFIX << x << " " << operators[operatorIndex - 1] << " " << y << " is " << iter->second.AsDouble() << std::endl; } else { std::cout << "There was an error in execution. Here is the log." << std::endl; Aws::Utils::ByteBuffer buffer = Aws::Utils::HashingUtils::Base64Decode( calculatedResult.GetLogResult()); std::cout << "With log " << buffer.GetUnderlyingData() << std::endl; } } answer = askQuestion("Would you like to try another operation? (y/n) "); } while (answer == "y"); std::cout << "A list of the lambda functions will be retrieved. Press return to continue, "; std::getline(std::cin, answer); // 7. List the Lambda functions. std::vector<Aws::String> functions; Aws::String marker; do { Aws::Lambda::Model::ListFunctionsRequest request; if (!marker.empty()) { request.SetMarker(marker); } Aws::Lambda::Model::ListFunctionsOutcome outcome = client.ListFunctions( request); if (outcome.IsSuccess()) { const Aws::Lambda::Model::ListFunctionsResult &result = outcome.GetResult(); std::cout << result.GetFunctions().size() << " lambda functions were retrieved." << std::endl; for (const Aws::Lambda::Model::FunctionConfiguration &functionConfiguration: result.GetFunctions()) { functions.push_back(functionConfiguration.GetFunctionName()); std::cout << functions.size() << " " << functionConfiguration.GetDescription() << std::endl; std::cout << " " << Aws::Lambda::Model::RuntimeMapper::GetNameForRuntime( functionConfiguration.GetRuntime()) << ": " << functionConfiguration.GetHandler() << std::endl; } marker = result.GetNextMarker(); } else { std::cerr << "Error with Lambda::ListFunctions. " << outcome.GetError().GetMessage() << std::endl; } } while (!marker.empty()); // 8. Get a Lambda function. if (!functions.empty()) { std::stringstream question; question << "Choose a function to retrieve between 1 and " << functions.size() << " "; int functionIndex = askQuestionForIntRange(question.str(), 1, static_cast<int>(functions.size())); Aws::String functionName = functions[functionIndex - 1]; 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; } } std::cout << "The resources will be deleted. Press return to continue, "; std::getline(std::cin, answer); // 9. Delete the Lambda function. bool result = deleteLambdaFunction(client); // 10. Delete the IAM role. return result && deleteIamRole(clientConfig); } //! Routine which invokes a Lambda function and returns the result. /*! \param jsonPayload: Payload for invoke function. \param logType: Log type setting for invoke function. \param invokeResult: InvokeResult object to receive the result. \param client: Lambda client. \return bool: Successful completion. */ bool AwsDoc::Lambda::invokeLambdaFunction(const Aws::Utils::Json::JsonValue &jsonPayload, Aws::Lambda::Model::LogType logType, Aws::Lambda::Model::InvokeResult &invokeResult, const Aws::Lambda::LambdaClient &client) { int seconds = 0; bool result = false; /* * In this example, the Invoke function can be called before recently created resources are * available. The Invoke function is called repeatedly until the resources are * available. */ do { Aws::Lambda::Model::InvokeRequest request; request.SetFunctionName(LAMBDA_NAME); request.SetLogType(logType); std::shared_ptr<Aws::IOStream> payload = Aws::MakeShared<Aws::StringStream>( "FunctionTest"); *payload << jsonPayload.View().WriteReadable(); request.SetBody(payload); request.SetContentType("application/json"); Aws::Lambda::Model::InvokeOutcome outcome = client.Invoke(request); if (outcome.IsSuccess()) { invokeResult = std::move(outcome.GetResult()); result = true; break; } // ACCESS_DENIED: because the role is not available yet. // RESOURCE_CONFLICT: because the Lambda function is being created or updated. else if ((outcome.GetError().GetErrorType() == Aws::Lambda::LambdaErrors::ACCESS_DENIED) || (outcome.GetError().GetErrorType() == Aws::Lambda::LambdaErrors::RESOURCE_CONFLICT)) { if ((seconds % 5) == 0) { // Log status every 10 seconds. std::cout << "Waiting for the invoke api to be available, status " << ((outcome.GetError().GetErrorType() == Aws::Lambda::LambdaErrors::ACCESS_DENIED ? "ACCESS_DENIED" : "RESOURCE_CONFLICT")) << ". " << seconds << " seconds elapsed." << std::endl; } } else { std::cerr << "Error with Lambda::InvokeRequest. " << outcome.GetError().GetMessage() << std::endl; break; } ++seconds; std::this_thread::sleep_for(std::chrono::seconds(1)); } while (seconds < 60); return result; }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for C++ APIReference.
-
- Go
-
- SDKpara Go V2
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Cree una situación interactiva que le muestre cómo empezar a usar las funciones de Lambda.
import ( "archive/zip" "bytes" "context" "encoding/base64" "encoding/json" "errors" "fmt" "log" "os" "strings" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/iam" iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" "github.com/aws/aws-sdk-go-v2/service/lambda" "github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools" "github.com/awsdocs/aws-doc-sdk-examples/gov2/lambda/actions" ) // GetStartedFunctionsScenario shows you how to use AWS Lambda to perform the following // actions: // // 1. Create an AWS Identity and Access Management (IAM) role and Lambda function, then upload handler code. // 2. Invoke the function with a single parameter and get results. // 3. Update the function code and configure with an environment variable. // 4. Invoke the function with new parameters and get results. Display the returned execution log. // 5. List the functions for your account, then clean up resources. type GetStartedFunctionsScenario struct { sdkConfig aws.Config functionWrapper actions.FunctionWrapper questioner demotools.IQuestioner helper IScenarioHelper isTestRun bool } // NewGetStartedFunctionsScenario constructs a GetStartedFunctionsScenario instance from a configuration. // It uses the specified config to get a Lambda client and create wrappers for the actions // used in the scenario. func NewGetStartedFunctionsScenario(sdkConfig aws.Config, questioner demotools.IQuestioner, helper IScenarioHelper) GetStartedFunctionsScenario { lambdaClient := lambda.NewFromConfig(sdkConfig) return GetStartedFunctionsScenario{ sdkConfig: sdkConfig, functionWrapper: actions.FunctionWrapper{LambdaClient: lambdaClient}, questioner: questioner, helper: helper, } } // Run runs the interactive scenario. func (scenario GetStartedFunctionsScenario) Run(ctx context.Context) { defer func() { if r := recover(); r != nil { log.Printf("Something went wrong with the demo.\n") } }() log.Println(strings.Repeat("-", 88)) log.Println("Welcome to the AWS Lambda get started with functions demo.") log.Println(strings.Repeat("-", 88)) role := scenario.GetOrCreateRole(ctx) funcName := scenario.CreateFunction(ctx, role) scenario.InvokeIncrement(ctx, funcName) scenario.UpdateFunction(ctx, funcName) scenario.InvokeCalculator(ctx, funcName) scenario.ListFunctions(ctx) scenario.Cleanup(ctx, role, funcName) log.Println(strings.Repeat("-", 88)) log.Println("Thanks for watching!") log.Println(strings.Repeat("-", 88)) } // GetOrCreateRole checks whether the specified role exists and returns it if it does. // Otherwise, a role is created that specifies Lambda as a trusted principal. // The AWSLambdaBasicExecutionRole managed policy is attached to the role and the role // is returned. func (scenario GetStartedFunctionsScenario) GetOrCreateRole(ctx context.Context) *iamtypes.Role { var role *iamtypes.Role iamClient := iam.NewFromConfig(scenario.sdkConfig) log.Println("First, we need an IAM role that Lambda can assume.") roleName := scenario.questioner.Ask("Enter a name for the role:", demotools.NotEmpty{}) getOutput, err := iamClient.GetRole(ctx, &iam.GetRoleInput{ RoleName: aws.String(roleName)}) if err != nil { var noSuch *iamtypes.NoSuchEntityException if errors.As(err, &noSuch) { log.Printf("Role %v doesn't exist. Creating it....\n", roleName) } else { log.Panicf("Couldn't check whether role %v exists. Here's why: %v\n", roleName, err) } } else { role = getOutput.Role log.Printf("Found role %v.\n", *role.RoleName) } if role == nil { trustPolicy := PolicyDocument{ Version: "2012-10-17", Statement: []PolicyStatement{{ Effect: "Allow", Principal: map[string]string{"Service": "lambda.amazonaws.com"}, Action: []string{"sts:AssumeRole"}, }}, } policyArn := "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" createOutput, err := iamClient.CreateRole(ctx, &iam.CreateRoleInput{ AssumeRolePolicyDocument: aws.String(trustPolicy.String()), RoleName: aws.String(roleName), }) if err != nil { log.Panicf("Couldn't create role %v. Here's why: %v\n", roleName, err) } role = createOutput.Role _, err = iamClient.AttachRolePolicy(ctx, &iam.AttachRolePolicyInput{ PolicyArn: aws.String(policyArn), RoleName: aws.String(roleName), }) if err != nil { log.Panicf("Couldn't attach a policy to role %v. Here's why: %v\n", roleName, err) } log.Printf("Created role %v.\n", *role.RoleName) log.Println("Let's give AWS a few seconds to propagate resources...") scenario.helper.Pause(10) } log.Println(strings.Repeat("-", 88)) return role } // CreateFunction creates a Lambda function and uploads a handler written in Python. // The code for the Python handler is packaged as a []byte in .zip format. func (scenario GetStartedFunctionsScenario) CreateFunction(ctx context.Context, role *iamtypes.Role) string { log.Println("Let's create a function that increments a number.\n" + "The function uses the 'lambda_handler_basic.py' script found in the \n" + "'handlers' directory of this project.") funcName := scenario.questioner.Ask("Enter a name for the Lambda function:", demotools.NotEmpty{}) zipPackage := scenario.helper.CreateDeploymentPackage("lambda_handler_basic.py", fmt.Sprintf("%v.py", funcName)) log.Printf("Creating function %v and waiting for it to be ready.", funcName) funcState := scenario.functionWrapper.CreateFunction(ctx, funcName, fmt.Sprintf("%v.lambda_handler", funcName), role.Arn, zipPackage) log.Printf("Your function is %v.", funcState) log.Println(strings.Repeat("-", 88)) return funcName } // InvokeIncrement invokes a Lambda function that increments a number. The function // parameters are contained in a Go struct that is used to serialize the parameters to // a JSON payload that is passed to the function. // The result payload is deserialized into a Go struct that contains an int value. func (scenario GetStartedFunctionsScenario) InvokeIncrement(ctx context.Context, funcName string) { parameters := actions.IncrementParameters{Action: "increment"} log.Println("Let's invoke our function. This function increments a number.") parameters.Number = scenario.questioner.AskInt("Enter a number to increment:", demotools.NotEmpty{}) log.Printf("Invoking %v with %v...\n", funcName, parameters.Number) invokeOutput := scenario.functionWrapper.Invoke(ctx, funcName, parameters, false) var payload actions.LambdaResultInt err := json.Unmarshal(invokeOutput.Payload, &payload) if err != nil { log.Panicf("Couldn't unmarshal payload from invoking %v. Here's why: %v\n", funcName, err) } log.Printf("Invoking %v with %v returned %v.\n", funcName, parameters.Number, payload) log.Println(strings.Repeat("-", 88)) } // UpdateFunction updates the code for a Lambda function by uploading a simple arithmetic // calculator written in Python. The code for the Python handler is packaged as a // []byte in .zip format. // After the code is updated, the configuration is also updated with a new log // level that instructs the handler to log additional information. func (scenario GetStartedFunctionsScenario) UpdateFunction(ctx context.Context, funcName string) { log.Println("Let's update the function to an arithmetic calculator.\n" + "The function uses the 'lambda_handler_calculator.py' script found in the \n" + "'handlers' directory of this project.") scenario.questioner.Ask("Press Enter when you're ready.") log.Println("Creating deployment package...") zipPackage := scenario.helper.CreateDeploymentPackage("lambda_handler_calculator.py", fmt.Sprintf("%v.py", funcName)) log.Println("...and updating the Lambda function and waiting for it to be ready.") funcState := scenario.functionWrapper.UpdateFunctionCode(ctx, funcName, zipPackage) log.Printf("Updated function %v. Its current state is %v.", funcName, funcState) log.Println("This function uses an environment variable to control logging level.") log.Println("Let's set it to DEBUG to get the most logging.") scenario.functionWrapper.UpdateFunctionConfiguration(ctx, funcName, map[string]string{"LOG_LEVEL": "DEBUG"}) log.Println(strings.Repeat("-", 88)) } // InvokeCalculator invokes the Lambda calculator function. The parameters are stored in a // Go struct that is used to serialize the parameters to a JSON payload. That payload is then passed // to the function. // The result payload is deserialized to a Go struct that stores the result as either an // int or float32, depending on the kind of operation that was specified. func (scenario GetStartedFunctionsScenario) InvokeCalculator(ctx context.Context, funcName string) { wantInvoke := true choices := []string{"plus", "minus", "times", "divided-by"} for wantInvoke { choice := scenario.questioner.AskChoice("Select an arithmetic operation:\n", choices) x := scenario.questioner.AskInt("Enter a value for x:", demotools.NotEmpty{}) y := scenario.questioner.AskInt("Enter a value for y:", demotools.NotEmpty{}) log.Printf("Invoking %v %v %v...", x, choices[choice], y) calcParameters := actions.CalculatorParameters{ Action: choices[choice], X: x, Y: y, } invokeOutput := scenario.functionWrapper.Invoke(ctx, funcName, calcParameters, true) var payload any if choice == 3 { // divide-by results in a float. payload = actions.LambdaResultFloat{} } else { payload = actions.LambdaResultInt{} } err := json.Unmarshal(invokeOutput.Payload, &payload) if err != nil { log.Panicf("Couldn't unmarshal payload from invoking %v. Here's why: %v\n", funcName, err) } log.Printf("Invoking %v with %v %v %v returned %v.\n", funcName, calcParameters.X, calcParameters.Action, calcParameters.Y, payload) scenario.questioner.Ask("Press Enter to see the logs from the call.") logRes, err := base64.StdEncoding.DecodeString(*invokeOutput.LogResult) if err != nil { log.Panicf("Couldn't decode log result. Here's why: %v\n", err) } log.Println(string(logRes)) wantInvoke = scenario.questioner.AskBool("Do you want to calculate again? (y/n)", "y") } log.Println(strings.Repeat("-", 88)) } // ListFunctions lists up to the specified number of functions for your account. func (scenario GetStartedFunctionsScenario) ListFunctions(ctx context.Context) { count := scenario.questioner.AskInt( "Let's list functions for your account. How many do you want to see?", demotools.NotEmpty{}) functions := scenario.functionWrapper.ListFunctions(ctx, count) log.Printf("Found %v functions:", len(functions)) for _, function := range functions { log.Printf("\t%v", *function.FunctionName) } log.Println(strings.Repeat("-", 88)) } // Cleanup removes the IAM and Lambda resources created by the example. func (scenario GetStartedFunctionsScenario) Cleanup(ctx context.Context, role *iamtypes.Role, funcName string) { if scenario.questioner.AskBool("Do you want to clean up resources created for this example? (y/n)", "y") { iamClient := iam.NewFromConfig(scenario.sdkConfig) policiesOutput, err := iamClient.ListAttachedRolePolicies(ctx, &iam.ListAttachedRolePoliciesInput{RoleName: role.RoleName}) if err != nil { log.Panicf("Couldn't get policies attached to role %v. Here's why: %v\n", *role.RoleName, err) } for _, policy := range policiesOutput.AttachedPolicies { _, err = iamClient.DetachRolePolicy(ctx, &iam.DetachRolePolicyInput{ PolicyArn: policy.PolicyArn, RoleName: role.RoleName, }) if err != nil { log.Panicf("Couldn't detach policy %v from role %v. Here's why: %v\n", *policy.PolicyArn, *role.RoleName, err) } } _, err = iamClient.DeleteRole(ctx, &iam.DeleteRoleInput{RoleName: role.RoleName}) if err != nil { log.Panicf("Couldn't delete role %v. Here's why: %v\n", *role.RoleName, err) } log.Printf("Deleted role %v.\n", *role.RoleName) scenario.functionWrapper.DeleteFunction(ctx, funcName) log.Printf("Deleted function %v.\n", funcName) } else { log.Println("Okay. Don't forget to delete the resources when you're done with them.") } } // IScenarioHelper abstracts I/O and wait functions from a scenario so that they // can be mocked for unit testing. type IScenarioHelper interface { Pause(secs int) CreateDeploymentPackage(sourceFile string, destinationFile string) *bytes.Buffer } // ScenarioHelper lets the caller specify the path to Lambda handler functions. type ScenarioHelper struct { HandlerPath string } // Pause waits for the specified number of seconds. func (helper *ScenarioHelper) Pause(secs int) { time.Sleep(time.Duration(secs) * time.Second) } // CreateDeploymentPackage creates an AWS Lambda deployment package from a source file. The // deployment package is stored in .zip format in a bytes.Buffer. The buffer can be // used to pass a []byte to Lambda when creating the function. // The specified destinationFile is the name to give the file when it's deployed to Lambda. func (helper *ScenarioHelper) CreateDeploymentPackage(sourceFile string, destinationFile string) *bytes.Buffer { var err error buffer := &bytes.Buffer{} writer := zip.NewWriter(buffer) zFile, err := writer.Create(destinationFile) if err != nil { log.Panicf("Couldn't create destination archive %v. Here's why: %v\n", destinationFile, err) } sourceBody, err := os.ReadFile(fmt.Sprintf("%v/%v", helper.HandlerPath, sourceFile)) if err != nil { log.Panicf("Couldn't read handler source file %v. Here's why: %v\n", sourceFile, err) } else { _, err = zFile.Write(sourceBody) if err != nil { log.Panicf("Couldn't write handler %v to zip archive. Here's why: %v\n", sourceFile, err) } } err = writer.Close() if err != nil { log.Panicf("Couldn't close zip writer. Here's why: %v\n", err) } return buffer }
Cree una estructura que ajuste las acciones individuales de Lambda.
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 } // GetFunction gets data about the Lambda function specified by functionName. func (wrapper FunctionWrapper) GetFunction(ctx context.Context, functionName string) types.State { var state types.State funcOutput, err := wrapper.LambdaClient.GetFunction(ctx, &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 } // CreateFunction creates a new Lambda function from code contained in the zipPackage // buffer. The specified handlerName must match the name of the file and function // contained in the uploaded code. The role specified by iamRoleArn is assumed by // Lambda and grants specific permissions. // When the function already exists, types.StateActive is returned. // When the function is created, a lambda.FunctionActiveV2Waiter is used to wait until the // function is active. func (wrapper FunctionWrapper) CreateFunction(ctx context.Context, functionName string, handlerName string, iamRoleArn *string, zipPackage *bytes.Buffer) types.State { var state types.State _, err := wrapper.LambdaClient.CreateFunction(ctx, &lambda.CreateFunctionInput{ Code: &types.FunctionCode{ZipFile: zipPackage.Bytes()}, FunctionName: aws.String(functionName), Role: iamRoleArn, Handler: aws.String(handlerName), Publish: true, Runtime: types.RuntimePython39, }) if err != nil { var resConflict *types.ResourceConflictException if errors.As(err, &resConflict) { log.Printf("Function %v already exists.\n", functionName) state = types.StateActive } else { log.Panicf("Couldn't create function %v. Here's why: %v\n", functionName, err) } } else { waiter := lambda.NewFunctionActiveV2Waiter(wrapper.LambdaClient) funcOutput, err := waiter.WaitForOutput(ctx, &lambda.GetFunctionInput{ FunctionName: aws.String(functionName)}, 1*time.Minute) if err != nil { log.Panicf("Couldn't wait for function %v to be active. Here's why: %v\n", functionName, err) } else { state = funcOutput.Configuration.State } } return state } // UpdateFunctionCode updates the code for the Lambda function specified by functionName. // The existing code for the Lambda function is entirely replaced by the code in the // zipPackage buffer. After the update action is called, a lambda.FunctionUpdatedV2Waiter // is used to wait until the update is successful. func (wrapper FunctionWrapper) UpdateFunctionCode(ctx context.Context, functionName string, zipPackage *bytes.Buffer) types.State { var state types.State _, err := wrapper.LambdaClient.UpdateFunctionCode(ctx, &lambda.UpdateFunctionCodeInput{ FunctionName: aws.String(functionName), ZipFile: zipPackage.Bytes(), }) if err != nil { log.Panicf("Couldn't update code for function %v. Here's why: %v\n", functionName, err) } else { waiter := lambda.NewFunctionUpdatedV2Waiter(wrapper.LambdaClient) funcOutput, err := waiter.WaitForOutput(ctx, &lambda.GetFunctionInput{ FunctionName: aws.String(functionName)}, 1*time.Minute) if err != nil { log.Panicf("Couldn't wait for function %v to be active. Here's why: %v\n", functionName, err) } else { state = funcOutput.Configuration.State } } return state } // UpdateFunctionConfiguration updates a map of environment variables configured for // the Lambda function specified by functionName. func (wrapper FunctionWrapper) UpdateFunctionConfiguration(ctx context.Context, functionName string, envVars map[string]string) { _, err := wrapper.LambdaClient.UpdateFunctionConfiguration(ctx, &lambda.UpdateFunctionConfigurationInput{ FunctionName: aws.String(functionName), Environment: &types.Environment{Variables: envVars}, }) if err != nil { log.Panicf("Couldn't update configuration for %v. Here's why: %v", functionName, err) } } // ListFunctions lists up to maxItems functions for the account. This function uses a // lambda.ListFunctionsPaginator to paginate the results. func (wrapper FunctionWrapper) ListFunctions(ctx context.Context, maxItems int) []types.FunctionConfiguration { var functions []types.FunctionConfiguration paginator := lambda.NewListFunctionsPaginator(wrapper.LambdaClient, &lambda.ListFunctionsInput{ MaxItems: aws.Int32(int32(maxItems)), }) for paginator.HasMorePages() && len(functions) < maxItems { pageOutput, err := paginator.NextPage(ctx) if err != nil { log.Panicf("Couldn't list functions for your account. Here's why: %v\n", err) } functions = append(functions, pageOutput.Functions...) } return functions } // 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) } } // Invoke invokes the Lambda function specified by functionName, passing the parameters // as a JSON payload. When getLog is true, types.LogTypeTail is specified, which tells // Lambda to include the last few log lines in the returned result. func (wrapper FunctionWrapper) Invoke(ctx context.Context, functionName string, parameters any, getLog bool) *lambda.InvokeOutput { logType := types.LogTypeNone if getLog { logType = types.LogTypeTail } payload, err := json.Marshal(parameters) if err != nil { log.Panicf("Couldn't marshal parameters to JSON. Here's why %v\n", err) } invokeOutput, err := wrapper.LambdaClient.Invoke(ctx, &lambda.InvokeInput{ FunctionName: aws.String(functionName), LogType: logType, Payload: payload, }) if err != nil { log.Panicf("Couldn't invoke function %v. Here's why: %v\n", functionName, err) } return invokeOutput } // IncrementParameters is used to serialize parameters to the increment Lambda handler. type IncrementParameters struct { Action string `json:"action"` Number int `json:"number"` } // CalculatorParameters is used to serialize parameters to the calculator Lambda handler. type CalculatorParameters struct { Action string `json:"action"` X int `json:"x"` Y int `json:"y"` } // LambdaResultInt is used to deserialize an int result from a Lambda handler. type LambdaResultInt struct { Result int `json:"result"` } // LambdaResultFloat is used to deserialize a float32 result from a Lambda handler. type LambdaResultFloat struct { Result float32 `json:"result"` }
Defina un controlador de Lambda que aumente un número.
import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): """ Accepts an action and a single number, performs the specified action on the number, and returns the result. The only allowable action is 'increment'. :param event: The event dict that contains the parameters sent when the function is invoked. :param context: The context in which the function is called. :return: The result of the action. """ result = None action = event.get("action") if action == "increment": result = event.get("number", 0) + 1 logger.info("Calculated result of %s", result) else: logger.error("%s is not a valid action.", action) response = {"result": result} return response
Defina un segundo controlador de Lambda que realice operaciones aritméticas.
import logging import os logger = logging.getLogger() # Define a list of Python lambda functions that are called by this AWS Lambda function. ACTIONS = { "plus": lambda x, y: x + y, "minus": lambda x, y: x - y, "times": lambda x, y: x * y, "divided-by": lambda x, y: x / y, } def lambda_handler(event, context): """ Accepts an action and two numbers, performs the specified action on the numbers, and returns the result. :param event: The event dict that contains the parameters sent when the function is invoked. :param context: The context in which the function is called. :return: The result of the specified action. """ # Set the log level based on a variable configured in the Lambda environment. logger.setLevel(os.environ.get("LOG_LEVEL", logging.INFO)) logger.debug("Event: %s", event) action = event.get("action") func = ACTIONS.get(action) x = event.get("x") y = event.get("y") result = None try: if func is not None and x is not None and y is not None: result = func(x, y) logger.info("%s %s %s is %s", x, action, y, result) else: logger.error("I can't calculate %s %s %s.", x, action, y) except ZeroDivisionError: logger.warning("I can't divide %s by 0!", x) response = {"result": result} return response
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for Go APIReference.
-
- Java
-
- SDKpara Java 2.x
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. /* * Lambda function names appear as: * * arn:aws:lambda:us-west-2:335556666777:function:HelloFunction * * To find this value, look at the function in the AWS Management Console. * * Before running this Java code example, set up your development environment, including your credentials. * * For more information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * This example performs the following tasks: * * 1. Creates an AWS Lambda function. * 2. Gets a specific AWS Lambda function. * 3. Lists all Lambda functions. * 4. Invokes a Lambda function. * 5. Updates the Lambda function code and invokes it again. * 6. Updates a Lambda function's configuration value. * 7. Deletes a Lambda function. */ public class LambdaScenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws InterruptedException { final String usage = """ Usage: <functionName> <role> <handler> <bucketName> <key>\s Where: functionName - The name of the Lambda function.\s role - The AWS Identity and Access Management (IAM) service role that has Lambda permissions.\s handler - The fully qualified method name (for example, example.Handler::handleRequest).\s bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name that contains the .zip or .jar used to update the Lambda function's code.\s key - The Amazon S3 key name that represents the .zip or .jar (for example, LambdaHello-1.0-SNAPSHOT.jar). """; if (args.length != 5) { System.out.println(usage); return; } String functionName = args[0]; String role = args[1]; String handler = args[2]; String bucketName = args[3]; String key = args[4]; LambdaClient awsLambda = LambdaClient.builder() .build(); System.out.println(DASHES); System.out.println("Welcome to the AWS Lambda Basics scenario."); System.out.println(DASHES); System.out.println(DASHES); System.out.println("1. Create an AWS Lambda function."); String funArn = createLambdaFunction(awsLambda, functionName, key, bucketName, role, handler); System.out.println("The AWS Lambda ARN is " + funArn); System.out.println(DASHES); System.out.println(DASHES); System.out.println("2. Get the " + functionName + " AWS Lambda function."); getFunction(awsLambda, functionName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("3. List all AWS Lambda functions."); listFunctions(awsLambda); System.out.println(DASHES); System.out.println(DASHES); System.out.println("4. Invoke the Lambda function."); System.out.println("*** Sleep for 1 min to get Lambda function ready."); Thread.sleep(60000); invokeFunction(awsLambda, functionName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("5. Update the Lambda function code and invoke it again."); updateFunctionCode(awsLambda, functionName, bucketName, key); System.out.println("*** Sleep for 1 min to get Lambda function ready."); Thread.sleep(60000); invokeFunction(awsLambda, functionName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("6. Update a Lambda function's configuration value."); updateFunctionConfiguration(awsLambda, functionName, handler); System.out.println(DASHES); System.out.println(DASHES); System.out.println("7. Delete the AWS Lambda function."); LambdaScenario.deleteLambdaFunction(awsLambda, functionName); System.out.println(DASHES); System.out.println(DASHES); System.out.println("The AWS Lambda scenario completed successfully"); System.out.println(DASHES); awsLambda.close(); } /** * Creates a new Lambda function in AWS using the AWS Lambda Java API. * * @param awsLambda the AWS Lambda client used to interact with the AWS Lambda service * @param functionName the name of the Lambda function to create * @param key the S3 key of the function code * @param bucketName the name of the S3 bucket containing the function code * @param role the IAM role to assign to the Lambda function * @param handler the fully qualified class name of the function handler * @return the Amazon Resource Name (ARN) of the created Lambda function */ public static String createLambdaFunction(LambdaClient awsLambda, String functionName, String key, String bucketName, String role, String handler) { try { LambdaWaiter waiter = awsLambda.waiter(); FunctionCode code = FunctionCode.builder() .s3Key(key) .s3Bucket(bucketName) .build(); CreateFunctionRequest functionRequest = CreateFunctionRequest.builder() .functionName(functionName) .description("Created by the Lambda Java API") .code(code) .handler(handler) .runtime(Runtime.JAVA17) .role(role) .build(); // Create a Lambda function using a waiter CreateFunctionResponse functionResponse = awsLambda.createFunction(functionRequest); GetFunctionRequest getFunctionRequest = GetFunctionRequest.builder() .functionName(functionName) .build(); WaiterResponse<GetFunctionResponse> waiterResponse = waiter.waitUntilFunctionExists(getFunctionRequest); waiterResponse.matched().response().ifPresent(System.out::println); return functionResponse.functionArn(); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } /** * Retrieves information about 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 AWS Lambda function to retrieve information about */ public static void getFunction(LambdaClient awsLambda, String functionName) { try { GetFunctionRequest functionRequest = GetFunctionRequest.builder() .functionName(functionName) .build(); GetFunctionResponse response = awsLambda.getFunction(functionRequest); System.out.println("The runtime of this Lambda function is " + response.configuration().runtime()); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } /** * Lists the AWS Lambda functions associated with the current AWS account. * * @param awsLambda an instance of the {@link LambdaClient} class, which is used to interact with the AWS Lambda service * * @throws LambdaException if an error occurs while interacting with the AWS Lambda service */ public static void listFunctions(LambdaClient awsLambda) { try { ListFunctionsResponse functionResult = awsLambda.listFunctions(); List<FunctionConfiguration> list = functionResult.functions(); for (FunctionConfiguration config : list) { System.out.println("The function name is " + config.functionName()); } } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } /** * Invokes a specific AWS Lambda function. * * @param awsLambda an instance of {@link LambdaClient} to interact with the AWS Lambda service * @param functionName the name of the AWS Lambda function to be invoked */ public static void invokeFunction(LambdaClient awsLambda, String functionName) { InvokeResponse res; try { // Need a SdkBytes instance for the payload. JSONObject jsonObj = new JSONObject(); jsonObj.put("inputValue", "2000"); String json = jsonObj.toString(); SdkBytes payload = SdkBytes.fromUtf8String(json); InvokeRequest request = InvokeRequest.builder() .functionName(functionName) .payload(payload) .build(); res = awsLambda.invoke(request); String value = res.payload().asUtf8String(); System.out.println(value); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } /** * Updates the code for an AWS Lambda function. * * @param awsLambda the AWS Lambda client * @param functionName the name of the Lambda function to update * @param bucketName the name of the S3 bucket where the function code is located * @param key the key (file name) of the function code in the S3 bucket * @throws LambdaException if there is an error updating the function code */ public static void updateFunctionCode(LambdaClient awsLambda, String functionName, String bucketName, String key) { try { LambdaWaiter waiter = awsLambda.waiter(); UpdateFunctionCodeRequest functionCodeRequest = UpdateFunctionCodeRequest.builder() .functionName(functionName) .publish(true) .s3Bucket(bucketName) .s3Key(key) .build(); UpdateFunctionCodeResponse response = awsLambda.updateFunctionCode(functionCodeRequest); GetFunctionConfigurationRequest getFunctionConfigRequest = GetFunctionConfigurationRequest.builder() .functionName(functionName) .build(); WaiterResponse<GetFunctionConfigurationResponse> waiterResponse = waiter .waitUntilFunctionUpdated(getFunctionConfigRequest); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("The last modified value is " + response.lastModified()); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } /** * Updates the configuration of an AWS Lambda function. * * @param awsLambda the {@link LambdaClient} instance to use for the AWS Lambda operation * @param functionName the name of the AWS Lambda function to update * @param handler the new handler for the AWS Lambda function * * @throws LambdaException if there is an error while updating the function configuration */ public static void updateFunctionConfiguration(LambdaClient awsLambda, String functionName, String handler) { try { UpdateFunctionConfigurationRequest configurationRequest = UpdateFunctionConfigurationRequest.builder() .functionName(functionName) .handler(handler) .runtime(Runtime.JAVA17) .build(); awsLambda.updateFunctionConfiguration(configurationRequest); } catch (LambdaException e) { System.err.println(e.getMessage()); System.exit(1); } } /** * 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); } } }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for Java 2.x APIReference.
-
- JavaScript
-
- SDKpara JavaScript (v3)
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Cree un rol AWS Identity and Access Management (IAM) que conceda permiso a Lambda para escribir en los registros.
logger.log(`Creating role (${NAME_ROLE_LAMBDA})...`); const response = await createRole(NAME_ROLE_LAMBDA); import { AttachRolePolicyCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * * @param {string} policyArn * @param {string} roleName */ export const attachRolePolicy = (policyArn, roleName) => { const command = new AttachRolePolicyCommand({ PolicyArn: policyArn, RoleName: roleName, }); return client.send(command); };
Cree una función de Lambda y cargue el código de controlador.
const createFunction = async (funcName, roleArn) => { const client = new LambdaClient({}); const code = await readFile(`${dirname}../functions/${funcName}.zip`); const command = new CreateFunctionCommand({ Code: { ZipFile: code }, FunctionName: funcName, Role: roleArn, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); };
Invoque la función con un único parámetro y obtenga resultados.
const invoke = async (funcName, payload) => { const client = new LambdaClient({}); const command = new InvokeCommand({ FunctionName: funcName, Payload: JSON.stringify(payload), LogType: LogType.Tail, }); const { Payload, LogResult } = await client.send(command); const result = Buffer.from(Payload).toString(); const logs = Buffer.from(LogResult, "base64").toString(); return { logs, result }; };
Actualice el código de la función y configure su entorno Lambda con una variable de entorno.
const updateFunctionCode = async (funcName, newFunc) => { const client = new LambdaClient({}); const code = await readFile(`${dirname}../functions/${newFunc}.zip`); const command = new UpdateFunctionCodeCommand({ ZipFile: code, FunctionName: funcName, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); }; const updateFunctionConfiguration = (funcName) => { const client = new LambdaClient({}); const config = readFileSync(`${dirname}../functions/config.json`).toString(); const command = new UpdateFunctionConfigurationCommand({ ...JSON.parse(config), FunctionName: funcName, }); const result = client.send(command); waitForFunctionUpdated({ FunctionName: funcName }); return result; };
Enumere las funciones de su cuenta.
const listFunctions = () => { const client = new LambdaClient({}); const command = new ListFunctionsCommand({}); return client.send(command); };
Elimine el IAM rol y la función Lambda.
import { DeleteRoleCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * * @param {string} roleName */ export const deleteRole = (roleName) => { const command = new DeleteRoleCommand({ RoleName: roleName }); return client.send(command); }; /** * @param {string} funcName */ const deleteFunction = (funcName) => { const client = new LambdaClient({}); const command = new DeleteFunctionCommand({ FunctionName: funcName }); return client.send(command); };
-
Para API obtener más información, consulte los siguientes temas en la sección de AWS SDK for JavaScript APIreferencia.
-
- Kotlin
-
- SDKpara Kotlin
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. suspend fun main(args: Array<String>) { val usage = """ Usage: <functionName> <role> <handler> <bucketName> <updatedBucketName> <key> Where: functionName - The name of the AWS Lambda function. role - The AWS Identity and Access Management (IAM) service role that has AWS Lambda permissions. handler - The fully qualified method name (for example, example.Handler::handleRequest). bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name that contains the ZIP or JAR used for the Lambda function's code. updatedBucketName - The Amazon S3 bucket name that contains the .zip or .jar used to update the Lambda function's code. key - The Amazon S3 key name that represents the .zip or .jar file (for example, LambdaHello-1.0-SNAPSHOT.jar). """ if (args.size != 6) { println(usage) exitProcess(1) } val functionName = args[0] val role = args[1] val handler = args[2] val bucketName = args[3] val updatedBucketName = args[4] val key = args[5] println("Creating a Lambda function named $functionName.") val funArn = createScFunction(functionName, bucketName, key, handler, role) println("The AWS Lambda ARN is $funArn") // Get a specific Lambda function. println("Getting the $functionName AWS Lambda function.") getFunction(functionName) // List the Lambda functions. println("Listing all AWS Lambda functions.") listFunctionsSc() // Invoke the Lambda function. println("*** Invoke the Lambda function.") invokeFunctionSc(functionName) // Update the AWS Lambda function code. println("*** Update the Lambda function code.") updateFunctionCode(functionName, updatedBucketName, key) // println("*** Invoke the function again after updating the code.") invokeFunctionSc(functionName) // Update the AWS Lambda function configuration. println("Update the run time of the function.") updateFunctionConfiguration(functionName, handler) // Delete the AWS Lambda function. println("Delete the AWS Lambda function.") delFunction(functionName) } suspend fun createScFunction( myFunctionName: String, s3BucketName: String, myS3Key: String, myHandler: String, myRole: String, ): String { val functionCode = FunctionCode { s3Bucket = s3BucketName s3Key = myS3Key } val request = CreateFunctionRequest { functionName = myFunctionName code = functionCode description = "Created by the Lambda Kotlin API" handler = myHandler role = myRole runtime = Runtime.Java8 } // Create a Lambda function using a waiter LambdaClient { region = "us-west-2" }.use { awsLambda -> val functionResponse = awsLambda.createFunction(request) awsLambda.waitUntilFunctionActive { functionName = myFunctionName } return functionResponse.functionArn.toString() } } suspend fun getFunction(functionNameVal: String) { val functionRequest = GetFunctionRequest { functionName = functionNameVal } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.getFunction(functionRequest) println("The runtime of this Lambda function is ${response.configuration?.runtime}") } } suspend fun listFunctionsSc() { val request = ListFunctionsRequest { maxItems = 10 } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.listFunctions(request) response.functions?.forEach { function -> println("The function name is ${function.functionName}") } } } suspend fun invokeFunctionSc(functionNameVal: String) { val json = """{"inputValue":"1000"}""" val byteArray = json.trimIndent().encodeToByteArray() val request = InvokeRequest { functionName = functionNameVal payload = byteArray logType = LogType.Tail } LambdaClient { region = "us-west-2" }.use { awsLambda -> val res = awsLambda.invoke(request) println("The function payload is ${res.payload?.toString(Charsets.UTF_8)}") } } suspend fun updateFunctionCode( functionNameVal: String?, bucketName: String?, key: String?, ) { val functionCodeRequest = UpdateFunctionCodeRequest { functionName = functionNameVal publish = true s3Bucket = bucketName s3Key = key } LambdaClient { region = "us-west-2" }.use { awsLambda -> val response = awsLambda.updateFunctionCode(functionCodeRequest) awsLambda.waitUntilFunctionUpdated { functionName = functionNameVal } println("The last modified value is " + response.lastModified) } } suspend fun updateFunctionConfiguration( functionNameVal: String?, handlerVal: String?, ) { val configurationRequest = UpdateFunctionConfigurationRequest { functionName = functionNameVal handler = handlerVal runtime = Runtime.Java11 } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.updateFunctionConfiguration(configurationRequest) } } suspend fun delFunction(myFunctionName: String) { val request = DeleteFunctionRequest { functionName = myFunctionName } LambdaClient { region = "us-west-2" }.use { awsLambda -> awsLambda.deleteFunction(request) println("$myFunctionName was deleted") } }
-
Para API obtener más información, consulta los siguientes temas en la sección AWS SDKde API referencia sobre Kotlin.
-
- PHP
-
- SDK para PHP
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. namespace Lambda; use Aws\S3\S3Client; use GuzzleHttp\Psr7\Stream; use Iam\IAMService; class GettingStartedWithLambda { public function run() { echo("\n"); echo("--------------------------------------\n"); print("Welcome to the AWS Lambda getting started demo using PHP!\n"); echo("--------------------------------------\n"); $clientArgs = [ 'region' => 'us-west-2', 'version' => 'latest', 'profile' => 'default', ]; $uniqid = uniqid(); $iamService = new IAMService(); $s3client = new S3Client($clientArgs); $lambdaService = new LambdaService(); echo "First, let's create a role to run our Lambda code.\n"; $roleName = "test-lambda-role-$uniqid"; $rolePolicyDocument = "{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"Service\": \"lambda.amazonaws.com\" }, \"Action\": \"sts:AssumeRole\" } ] }"; $role = $iamService->createRole($roleName, $rolePolicyDocument); echo "Created role {$role['RoleName']}.\n"; $iamService->attachRolePolicy( $role['RoleName'], "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ); echo "Attached the AWSLambdaBasicExecutionRole to {$role['RoleName']}.\n"; echo "\nNow let's create an S3 bucket and upload our Lambda code there.\n"; $bucketName = "test-example-bucket-$uniqid"; $s3client->createBucket([ 'Bucket' => $bucketName, ]); echo "Created bucket $bucketName.\n"; $functionName = "doc_example_lambda_$uniqid"; $codeBasic = __DIR__ . "/lambda_handler_basic.zip"; $handler = "lambda_handler_basic"; $file = file_get_contents($codeBasic); $s3client->putObject([ 'Bucket' => $bucketName, 'Key' => $functionName, 'Body' => $file, ]); echo "Uploaded the Lambda code.\n"; $createLambdaFunction = $lambdaService->createFunction($functionName, $role, $bucketName, $handler); // Wait until the function has finished being created. do { $getLambdaFunction = $lambdaService->getFunction($createLambdaFunction['FunctionName']); } while ($getLambdaFunction['Configuration']['State'] == "Pending"); echo "Created Lambda function {$getLambdaFunction['Configuration']['FunctionName']}.\n"; sleep(1); echo "\nOk, let's invoke that Lambda code.\n"; $basicParams = [ 'action' => 'increment', 'number' => 3, ]; /** @var Stream $invokeFunction */ $invokeFunction = $lambdaService->invoke($functionName, $basicParams)['Payload']; $result = json_decode($invokeFunction->getContents())->result; echo "After invoking the Lambda code with the input of {$basicParams['number']} we received $result.\n"; echo "\nSince that's working, let's update the Lambda code.\n"; $codeCalculator = "lambda_handler_calculator.zip"; $handlerCalculator = "lambda_handler_calculator"; echo "First, put the new code into the S3 bucket.\n"; $file = file_get_contents($codeCalculator); $s3client->putObject([ 'Bucket' => $bucketName, 'Key' => $functionName, 'Body' => $file, ]); echo "New code uploaded.\n"; $lambdaService->updateFunctionCode($functionName, $bucketName, $functionName); // Wait for the Lambda code to finish updating. do { $getLambdaFunction = $lambdaService->getFunction($createLambdaFunction['FunctionName']); } while ($getLambdaFunction['Configuration']['LastUpdateStatus'] !== "Successful"); echo "New Lambda code uploaded.\n"; $environment = [ 'Variable' => ['Variables' => ['LOG_LEVEL' => 'DEBUG']], ]; $lambdaService->updateFunctionConfiguration($functionName, $handlerCalculator, $environment); do { $getLambdaFunction = $lambdaService->getFunction($createLambdaFunction['FunctionName']); } while ($getLambdaFunction['Configuration']['LastUpdateStatus'] !== "Successful"); echo "Lambda code updated with new handler and a LOG_LEVEL of DEBUG for more information.\n"; echo "Invoke the new code with some new data.\n"; $calculatorParams = [ 'action' => 'plus', 'x' => 5, 'y' => 4, ]; $invokeFunction = $lambdaService->invoke($functionName, $calculatorParams, "Tail"); $result = json_decode($invokeFunction['Payload']->getContents())->result; echo "Indeed, {$calculatorParams['x']} + {$calculatorParams['y']} does equal $result.\n"; echo "Here's the extra debug info: "; echo base64_decode($invokeFunction['LogResult']) . "\n"; echo "\nBut what happens if you try to divide by zero?\n"; $divZeroParams = [ 'action' => 'divide', 'x' => 5, 'y' => 0, ]; $invokeFunction = $lambdaService->invoke($functionName, $divZeroParams, "Tail"); $result = json_decode($invokeFunction['Payload']->getContents())->result; echo "You get a |$result| result.\n"; echo "And an error message: "; echo base64_decode($invokeFunction['LogResult']) . "\n"; echo "\nHere's all the Lambda functions you have in this Region:\n"; $listLambdaFunctions = $lambdaService->listFunctions(5); $allLambdaFunctions = $listLambdaFunctions['Functions']; $next = $listLambdaFunctions->get('NextMarker'); while ($next != false) { $listLambdaFunctions = $lambdaService->listFunctions(5, $next); $next = $listLambdaFunctions->get('NextMarker'); $allLambdaFunctions = array_merge($allLambdaFunctions, $listLambdaFunctions['Functions']); } foreach ($allLambdaFunctions as $function) { echo "{$function['FunctionName']}\n"; } echo "\n\nAnd don't forget to clean up your data!\n"; $lambdaService->deleteFunction($functionName); echo "Deleted Lambda function.\n"; $iamService->deleteRole($role['RoleName']); echo "Deleted Role.\n"; $deleteObjects = $s3client->listObjectsV2([ 'Bucket' => $bucketName, ]); $deleteObjects = $s3client->deleteObjects([ 'Bucket' => $bucketName, 'Delete' => [ 'Objects' => $deleteObjects['Contents'], ] ]); echo "Deleted all objects from the S3 bucket.\n"; $s3client->deleteBucket(['Bucket' => $bucketName]); echo "Deleted the bucket.\n"; } }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK for PHP APIReference.
-
- Python
-
- SDKpara Python (Boto3)
-
nota
Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Defina un controlador de Lambda que aumente un número.
import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): """ Accepts an action and a single number, performs the specified action on the number, and returns the result. The only allowable action is 'increment'. :param event: The event dict that contains the parameters sent when the function is invoked. :param context: The context in which the function is called. :return: The result of the action. """ result = None action = event.get("action") if action == "increment": result = event.get("number", 0) + 1 logger.info("Calculated result of %s", result) else: logger.error("%s is not a valid action.", action) response = {"result": result} return response
Defina un segundo controlador Lambda que realice operaciones aritméticas.
import logging import os logger = logging.getLogger() # Define a list of Python lambda functions that are called by this AWS Lambda function. ACTIONS = { "plus": lambda x, y: x + y, "minus": lambda x, y: x - y, "times": lambda x, y: x * y, "divided-by": lambda x, y: x / y, } def lambda_handler(event, context): """ Accepts an action and two numbers, performs the specified action on the numbers, and returns the result. :param event: The event dict that contains the parameters sent when the function is invoked. :param context: The context in which the function is called. :return: The result of the specified action. """ # Set the log level based on a variable configured in the Lambda environment. logger.setLevel(os.environ.get("LOG_LEVEL", logging.INFO)) logger.debug("Event: %s", event) action = event.get("action") func = ACTIONS.get(action) x = event.get("x") y = event.get("y") result = None try: if func is not None and x is not None and y is not None: result = func(x, y) logger.info("%s %s %s is %s", x, action, y, result) else: logger.error("I can't calculate %s %s %s.", x, action, y) except ZeroDivisionError: logger.warning("I can't divide %s by 0!", x) response = {"result": result} return response
Cree funciones que ajusten las acciones de Lambda.
class LambdaWrapper: def __init__(self, lambda_client, iam_resource): self.lambda_client = lambda_client self.iam_resource = iam_resource @staticmethod def create_deployment_package(source_file, destination_file): """ Creates a Lambda deployment package in .zip format in an in-memory buffer. This buffer can be passed directly to Lambda when creating the function. :param source_file: The name of the file that contains the Lambda handler function. :param destination_file: The name to give the file when it's deployed to Lambda. :return: The deployment package. """ buffer = io.BytesIO() with zipfile.ZipFile(buffer, "w") as zipped: zipped.write(source_file, destination_file) buffer.seek(0) return buffer.read() def get_iam_role(self, iam_role_name): """ Get an AWS Identity and Access Management (IAM) role. :param iam_role_name: The name of the role to retrieve. :return: The IAM role. """ role = None try: temp_role = self.iam_resource.Role(iam_role_name) temp_role.load() role = temp_role logger.info("Got IAM role %s", role.name) except ClientError as err: if err.response["Error"]["Code"] == "NoSuchEntity": logger.info("IAM role %s does not exist.", iam_role_name) else: logger.error( "Couldn't get IAM role %s. Here's why: %s: %s", iam_role_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise return role def create_iam_role_for_lambda(self, iam_role_name): """ Creates an IAM role that grants the Lambda function basic permissions. If a role with the specified name already exists, it is used for the demo. :param iam_role_name: The name of the role to create. :return: The role and a value that indicates whether the role is newly created. """ role = self.get_iam_role(iam_role_name) if role is not None: return role, False lambda_assume_role_policy = { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole", } ], } policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" try: role = self.iam_resource.create_role( RoleName=iam_role_name, AssumeRolePolicyDocument=json.dumps(lambda_assume_role_policy), ) logger.info("Created role %s.", role.name) role.attach_policy(PolicyArn=policy_arn) logger.info("Attached basic execution policy to role %s.", role.name) except ClientError as error: if error.response["Error"]["Code"] == "EntityAlreadyExists": role = self.iam_resource.Role(iam_role_name) logger.warning("The role %s already exists. Using it.", iam_role_name) else: logger.exception( "Couldn't create role %s or attach policy %s.", iam_role_name, policy_arn, ) raise return role, True 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 def create_function( self, function_name, handler_name, iam_role, deployment_package ): """ Deploys a Lambda function. :param function_name: The name of the Lambda function. :param handler_name: The fully qualified name of the handler function. This must include the file name and the function name. :param iam_role: The IAM role to use for the function. :param deployment_package: The deployment package that contains the function code in .zip format. :return: The Amazon Resource Name (ARN) of the newly created function. """ try: response = self.lambda_client.create_function( FunctionName=function_name, Description="AWS Lambda doc example", Runtime="python3.9", Role=iam_role.arn, Handler=handler_name, Code={"ZipFile": deployment_package}, Publish=True, ) function_arn = response["FunctionArn"] waiter = self.lambda_client.get_waiter("function_active_v2") waiter.wait(FunctionName=function_name) logger.info( "Created function '%s' with ARN: '%s'.", function_name, response["FunctionArn"], ) except ClientError: logger.error("Couldn't create function %s.", function_name) raise else: return function_arn 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 def invoke_function(self, function_name, function_params, get_log=False): """ Invokes a Lambda function. :param function_name: The name of the function to invoke. :param function_params: The parameters of the function as a dict. This dict is serialized to JSON before it is sent to Lambda. :param get_log: When true, the last 4 KB of the execution log are included in the response. :return: The response from the function invocation. """ try: response = self.lambda_client.invoke( FunctionName=function_name, Payload=json.dumps(function_params), LogType="Tail" if get_log else "None", ) logger.info("Invoked function %s.", function_name) except ClientError: logger.exception("Couldn't invoke function %s.", function_name) raise return response def update_function_code(self, function_name, deployment_package): """ Updates the code for a Lambda function by submitting a .zip archive that contains the code for the function. :param function_name: The name of the function to update. :param deployment_package: The function code to update, packaged as bytes in .zip format. :return: Data about the update, including the status. """ try: response = self.lambda_client.update_function_code( FunctionName=function_name, ZipFile=deployment_package ) except ClientError as err: logger.error( "Couldn't update function %s. Here's why: %s: %s", function_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def update_function_configuration(self, function_name, env_vars): """ Updates the environment variables for a Lambda function. :param function_name: The name of the function to update. :param env_vars: A dict of environment variables to update. :return: Data about the update, including the status. """ try: response = self.lambda_client.update_function_configuration( FunctionName=function_name, Environment={"Variables": env_vars} ) except ClientError as err: logger.error( "Couldn't update function configuration %s. Here's why: %s: %s", function_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def list_functions(self): """ Lists the Lambda functions for the current account. """ try: func_paginator = self.lambda_client.get_paginator("list_functions") for func_page in func_paginator.paginate(): for func in func_page["Functions"]: print(func["FunctionName"]) desc = func.get("Description") if desc: print(f"\t{desc}") print(f"\t{func['Runtime']}: {func['Handler']}") except ClientError as err: logger.error( "Couldn't list functions. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
Cree una función que ejecute el escenario.
class UpdateFunctionWaiter(CustomWaiter): """A custom waiter that waits until a function is successfully updated.""" def __init__(self, client): super().__init__( "UpdateSuccess", "GetFunction", "Configuration.LastUpdateStatus", {"Successful": WaitState.SUCCESS, "Failed": WaitState.FAILURE}, client, ) def wait(self, function_name): self._wait(FunctionName=function_name) def run_scenario(lambda_client, iam_resource, basic_file, calculator_file, lambda_name): """ Runs the scenario. :param lambda_client: A Boto3 Lambda client. :param iam_resource: A Boto3 IAM resource. :param basic_file: The name of the file that contains the basic Lambda handler. :param calculator_file: The name of the file that contains the calculator Lambda handler. :param lambda_name: The name to give resources created for the scenario, such as the IAM role and the Lambda function. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print("Welcome to the AWS Lambda getting started with functions demo.") print("-" * 88) wrapper = LambdaWrapper(lambda_client, iam_resource) print("Checking for IAM role for Lambda...") iam_role, should_wait = wrapper.create_iam_role_for_lambda(lambda_name) if should_wait: logger.info("Giving AWS time to create resources...") wait(10) print(f"Looking for function {lambda_name}...") function = wrapper.get_function(lambda_name) if function is None: print("Zipping the Python script into a deployment package...") deployment_package = wrapper.create_deployment_package( basic_file, f"{lambda_name}.py" ) print(f"...and creating the {lambda_name} Lambda function.") wrapper.create_function( lambda_name, f"{lambda_name}.lambda_handler", iam_role, deployment_package ) else: print(f"Function {lambda_name} already exists.") print("-" * 88) print(f"Let's invoke {lambda_name}. This function increments a number.") action_params = { "action": "increment", "number": q.ask("Give me a number to increment: ", q.is_int), } print(f"Invoking {lambda_name}...") response = wrapper.invoke_function(lambda_name, action_params) print( f"Incrementing {action_params['number']} resulted in " f"{json.load(response['Payload'])}" ) print("-" * 88) print(f"Let's update the function to an arithmetic calculator.") q.ask("Press Enter when you're ready.") print("Creating a new deployment package...") deployment_package = wrapper.create_deployment_package( calculator_file, f"{lambda_name}.py" ) print(f"...and updating the {lambda_name} Lambda function.") update_waiter = UpdateFunctionWaiter(lambda_client) wrapper.update_function_code(lambda_name, deployment_package) update_waiter.wait(lambda_name) print(f"This function uses an environment variable to control logging level.") print(f"Let's set it to DEBUG to get the most logging.") wrapper.update_function_configuration( lambda_name, {"LOG_LEVEL": logging.getLevelName(logging.DEBUG)} ) actions = ["plus", "minus", "times", "divided-by"] want_invoke = True while want_invoke: print(f"Let's invoke {lambda_name}. You can invoke these actions:") for index, action in enumerate(actions): print(f"{index + 1}: {action}") action_params = {} action_index = q.ask( "Enter the number of the action you want to take: ", q.is_int, q.in_range(1, len(actions)), ) action_params["action"] = actions[action_index - 1] print(f"You've chosen to invoke 'x {action_params['action']} y'.") action_params["x"] = q.ask("Enter a value for x: ", q.is_int) action_params["y"] = q.ask("Enter a value for y: ", q.is_int) print(f"Invoking {lambda_name}...") response = wrapper.invoke_function(lambda_name, action_params, True) print( f"Calculating {action_params['x']} {action_params['action']} {action_params['y']} " f"resulted in {json.load(response['Payload'])}" ) q.ask("Press Enter to see the logs from the call.") print(base64.b64decode(response["LogResult"]).decode()) want_invoke = q.ask("That was fun. Shall we do it again? (y/n) ", q.is_yesno) print("-" * 88) if q.ask( "Do you want to list all of the functions in your account? (y/n) ", q.is_yesno ): wrapper.list_functions() print("-" * 88) if q.ask("Ready to delete the function and role? (y/n) ", q.is_yesno): for policy in iam_role.attached_policies.all(): policy.detach_role(RoleName=iam_role.name) iam_role.delete() print(f"Deleted role {lambda_name}.") wrapper.delete_function(lambda_name) print(f"Deleted function {lambda_name}.") print("\nThanks for watching!") print("-" * 88) if __name__ == "__main__": try: run_scenario( boto3.client("lambda"), boto3.resource("iam"), "lambda_handler_basic.py", "lambda_handler_calculator.py", "doc_example_lambda_calculator", ) except Exception: logging.exception("Something went wrong with the demo!")
-
Para API obtener más información, consulte los siguientes temas en la sección AWS SDKde referencia sobre Python (Boto3). API
-
- Ruby
-
- SDKpara Ruby
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. Configure los IAM permisos necesarios para una función Lambda capaz de escribir registros.
# Get an AWS Identity and Access Management (IAM) role. # # @param iam_role_name: The name of the role to retrieve. # @param action: Whether to create or destroy the IAM apparatus. # @return: The IAM role. def manage_iam(iam_role_name, action) case action when 'create' create_iam_role(iam_role_name) when 'destroy' destroy_iam_role(iam_role_name) else raise "Incorrect action provided. Must provide 'create' or 'destroy'" end end private def create_iam_role(iam_role_name) role_policy = { 'Version': '2012-10-17', 'Statement': [ { 'Effect': 'Allow', 'Principal': { 'Service': 'lambda.amazonaws.com' }, 'Action': 'sts:AssumeRole' } ] } role = @iam_client.create_role( role_name: iam_role_name, assume_role_policy_document: role_policy.to_json ) @iam_client.attach_role_policy( { policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', role_name: iam_role_name } ) wait_for_role_to_exist(iam_role_name) @logger.debug("Successfully created IAM role: #{role['role']['arn']}") sleep(10) [role, role_policy.to_json] end def destroy_iam_role(iam_role_name) @iam_client.detach_role_policy( { policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole', role_name: iam_role_name } ) @iam_client.delete_role(role_name: iam_role_name) @logger.debug("Detached policy & deleted IAM role: #{iam_role_name}") end def wait_for_role_to_exist(iam_role_name) @iam_client.wait_until(:role_exists, { role_name: iam_role_name }) do |w| w.max_attempts = 5 w.delay = 5 end end
Definir un controlador de Lambda que aumente un número dado como parámetro de invocación.
require 'logger' # A function that increments a whole number by one (1) and logs the result. # Requires a manually-provided runtime parameter, 'number', which must be Int # # @param event [Hash] Parameters sent when the function is invoked # @param context [Hash] Methods and properties that provide information # about the invocation, function, and execution environment. # @return incremented_number [String] The incremented number. def lambda_handler(event:, context:) logger = Logger.new($stdout) log_level = ENV['LOG_LEVEL'] logger.level = case log_level when 'debug' Logger::DEBUG when 'info' Logger::INFO else Logger::ERROR end logger.debug('This is a debug log message.') logger.info('This is an info log message. Code executed successfully!') number = event['number'].to_i incremented_number = number + 1 logger.info("You provided #{number.round} and it was incremented to #{incremented_number.round}") incremented_number.round.to_s end
Comprimir su función de Lambda en un paquete de implementación.
# Creates a Lambda deployment package in .zip format. # # @param source_file: The name of the object, without suffix, for the Lambda file and zip. # @return: The deployment package. def create_deployment_package(source_file) Dir.chdir(File.dirname(__FILE__)) if File.exist?('lambda_function.zip') File.delete('lambda_function.zip') @logger.debug('Deleting old zip: lambda_function.zip') end Zip::File.open('lambda_function.zip', create: true) do |zipfile| zipfile.add('lambda_function.rb', "#{source_file}.rb") end @logger.debug("Zipping #{source_file}.rb into: lambda_function.zip.") File.read('lambda_function.zip').to_s rescue StandardError => e @logger.error("There was an error creating deployment package:\n #{e.message}") end
Crear una nueva función de Lambda.
# Deploys a Lambda function. # # @param function_name: The name of the Lambda function. # @param handler_name: The fully qualified name of the handler function. # @param role_arn: The IAM role to use for the function. # @param deployment_package: The deployment package that contains the function code in .zip format. # @return: The Amazon Resource Name (ARN) of the newly created function. def create_function(function_name, handler_name, role_arn, deployment_package) response = @lambda_client.create_function({ role: role_arn.to_s, function_name: function_name, handler: handler_name, runtime: 'ruby2.7', code: { zip_file: deployment_package }, environment: { variables: { 'LOG_LEVEL' => 'info' } } }) @lambda_client.wait_until(:function_active_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end response rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error creating #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
Invocar su función de Lambda con parámetros de tiempo de ejecución opcionales.
# Invokes a Lambda function. # @param function_name [String] The name of the function to invoke. # @param payload [nil] Payload containing runtime parameters. # @return [Object] The response from the function invocation. def invoke_function(function_name, payload = nil) params = { function_name: function_name } params[:payload] = payload unless payload.nil? @lambda_client.invoke(params) rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error executing #{function_name}:\n #{e.message}") end
Actualizar la configuración de su función de Lambda para introducir una nueva variable de entorno.
# Updates the environment variables for a Lambda function. # @param function_name: The name of the function to update. # @param log_level: The log level of the function. # @return: Data about the update, including the status. def update_function_configuration(function_name, log_level) @lambda_client.update_function_configuration({ function_name: function_name, environment: { variables: { 'LOG_LEVEL' => log_level } } }) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating configurations for #{function_name}:\n #{e.message}") rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}") end
Actualizar el código de su función de Lambda con un paquete de implementación diferente que contenga un código diferente.
# Updates the code for a Lambda function by submitting a .zip archive that contains # the code for the function. # # @param function_name: The name of the function to update. # @param deployment_package: The function code to update, packaged as bytes in # .zip format. # @return: Data about the update, including the status. def update_function_code(function_name, deployment_package) @lambda_client.update_function_code( function_name: function_name, zip_file: deployment_package ) @lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w| w.max_attempts = 5 w.delay = 5 end rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error updating function code for: #{function_name}:\n #{e.message}") nil rescue Aws::Waiters::Errors::WaiterFailed => e @logger.error("Failed waiting for #{function_name} to update:\n #{e.message}") end
Enumerar todas las funciones de Lambda existentes mediante el paginador integrado.
# Lists the Lambda functions for the current account. def list_functions functions = [] @lambda_client.list_functions.each do |response| response['functions'].each do |function| functions.append(function['function_name']) end end functions rescue Aws::Lambda::Errors::ServiceException => e @logger.error("There was an error listing functions:\n #{e.message}") end
Eliminar una función de Lambda específica.
# 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
-
Para API obtener más información, consulte los siguientes temas en la sección de referencia.AWS SDK for Ruby API
-
- Rust
-
- SDKpara Rust
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. El Cargo.toml con las dependencias utilizadas en este escenario.
[package] name = "lambda-code-examples" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] aws-config = { version = "1.0.1", features = ["behavior-version-latest"] } aws-sdk-ec2 = { version = "1.3.0" } aws-sdk-iam = { version = "1.3.0" } aws-sdk-lambda = { version = "1.3.0" } aws-sdk-s3 = { version = "1.4.0" } aws-smithy-types = { version = "1.0.1" } aws-types = { version = "1.0.1" } clap = { version = "4.4", features = ["derive"] } tokio = { version = "1.20.1", features = ["full"] } tracing-subscriber = { version = "0.3.15", features = ["env-filter"] } tracing = "0.1.37" serde_json = "1.0.94" anyhow = "1.0.71" uuid = { version = "1.3.3", features = ["v4"] } lambda_runtime = "0.8.0" serde = "1.0.164"
Un conjunto de utilidades que simplifican las llamadas a Lambda para este escenario. Este archivo es src/ations.rs en la caja.
use anyhow::anyhow; use aws_sdk_iam::operation::{create_role::CreateRoleError, delete_role::DeleteRoleOutput}; use aws_sdk_lambda::{ operation::{ delete_function::DeleteFunctionOutput, get_function::GetFunctionOutput, invoke::InvokeOutput, list_functions::ListFunctionsOutput, update_function_code::UpdateFunctionCodeOutput, update_function_configuration::UpdateFunctionConfigurationOutput, }, primitives::ByteStream, types::{Environment, FunctionCode, LastUpdateStatus, State}, }; use aws_sdk_s3::{ error::ErrorMetadata, operation::{delete_bucket::DeleteBucketOutput, delete_object::DeleteObjectOutput}, types::CreateBucketConfiguration, }; use aws_smithy_types::Blob; use serde::{ser::SerializeMap, Serialize}; use std::{fmt::Display, path::PathBuf, str::FromStr, time::Duration}; use tracing::{debug, info, warn}; /* Operation describes */ #[derive(Clone, Copy, Debug, Serialize)] pub enum Operation { #[serde(rename = "plus")] Plus, #[serde(rename = "minus")] Minus, #[serde(rename = "times")] Times, #[serde(rename = "divided-by")] DividedBy, } impl FromStr for Operation { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { match s { "plus" => Ok(Operation::Plus), "minus" => Ok(Operation::Minus), "times" => Ok(Operation::Times), "divided-by" => Ok(Operation::DividedBy), _ => Err(anyhow!("Unknown operation {s}")), } } } impl Display for Operation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Operation::Plus => write!(f, "plus"), Operation::Minus => write!(f, "minus"), Operation::Times => write!(f, "times"), Operation::DividedBy => write!(f, "divided-by"), } } } /** * InvokeArgs will be serialized as JSON and sent to the AWS Lambda handler. */ #[derive(Debug)] pub enum InvokeArgs { Increment(i32), Arithmetic(Operation, i32, i32), } impl Serialize for InvokeArgs { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer, { match self { InvokeArgs::Increment(i) => serializer.serialize_i32(*i), InvokeArgs::Arithmetic(o, i, j) => { let mut map: S::SerializeMap = serializer.serialize_map(Some(3))?; map.serialize_key(&"op".to_string())?; map.serialize_value(&o.to_string())?; map.serialize_key(&"i".to_string())?; map.serialize_value(&i)?; map.serialize_key(&"j".to_string())?; map.serialize_value(&j)?; map.end() } } } } /** A policy document allowing Lambda to execute this function on the account's behalf. */ const ROLE_POLICY_DOCUMENT: &str = r#"{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }"#; /** * A LambdaManager gathers all the resources necessary to run the Lambda example scenario. * This includes instantiated aws_sdk clients and details of resource names. */ pub struct LambdaManager { iam_client: aws_sdk_iam::Client, lambda_client: aws_sdk_lambda::Client, s3_client: aws_sdk_s3::Client, lambda_name: String, role_name: String, bucket: String, own_bucket: bool, } // These unit type structs provide nominal typing on top of String parameters for LambdaManager::new pub struct LambdaName(pub String); pub struct RoleName(pub String); pub struct Bucket(pub String); pub struct OwnBucket(pub bool); impl LambdaManager { pub fn new( iam_client: aws_sdk_iam::Client, lambda_client: aws_sdk_lambda::Client, s3_client: aws_sdk_s3::Client, lambda_name: LambdaName, role_name: RoleName, bucket: Bucket, own_bucket: OwnBucket, ) -> Self { Self { iam_client, lambda_client, s3_client, lambda_name: lambda_name.0, role_name: role_name.0, bucket: bucket.0, own_bucket: own_bucket.0, } } /** * Load the AWS configuration from the environment. * Look up lambda_name and bucket if none are given, or generate a random name if not present in the environment. * If the bucket name is provided, the caller needs to have created the bucket. * If the bucket name is generated, it will be created. */ pub async fn load_from_env(lambda_name: Option<String>, bucket: Option<String>) -> Self { let sdk_config = aws_config::load_from_env().await; let lambda_name = LambdaName(lambda_name.unwrap_or_else(|| { std::env::var("LAMBDA_NAME").unwrap_or_else(|_| "rust_lambda_example".to_string()) })); let role_name = RoleName(format!("{}_role", lambda_name.0)); let (bucket, own_bucket) = match bucket { Some(bucket) => (Bucket(bucket), false), None => ( Bucket(std::env::var("LAMBDA_BUCKET").unwrap_or_else(|_| { format!("rust-lambda-example-{}", uuid::Uuid::new_v4()) })), true, ), }; let s3_client = aws_sdk_s3::Client::new(&sdk_config); if own_bucket { info!("Creating bucket for demo: {}", bucket.0); s3_client .create_bucket() .bucket(bucket.0.clone()) .create_bucket_configuration( CreateBucketConfiguration::builder() .location_constraint(aws_sdk_s3::types::BucketLocationConstraint::from( sdk_config.region().unwrap().as_ref(), )) .build(), ) .send() .await .unwrap(); } Self::new( aws_sdk_iam::Client::new(&sdk_config), aws_sdk_lambda::Client::new(&sdk_config), s3_client, lambda_name, role_name, bucket, OwnBucket(own_bucket), ) } /** * Upload function code from a path to a zip file. * The zip file must have an AL2 Linux-compatible binary called `bootstrap`. * The easiest way to create such a zip is to use `cargo lambda build --output-format Zip`. */ async fn prepare_function( &self, zip_file: PathBuf, key: Option<String>, ) -> Result<FunctionCode, anyhow::Error> { let body = ByteStream::from_path(zip_file).await?; let key = key.unwrap_or_else(|| format!("{}_code", self.lambda_name)); info!("Uploading function code to s3://{}/{}", self.bucket, key); let _ = self .s3_client .put_object() .bucket(self.bucket.clone()) .key(key.clone()) .body(body) .send() .await?; Ok(FunctionCode::builder() .s3_bucket(self.bucket.clone()) .s3_key(key) .build()) } /** * Create a function, uploading from a zip file. */ pub async fn create_function(&self, zip_file: PathBuf) -> Result<String, anyhow::Error> { let code = self.prepare_function(zip_file, None).await?; let key = code.s3_key().unwrap().to_string(); let role = self.create_role().await.map_err(|e| anyhow!(e))?; info!("Created iam role, waiting 15s for it to become active"); tokio::time::sleep(Duration::from_secs(15)).await; info!("Creating lambda function {}", self.lambda_name); let _ = self .lambda_client .create_function() .function_name(self.lambda_name.clone()) .code(code) .role(role.arn()) .runtime(aws_sdk_lambda::types::Runtime::Providedal2) .handler("_unused") .send() .await .map_err(anyhow::Error::from)?; self.wait_for_function_ready().await?; self.lambda_client .publish_version() .function_name(self.lambda_name.clone()) .send() .await?; Ok(key) } /** * Create an IAM execution role for the managed Lambda function. * If the role already exists, use that instead. */ async fn create_role(&self) -> Result<aws_sdk_iam::types::Role, CreateRoleError> { info!("Creating execution role for function"); let get_role = self .iam_client .get_role() .role_name(self.role_name.clone()) .send() .await; if let Ok(get_role) = get_role { if let Some(role) = get_role.role { return Ok(role); } } let create_role = self .iam_client .create_role() .role_name(self.role_name.clone()) .assume_role_policy_document(ROLE_POLICY_DOCUMENT) .send() .await; match create_role { Ok(create_role) => match create_role.role { Some(role) => Ok(role), None => Err(CreateRoleError::generic( ErrorMetadata::builder() .message("CreateRole returned empty success") .build(), )), }, Err(err) => Err(err.into_service_error()), } } /** * Poll `is_function_ready` with a 1-second delay. It returns when the function is ready or when there's an error checking the function's state. */ pub async fn wait_for_function_ready(&self) -> Result<(), anyhow::Error> { info!("Waiting for function"); while !self.is_function_ready(None).await? { info!("Function is not ready, sleeping 1s"); tokio::time::sleep(Duration::from_secs(1)).await; } Ok(()) } /** * Check if a Lambda function is ready to be invoked. * A Lambda function is ready for this scenario when its state is active and its LastUpdateStatus is Successful. * Additionally, if a sha256 is provided, the function must have that as its current code hash. * Any missing properties or failed requests will be reported as an Err. */ async fn is_function_ready( &self, expected_code_sha256: Option<&str>, ) -> Result<bool, anyhow::Error> { match self.get_function().await { Ok(func) => { if let Some(config) = func.configuration() { if let Some(state) = config.state() { info!(?state, "Checking if function is active"); if !matches!(state, State::Active) { return Ok(false); } } match config.last_update_status() { Some(last_update_status) => { info!(?last_update_status, "Checking if function is ready"); match last_update_status { LastUpdateStatus::Successful => { // continue } LastUpdateStatus::Failed | LastUpdateStatus::InProgress => { return Ok(false); } unknown => { warn!( status_variant = unknown.as_str(), "LastUpdateStatus unknown" ); return Err(anyhow!( "Unknown LastUpdateStatus, fn config is {config:?}" )); } } } None => { warn!("Missing last update status"); return Ok(false); } }; if expected_code_sha256.is_none() { return Ok(true); } if let Some(code_sha256) = config.code_sha256() { return Ok(code_sha256 == expected_code_sha256.unwrap_or_default()); } } } Err(e) => { warn!(?e, "Could not get function while waiting"); } } Ok(false) } /** 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) } /** List all Lambda functions in the current Region. */ pub async fn list_functions(&self) -> Result<ListFunctionsOutput, anyhow::Error> { info!("Listing lambda functions"); self.lambda_client .list_functions() .send() .await .map_err(anyhow::Error::from) } /** Invoke the lambda function using calculator InvokeArgs. */ pub async fn invoke(&self, args: InvokeArgs) -> Result<InvokeOutput, anyhow::Error> { info!(?args, "Invoking {}", self.lambda_name); let payload = serde_json::to_string(&args)?; debug!(?payload, "Sending payload"); self.lambda_client .invoke() .function_name(self.lambda_name.clone()) .payload(Blob::new(payload)) .send() .await .map_err(anyhow::Error::from) } /** Given a Path to a zip file, update the function's code and wait for the update to finish. */ pub async fn update_function_code( &self, zip_file: PathBuf, key: String, ) -> Result<UpdateFunctionCodeOutput, anyhow::Error> { let function_code = self.prepare_function(zip_file, Some(key)).await?; info!("Updating code for {}", self.lambda_name); let update = self .lambda_client .update_function_code() .function_name(self.lambda_name.clone()) .s3_bucket(self.bucket.clone()) .s3_key(function_code.s3_key().unwrap().to_string()) .send() .await .map_err(anyhow::Error::from)?; self.wait_for_function_ready().await?; Ok(update) } /** Update the environment for a function. */ pub async fn update_function_configuration( &self, environment: Environment, ) -> Result<UpdateFunctionConfigurationOutput, anyhow::Error> { info!( ?environment, "Updating environment for {}", self.lambda_name ); let updated = self .lambda_client .update_function_configuration() .function_name(self.lambda_name.clone()) .environment(environment) .send() .await .map_err(anyhow::Error::from)?; self.wait_for_function_ready().await?; Ok(updated) } /** 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) } pub async fn cleanup( &self, location: Option<String>, ) -> ( ( Result<DeleteFunctionOutput, anyhow::Error>, Result<DeleteRoleOutput, anyhow::Error>, Option<Result<DeleteObjectOutput, anyhow::Error>>, ), Option<Result<DeleteBucketOutput, anyhow::Error>>, ) { let delete_function = self.delete_function(location).await; let delete_bucket = if self.own_bucket { info!("Deleting bucket {}", self.bucket); if delete_function.2.is_none() || delete_function.2.as_ref().unwrap().is_ok() { Some( self.s3_client .delete_bucket() .bucket(self.bucket.clone()) .send() .await .map_err(anyhow::Error::from), ) } else { None } } else { info!("No bucket to clean up"); None }; (delete_function, delete_bucket) } } /** * Testing occurs primarily as an integration test running the `scenario` bin successfully. * Each action relies deeply on the internal workings and state of Amazon Simple Storage Service (Amazon S3), Lambda, and IAM working together. * It is therefore infeasible to mock the clients to test the individual actions. */ #[cfg(test)] mod test { use super::{InvokeArgs, Operation}; use serde_json::json; /** Make sure that the JSON output of serializing InvokeArgs is what's expected by the calculator. */ #[test] fn test_serialize() { assert_eq!(json!(InvokeArgs::Increment(5)), 5); assert_eq!( json!(InvokeArgs::Arithmetic(Operation::Plus, 5, 7)).to_string(), r#"{"op":"plus","i":5,"j":7}"#.to_string(), ); } }
Un binario para ejecutar el escenario de principio a fin, con indicadores de líneas de comando para controlar algunos comportamientos. Este archivo es el src/bin/scenario archivo.rs de la caja.
/* ## Service actions Service actions wrap the SDK call, taking a client and any specific parameters necessary for the call. * CreateFunction * GetFunction * ListFunctions * Invoke * UpdateFunctionCode * UpdateFunctionConfiguration * DeleteFunction ## Scenario A scenario runs at a command prompt and prints output to the user on the result of each service action. A scenario can run in one of two ways: straight through, printing out progress as it goes, or as an interactive question/answer script. ## Getting started with functions Use an SDK to manage AWS Lambda functions: create a function, invoke it, update its code, invoke it again, view its output and logs, and delete it. This scenario uses two Lambda handlers: _Note: Handlers don't use AWS SDK API calls._ The increment handler is straightforward: 1. It accepts a number, increments it, and returns the new value. 2. It performs simple logging of the result. The arithmetic handler is more complex: 1. It accepts a set of actions ['plus', 'minus', 'times', 'divided-by'] and two numbers, and returns the result of the calculation. 2. It uses an environment variable to control log level (such as DEBUG, INFO, WARNING, ERROR). It logs a few things at different levels, such as: * DEBUG: Full event data. * INFO: The calculation result. * WARN~ING~: When a divide by zero error occurs. * This will be the typical `RUST_LOG` variable. The steps of the scenario are: 1. Create an AWS Identity and Access Management (IAM) role that meets the following requirements: * Has an assume_role policy that grants 'lambda.amazonaws.com' the 'sts:AssumeRole' action. * Attaches the 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' managed role. * _You must wait for ~10 seconds after the role is created before you can use it!_ 2. Create a function (CreateFunction) for the increment handler by packaging it as a zip and doing one of the following: * Adding it with CreateFunction Code.ZipFile. * --or-- * Uploading it to Amazon Simple Storage Service (Amazon S3) and adding it with CreateFunction Code.S3Bucket/S3Key. * _Note: Zipping the file does not have to be done in code._ * If you have a waiter, use it to wait until the function is active. Otherwise, call GetFunction until State is Active. 3. Invoke the function with a number and print the result. 4. Update the function (UpdateFunctionCode) to the arithmetic handler by packaging it as a zip and doing one of the following: * Adding it with UpdateFunctionCode ZipFile. * --or-- * Uploading it to Amazon S3 and adding it with UpdateFunctionCode S3Bucket/S3Key. 5. Call GetFunction until Configuration.LastUpdateStatus is 'Successful' (or 'Failed'). 6. Update the environment variable by calling UpdateFunctionConfiguration and pass it a log level, such as: * Environment={'Variables': {'RUST_LOG': 'TRACE'}} 7. Invoke the function with an action from the list and a couple of values. Include LogType='Tail' to get logs in the result. Print the result of the calculation and the log. 8. [Optional] Invoke the function to provoke a divide-by-zero error and show the log result. 9. List all functions for the account, using pagination (ListFunctions). 10. Delete the function (DeleteFunction). 11. Delete the role. Each step should use the function created in Service Actions to abstract calling the SDK. */ use aws_sdk_lambda::{operation::invoke::InvokeOutput, types::Environment}; use clap::Parser; use std::{collections::HashMap, path::PathBuf}; use tracing::{debug, info, warn}; use tracing_subscriber::EnvFilter; use lambda_code_examples::actions::{ InvokeArgs::{Arithmetic, Increment}, LambdaManager, Operation, }; #[derive(Debug, Parser)] pub struct Opt { /// The AWS Region. #[structopt(short, long)] pub region: Option<String>, // The bucket to use for the FunctionCode. #[structopt(short, long)] pub bucket: Option<String>, // The name of the Lambda function. #[structopt(short, long)] pub lambda_name: Option<String>, // The number to increment. #[structopt(short, long, default_value = "12")] pub inc: i32, // The left operand. #[structopt(long, default_value = "19")] pub num_a: i32, // The right operand. #[structopt(long, default_value = "23")] pub num_b: i32, // The arithmetic operation. #[structopt(short, long, default_value = "plus")] pub operation: Operation, #[structopt(long)] pub cleanup: Option<bool>, #[structopt(long)] pub no_cleanup: Option<bool>, } fn code_path(lambda: &str) -> PathBuf { PathBuf::from(format!("../target/lambda/{lambda}/bootstrap.zip")) } fn log_invoke_output(invoke: &InvokeOutput, message: &str) { if let Some(payload) = invoke.payload().cloned() { let payload = String::from_utf8(payload.into_inner()); info!(?payload, message); } else { info!("Could not extract payload") } if let Some(logs) = invoke.log_result() { debug!(?logs, "Invoked function logs") } else { debug!("Invoked function had no logs") } } async fn main_block( opt: &Opt, manager: &LambdaManager, code_location: String, ) -> Result<(), anyhow::Error> { let invoke = manager.invoke(Increment(opt.inc)).await?; log_invoke_output(&invoke, "Invoked function configured as increment"); let update_code = manager .update_function_code(code_path("arithmetic"), code_location.clone()) .await?; let code_sha256 = update_code.code_sha256().unwrap_or("Unknown SHA"); info!(?code_sha256, "Updated function code with arithmetic.zip"); let arithmetic_args = Arithmetic(opt.operation, opt.num_a, opt.num_b); let invoke = manager.invoke(arithmetic_args).await?; log_invoke_output(&invoke, "Invoked function configured as arithmetic"); let update = manager .update_function_configuration( Environment::builder() .set_variables(Some(HashMap::from([( "RUST_LOG".to_string(), "trace".to_string(), )]))) .build(), ) .await?; let updated_environment = update.environment(); info!(?updated_environment, "Updated function configuration"); let invoke = manager .invoke(Arithmetic(opt.operation, opt.num_a, opt.num_b)) .await?; log_invoke_output( &invoke, "Invoked function configured as arithmetic with increased logging", ); let invoke = manager .invoke(Arithmetic(Operation::DividedBy, opt.num_a, 0)) .await?; log_invoke_output( &invoke, "Invoked function configured as arithmetic with divide by zero", ); Ok::<(), anyhow::Error>(()) } #[tokio::main] async fn main() { tracing_subscriber::fmt() .without_time() .with_file(true) .with_line_number(true) .with_env_filter(EnvFilter::from_default_env()) .init(); let opt = Opt::parse(); let manager = LambdaManager::load_from_env(opt.lambda_name.clone(), opt.bucket.clone()).await; let key = match manager.create_function(code_path("increment")).await { Ok(init) => { info!(?init, "Created function, initially with increment.zip"); let run_block = main_block(&opt, &manager, init.clone()).await; info!(?run_block, "Finished running example, cleaning up"); Some(init) } Err(err) => { warn!(?err, "Error happened when initializing function"); None } }; if Some(false) == opt.cleanup || Some(true) == opt.no_cleanup { info!("Skipping cleanup") } else { let delete = manager.cleanup(key).await; info!(?delete, "Deleted function & cleaned up resources"); } }
-
Para API obtener más información, consulte los siguientes temas en AWS SDK la sección de referencia sobre Rust. API
-
- SAP ABAP
-
- SDKpara SAP ABAP
-
nota
Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. TRY. "Create an AWS Identity and Access Management (IAM) role that grants AWS Lambda permission to write to logs." DATA(lv_policy_document) = `{` && `"Version":"2012-10-17",` && `"Statement": [` && `{` && `"Effect": "Allow",` && `"Action": [` && `"sts:AssumeRole"` && `],` && `"Principal": {` && `"Service": [` && `"lambda.amazonaws.com"` && `]` && `}` && `}` && `]` && `}`. TRY. DATA(lo_create_role_output) = lo_iam->createrole( iv_rolename = iv_role_name iv_assumerolepolicydocument = lv_policy_document iv_description = 'Grant lambda permission to write to logs' ). MESSAGE 'IAM role created.' TYPE 'I'. WAIT UP TO 10 SECONDS. " Make sure that the IAM role is ready for use. " CATCH /aws1/cx_iamentityalrdyexex. MESSAGE 'IAM role already exists.' TYPE 'E'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iammalformedplydocex. MESSAGE 'Policy document in the request is malformed.' TYPE 'E'. ENDTRY. TRY. lo_iam->attachrolepolicy( iv_rolename = iv_role_name iv_policyarn = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ). MESSAGE 'Attached policy to the IAM role.' TYPE 'I'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamplynotattachableex. MESSAGE 'Service role policies can only be attached to the service-linked role for their service.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. " Create a Lambda function and upload handler code. " " Lambda function performs 'increment' action on a number. " TRY. lo_lmd->createfunction( iv_functionname = iv_function_name iv_runtime = `python3.9` iv_role = lo_create_role_output->get_role( )->get_arn( ) iv_handler = iv_handler io_code = io_initial_zip_file iv_description = 'AWS Lambda code example' ). MESSAGE 'Lambda function created.' TYPE 'I'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. " Verify the function is in Active state " WHILE lo_lmd->getfunction( iv_functionname = iv_function_name )->get_configuration( )->ask_state( ) <> 'Active'. IF sy-index = 10. EXIT. " Maximum 10 seconds. " ENDIF. WAIT UP TO 1 SECONDS. ENDWHILE. "Invoke the function with a single parameter and get results." TRY. DATA(lv_json) = /aws1/cl_rt_util=>string_to_xstring( `{` && `"action": "increment",` && `"number": 10` && `}` ). DATA(lo_initial_invoke_output) = lo_lmd->invoke( iv_functionname = iv_function_name iv_payload = lv_json ). ov_initial_invoke_payload = lo_initial_invoke_output->get_payload( ). " ov_initial_invoke_payload is returned for testing purposes. " DATA(lo_writer_json) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). CALL TRANSFORMATION id SOURCE XML ov_initial_invoke_payload RESULT XML lo_writer_json. DATA(lv_result) = cl_abap_codepage=>convert_from( lo_writer_json->get_output( ) ). MESSAGE 'Lambda function invoked.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdinvrequestcontex. MESSAGE 'Unable to parse request body as JSON.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdunsuppedmediatyp00. MESSAGE 'Invoke request body does not have JSON as its content type.' TYPE 'E'. ENDTRY. " Update the function code and configure its Lambda environment with an environment variable. " " Lambda function is updated to perform 'decrement' action also. " TRY. lo_lmd->updatefunctioncode( iv_functionname = iv_function_name iv_zipfile = io_updated_zip_file ). WAIT UP TO 10 SECONDS. " Make sure that the update is completed. " MESSAGE 'Lambda function code updated.' TYPE 'I'. CATCH /aws1/cx_lmdcodestorageexcdex. MESSAGE 'Maximum total code size per account exceeded.' TYPE 'E'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. TRY. DATA lt_variables TYPE /aws1/cl_lmdenvironmentvaria00=>tt_environmentvariables. DATA ls_variable LIKE LINE OF lt_variables. ls_variable-key = 'LOG_LEVEL'. ls_variable-value = NEW /aws1/cl_lmdenvironmentvaria00( iv_value = 'info' ). INSERT ls_variable INTO TABLE lt_variables. lo_lmd->updatefunctionconfiguration( iv_functionname = iv_function_name io_environment = NEW /aws1/cl_lmdenvironment( it_variables = lt_variables ) ). WAIT UP TO 10 SECONDS. " Make sure that the update is completed. " MESSAGE 'Lambda function configuration/settings updated.' 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'. ENDTRY. "Invoke the function with new parameters and get results. Display the execution log that's returned from the invocation." TRY. lv_json = /aws1/cl_rt_util=>string_to_xstring( `{` && `"action": "decrement",` && `"number": 10` && `}` ). DATA(lo_updated_invoke_output) = lo_lmd->invoke( iv_functionname = iv_function_name iv_payload = lv_json ). ov_updated_invoke_payload = lo_updated_invoke_output->get_payload( ). " ov_updated_invoke_payload is returned for testing purposes. " lo_writer_json = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ). CALL TRANSFORMATION id SOURCE XML ov_updated_invoke_payload RESULT XML lo_writer_json. lv_result = cl_abap_codepage=>convert_from( lo_writer_json->get_output( ) ). MESSAGE 'Lambda function invoked.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_lmdinvrequestcontex. MESSAGE 'Unable to parse request body as JSON.' TYPE 'E'. CATCH /aws1/cx_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. CATCH /aws1/cx_lmdunsuppedmediatyp00. MESSAGE 'Invoke request body does not have JSON as its content type.' TYPE 'E'. ENDTRY. " List the functions for your account. " TRY. DATA(lo_list_output) = lo_lmd->listfunctions( ). DATA(lt_functions) = lo_list_output->get_functions( ). MESSAGE 'Retrieved list of Lambda functions.' TYPE 'I'. CATCH /aws1/cx_lmdinvparamvalueex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. ENDTRY. " Delete the Lambda function. " 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_lmdresourcenotfoundex. MESSAGE 'The requested resource does not exist.' TYPE 'E'. ENDTRY. " Detach role policy. " TRY. lo_iam->detachrolepolicy( iv_rolename = iv_role_name iv_policyarn = 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole' ). MESSAGE 'Detached policy from the IAM role.' TYPE 'I'. CATCH /aws1/cx_iaminvalidinputex. MESSAGE 'The request contains a non-valid parameter.' TYPE 'E'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamplynotattachableex. MESSAGE 'Service role policies can only be attached to the service-linked role for their service.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. " Delete the IAM role. " TRY. lo_iam->deleterole( iv_rolename = iv_role_name ). MESSAGE 'IAM role deleted.' TYPE 'I'. CATCH /aws1/cx_iamnosuchentityex. MESSAGE 'The requested resource entity does not exist.' TYPE 'E'. CATCH /aws1/cx_iamunmodableentityex. MESSAGE 'Service that depends on the service-linked role is not modifiable.' TYPE 'E'. ENDTRY. CATCH /aws1/cx_rt_service_generic INTO lo_exception. DATA(lv_error) = lo_exception->get_longtext( ). MESSAGE lv_error TYPE 'E'. ENDTRY.
-
Para API obtener más información, consulta los siguientes temas SAPABAPAPIcomo referencia.AWS SDK
-