static async Task <(string functionArn, string functionName, Func <Task> functionCleanup)> CreateFunction( string roleArn, IAmazonLambda lambda, LambdaCreationOptions options) { var functionName = $"Beetles_{options.Name}_{options.Timestamp}"; var retryWait = Stopwatch.StartNew(); CreateFunctionResponse function = null; do { try { function = await lambda.CreateFunctionAsync(new CreateFunctionRequest { Code = new FunctionCode { ZipFile = new MemoryStream(File.ReadAllBytes(options.PackagePath)) }, Description = $"Beetles Load Test {options.Name}", FunctionName = functionName, Handler = options.LambdaHandlerName, MemorySize = options.MemorySize, Publish = true, Role = roleArn, Runtime = "dotnetcore2.1", Timeout = 120, Environment = new Environment { Variables = { [Constants.ConfigurationTypeNameKey] = options.SettingsTypeName } }, VpcConfig = new VpcConfig() }, options.CancellationToken); // await lambda.PutFunctionConcurrencyAsync(new PutFunctionConcurrencyRequest() // { // FunctionName = functionName, // ReservedConcurrentExecutions = options.ProvisionedConcurrency // }, options.CancellationToken); } catch (InvalidParameterValueException) when(retryWait.Elapsed < TimeSpan.FromMinutes(1)) { } } while (function == null); var functionArn = function.FunctionArn; return(functionArn, functionName, async() => await lambda.DeleteFunctionAsync(functionName)); }
protected async Task CreateFunctionAsync(IAmazonLambda lambdaClient, string bucketName) { await DeleteFunctionIfExistsAsync(lambdaClient); var createRequest = new CreateFunctionRequest { FunctionName = FunctionName, Code = new FunctionCode { S3Bucket = bucketName, S3Key = DeploymentZipKey }, Handler = "PingAsync", MemorySize = 512, Timeout = 30, Runtime = Runtime.ProvidedAl2, Role = ExecutionRoleArn }; var startTime = DateTime.Now; var created = false; while (DateTime.Now < startTime.AddSeconds(30)) { try { await lambdaClient.CreateFunctionAsync(createRequest); created = true; break; } catch (InvalidParameterValueException ipve) { // Wait for the role to be fully propagated through AWS if (ipve.Message == "The role defined for the function cannot be assumed by Lambda.") { await Task.Delay(2000); } else { throw; } } } await Task.Delay(5000); if (!created) { throw new Exception($"Timed out trying to create Lambda function {FunctionName}"); } }
private Amazon.Lambda.Model.CreateFunctionResponse CallAWSServiceOperation(IAmazonLambda client, Amazon.Lambda.Model.CreateFunctionRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Lambda", "CreateFunction"); try { #if DESKTOP return(client.CreateFunction(request)); #elif CORECLR return(client.CreateFunctionAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }