public async Task ExecuteAsync()

in Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/Runtime/LambdaExecutor.cs [19:104]


        public async Task<ExecutionResponse> ExecuteAsync(ExecutionRequest request)
        {
            var logger = new LocalLambdaLogger();
            var response = new ExecutionResponse();

            if (!string.IsNullOrEmpty(request.Function.ErrorMessage))
            {
                response.Error = request.Function.ErrorMessage;
                return response;
            }

            try
            {
                if (!string.IsNullOrEmpty(request.AWSRegion))
                {
                    Environment.SetEnvironmentVariable("AWS_REGION", request.AWSRegion);
                }
                if (!string.IsNullOrEmpty(request.AWSProfile))
                {
                    Environment.SetEnvironmentVariable("AWS_PROFILE", request.AWSProfile);
                }

                // Set the Lambda environment variable for the function name. Some libraries like
                // our Amazon.Lambda.AspNetCoreServer.Hosting use this environment variable to
                // tell if they are running in Lambda and if so activate. Since we are emulating
                // Lambda we want those libraries to activate.
                Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", request.Function.FunctionInfo.Name);

                // If Environment variables were defined for the function
                // then set them for the process to the emulated function picks up the variables.
                foreach (var kvp in request.Function.FunctionInfo.EnvironmentVariables)
                {
                    Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
                }

                var context = new LocalLambdaContext()
                {
                    Logger = logger,
                    AwsRequestId = Guid.NewGuid().ToString(),
                    FunctionName = request.Function.FunctionInfo.Name,
                    InvokedFunctionArn = string.Format("arn:aws:lambda:{0}::function:{1}", request.AWSRegion, request.Function.FunctionInfo.Name)
                };

                object instance = null;
                if (!request.Function.LambdaMethod.IsStatic)
                {
                    instance = Activator.CreateInstance(request.Function.LambdaType);
                }

                var parameters = BuildParameters(request, context);

                // Because a Lambda compute environment never executes more then one event at a time
                // create a lock around the execution to match that environment.
                await executeSlim.WaitAsync();
                try
                {
                    using (var wrapper = new ConsoleOutWrapper(logger))
                    {
                        var lambdaReturnObject = request.Function.LambdaMethod.Invoke(instance, parameters);
                        response.Response = await ProcessReturnAsync(request, lambdaReturnObject);
                    }
                }
                finally
                {
                    // To avoid side effects remove the environment variables that were set specifically 
                    // for running the lambda function.
                    foreach (var kvp in request.Function.FunctionInfo.EnvironmentVariables)
                    {
                        Environment.SetEnvironmentVariable(kvp.Key, null);
                    }
                    executeSlim.Release();
                }
            }
            catch (TargetInvocationException e)
            {
                response.Error = GenerateErrorMessage(e.InnerException);
            }
            catch (Exception e)
            {
                response.Error = GenerateErrorMessage(e);
            }

            response.Logs = logger.Buffer;

            return response;
        }