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)); }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) { CreateFunctionResponse response = new CreateFunctionResponse(); UnmarshallResult(context, response); if (context.ResponseData.IsHeaderPresent("ETag")) { response.ETag = context.ResponseData.GetHeaderValue("ETag"); } if (context.ResponseData.IsHeaderPresent("Location")) { response.Location = context.ResponseData.GetHeaderValue("Location"); } return(response); }
/// <summary> /// 创建函数 /// </summary> public static void CreateFunction(FunctionGraphClient client) { CreateFunctionRequest req = new CreateFunctionRequest { Body = new CreateFunctionRequestBody { FuncName = "csharpSdkTest", Handler = "index.handler", MemorySize = 128, Timeout = 3, Runtime = CreateFunctionRequestBody.RuntimeEnum.NODE_JS6_10, Package = "CsharpSdkTest", CodeType = CreateFunctionRequestBody.CodeTypeEnum.INLINE, CodeFilename = "index.zip", FuncCode = new FuncCode { File = "UEsDBAoAAAAAAHYLfU2fZFKzsAAAALAAAAAIAAAAaW5kZXguanNleHBvcnRzLmhhbmRsZXIgPSBmdW5jdGlvbiAoZXZlbnQsIGNvbnRleHQsIGNhbGxiYWNrKSB7DQogICAgY29uc3QgZXJyb3IgPSBudWxsOw0KICAgIGNvbnN0IG91dHB1dCA9IGBIZWxsbyBtZXNzYWdlOiAke0pTT04uc3RyaW5naWZ5KGV2ZW50KX1gOw0KICAgIGNhbGxiYWNrKGVycm9yLCBvdXRwdXQpOw0KfVBLAQIeAwoAAAAAAHYLfU2fZFKzsAAAALAAAAAIAAAAAAAAAAAAAAC0gQAAAABpbmRleC5qc1BLBQYAAAAAAQABADYAAADWAAAAAAA=" } } }; try { CreateFunctionResponse resp = client.CreateFunction(req); Console.WriteLine("Create Function Body=" + JsonConvert.SerializeObject(resp)); Console.WriteLine("func_name=" + resp.FuncName); Console.WriteLine("func_urn=" + resp.FuncUrn); Console.WriteLine("Create Function StatusCode=" + resp.HttpStatusCode); } catch (ClientRequestException e) { Console.WriteLine(e.HttpStatusCode); Console.WriteLine(e.ErrorCode); Console.WriteLine(e.ErrorMsg); } catch (ConnectionException e) { Console.WriteLine(e.ErrorMessage); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { CreateFunctionResponse response = new CreateFunctionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("functionConfiguration", targetDepth)) { var unmarshaller = FunctionConfigurationUnmarshaller.Instance; response.FunctionConfiguration = unmarshaller.Unmarshall(context); continue; } } return(response); }
private static void UnmarshallResult(XmlUnmarshallerContext context, CreateFunctionResponse response) { int originalDepth = context.CurrentDepth; int targetDepth = originalDepth + 1; while (context.Read()) { if (context.IsStartElement || context.IsAttribute) { if (context.TestExpression("FunctionSummary", targetDepth)) { var unmarshaller = FunctionSummaryUnmarshaller.Instance; response.FunctionSummary = unmarshaller.Unmarshall(context); continue; } } else if (context.IsEndElement && context.CurrentDepth < originalDepth) { return; } } return; }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { CreateFunctionResponse response = new CreateFunctionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("CodeSha256", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.CodeSha256 = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("CodeSize", targetDepth)) { var unmarshaller = LongUnmarshaller.Instance; response.CodeSize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("DeadLetterConfig", targetDepth)) { var unmarshaller = DeadLetterConfigUnmarshaller.Instance; response.DeadLetterConfig = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Description", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Description = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Environment", targetDepth)) { var unmarshaller = EnvironmentResponseUnmarshaller.Instance; response.Environment = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionName", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionName = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Handler", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Handler = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("KMSKeyArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.KMSKeyArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastModified", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastModified = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("MemorySize", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.MemorySize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Role", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Role = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Runtime", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Runtime = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Timeout", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.Timeout = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("TracingConfig", targetDepth)) { var unmarshaller = TracingConfigResponseUnmarshaller.Instance; response.TracingConfig = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Version", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Version = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("VpcConfig", targetDepth)) { var unmarshaller = VpcConfigDetailUnmarshaller.Instance; response.VpcConfig = unmarshaller.Unmarshall(context); continue; } } return(response); }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { CreateFunctionResponse response = new CreateFunctionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("CodeSize", targetDepth)) { var unmarshaller = LongUnmarshaller.Instance; response.CodeSize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Description", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Description = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionName", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionName = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Handler", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Handler = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastModified", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastModified = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("MemorySize", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.MemorySize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Role", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Role = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Runtime", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Runtime = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Timeout", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.Timeout = unmarshaller.Unmarshall(context); continue; } } return(response); }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { CreateFunctionResponse response = new CreateFunctionResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("CodeSha256", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.CodeSha256 = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("CodeSize", targetDepth)) { var unmarshaller = LongUnmarshaller.Instance; response.CodeSize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("DeadLetterConfig", targetDepth)) { var unmarshaller = DeadLetterConfigUnmarshaller.Instance; response.DeadLetterConfig = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Description", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Description = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Environment", targetDepth)) { var unmarshaller = EnvironmentResponseUnmarshaller.Instance; response.Environment = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FileSystemConfigs", targetDepth)) { var unmarshaller = new ListUnmarshaller <FileSystemConfig, FileSystemConfigUnmarshaller>(FileSystemConfigUnmarshaller.Instance); response.FileSystemConfigs = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("FunctionName", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.FunctionName = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Handler", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Handler = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("ImageConfigResponse", targetDepth)) { var unmarshaller = ImageConfigResponseUnmarshaller.Instance; response.ImageConfigResponse = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("KMSKeyArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.KMSKeyArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastModified", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastModified = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastUpdateStatus", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastUpdateStatus = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastUpdateStatusReason", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastUpdateStatusReason = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("LastUpdateStatusReasonCode", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.LastUpdateStatusReasonCode = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Layers", targetDepth)) { var unmarshaller = new ListUnmarshaller <Layer, LayerUnmarshaller>(LayerUnmarshaller.Instance); response.Layers = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("MasterArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.MasterArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("MemorySize", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.MemorySize = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("PackageType", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.PackageType = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("RevisionId", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.RevisionId = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Role", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Role = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Runtime", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Runtime = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("SigningJobArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.SigningJobArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("SigningProfileVersionArn", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.SigningProfileVersionArn = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("State", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.State = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("StateReason", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.StateReason = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("StateReasonCode", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.StateReasonCode = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Timeout", targetDepth)) { var unmarshaller = IntUnmarshaller.Instance; response.Timeout = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("TracingConfig", targetDepth)) { var unmarshaller = TracingConfigResponseUnmarshaller.Instance; response.TracingConfig = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("Version", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Version = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("VpcConfig", targetDepth)) { var unmarshaller = VpcConfigDetailUnmarshaller.Instance; response.VpcConfig = unmarshaller.Unmarshall(context); continue; } } return(response); }
/// <summary> /// Updates the AWS Lambda functions code. /// </summary> /// <param name="functionName">The name of an AWS Lambda function.</param> /// <param name="settings">The <see cref="UpdateFunctionCodeSettings"/> used during the request to AWS.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> public async Task <string> CreateFunction(string functionName, CreateFunctionSettings settings, CancellationToken cancellationToken = default(CancellationToken)) { if (String.IsNullOrEmpty(functionName)) { throw new ArgumentNullException(nameof(functionName)); } // Create Request AmazonLambdaClient client = this.CreateClient(settings); CreateFunctionRequest request = new CreateFunctionRequest() { FunctionName = functionName, Handler = settings.Handler, Runtime = Runtime.FindValue(settings.Runtime), Role = settings.Role, Environment = new Amazon.Lambda.Model.Environment() { Variables = settings.Environment }, Timeout = settings.Timeout, MemorySize = settings.MemorySize, Publish = settings.Publish, KMSKeyArn = settings.KMSKeyArn, Tags = settings.Tags, Description = settings.Description, Code = new FunctionCode() { S3Bucket = settings.S3Bucket, S3Key = settings.S3Key, S3ObjectVersion = settings.S3Version, } }; if (!String.IsNullOrEmpty(settings.DeadLetterConfig)) { request.DeadLetterConfig.TargetArn = settings.DeadLetterConfig; } if (settings.ZipPath != null) { request.Code.ZipFile = this.GetFileStream(settings.ZipPath, settings); } if (settings.VpcSecurityGroupIds.Count > 0) { request.VpcConfig.SecurityGroupIds = settings.VpcSecurityGroupIds; } if (settings.VpcSubnetIds.Count > 0) { request.VpcConfig.SubnetIds = settings.VpcSubnetIds; } // Check Response CreateFunctionResponse response = await client.CreateFunctionAsync(request, cancellationToken); if (response.HttpStatusCode == HttpStatusCode.OK) { _Log.Verbose("Successfully updated function '{0}'", functionName); return(response.Version); } else { _Log.Error("Failed to update function '{0}'", functionName); return(""); } }