/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context) { var client = new AmazonStepFunctionsClient(); var sfnArn = Environment.GetEnvironmentVariable("STEP_FUNCTION_ARN"); LambdaLogger.Log($"Processing {dynamoEvent.Records.Count()}"); LambdaLogger.Log($"ARN {sfnArn}"); IList <DynamoDBEvent.DynamodbStreamRecord> r = dynamoEvent.Records; foreach (DynamoDBEvent.DynamodbStreamRecord record in dynamoEvent.Records) { if (record.EventName == OperationType.INSERT) { var incoming = record.Dynamodb.NewImage.ToDynamoEntity <PurchaseOrder>(); LambdaLogger.Log($"ID {incoming.PurchaseId}"); LambdaLogger.Log($"Color {incoming.ColorName}"); var request = new StartExecutionRequest() { Input = JsonSerializer.Serialize(new { PurchasedInventory = new object[] { incoming } }), Name = Guid.NewGuid().ToString(), StateMachineArn = sfnArn, }; var resp = await client.StartExecutionAsync(request); if (resp.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new Exception($"Failed with code {resp.HttpStatusCode}: {JsonSerializer.Serialize(resp.ResponseMetadata)}"); } } } }
public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems) { AmazonStepFunctionsConfig config = new AmazonStepFunctionsConfig(); config.RegionEndpoint = region; ConfigureClient(config); AmazonStepFunctionsClient client = new AmazonStepFunctionsClient(creds, config); ListActivitiesResponse resp = new ListActivitiesResponse(); do { ListActivitiesRequest req = new ListActivitiesRequest { NextToken = resp.NextToken , MaxResults = maxItems }; resp = client.ListActivities(req); CheckError(resp.HttpStatusCode, "200"); foreach (var obj in resp.Activities) { AddObject(obj); } }while (!string.IsNullOrEmpty(resp.NextToken)); }
static void ExecuteStepFunctionUsingAssumedExistingStateMachineRole() { var options = new AWSOptions() { Profile = "default", Region = RegionEndpoint.EUWest2 }; var assumedRoleResponse = ManualAssume(options).ConfigureAwait(false).GetAwaiter().GetResult(); var assumedCredentials = assumedRoleResponse.Credentials; var amazonStepFunctionsConfig = new AmazonStepFunctionsConfig { RegionEndpoint = RegionEndpoint.EUWest2 }; using (var amazonStepFunctionsClient = new AmazonStepFunctionsClient( assumedCredentials.AccessKeyId, assumedCredentials.SecretAccessKey, amazonStepFunctionsConfig)) { var state = new State { Name = "MyStepFunctions" }; var jsonData1 = JsonConvert.SerializeObject(state); var startExecutionRequest = new StartExecutionRequest { Input = jsonData1, Name = $"SchedulingEngine_{Guid.NewGuid().ToString("N")}", StateMachineArn = "arn:aws:states:eu-west-2:464534050515:stateMachine:StateMachine-z8hrOwmL9CiG" }; var taskStartExecutionResponse = amazonStepFunctionsClient.StartExecutionAsync(startExecutionRequest).ConfigureAwait(false).GetAwaiter().GetResult(); } Console.ReadLine(); }
private async Task SendSuccessAsync() { AmazonStepFunctionsClient client = new AmazonStepFunctionsClient(); SendTaskSuccessRequest successRequest = new SendTaskSuccessRequest(); successRequest.TaskToken = taskToken; Dictionary <String, String> result = new Dictionary <String, String> { { "Result", "Success" }, { "Message", "Completed" } }; string requestOutput = JsonConvert.SerializeObject(result, Formatting.Indented); successRequest.Output = requestOutput; try { await client.SendTaskSuccessAsync(successRequest); } catch (Exception error) { Console.WriteLine("ERROR : SendSuccessAsync : " + error.Message); Console.WriteLine("ERROR : SendSuccessAsync : " + error.StackTrace); } await Task.CompletedTask; }
public Function() { stepFunctionsClient = new AmazonStepFunctionsClient(); dbClient = new AmazonDynamoDBClient(); emailServiceClient = new AmazonSimpleEmailServiceClient(); s3client = new AmazonS3Client(); }
public AWSStepFunctionsAPI(SessionAWSCredentials sessionAWSCredentials) { this.sessionAWSCredentials = sessionAWSCredentials; var credentials = sessionAWSCredentials.GetCredentials(); basicAWSCredentials = new BasicAWSCredentials(credentials.AccessKey, credentials.SecretKey); amazonStepFunctionsClient = new AmazonStepFunctionsClient(sessionAWSCredentials); }
public HeartbeatHostedService( AmazonStepFunctionsClient client, ILogger <HeartbeatHostedService> logger) { logger.LogDebug($"Creating HeartbeatHostedService"); _client = client; _logger = logger; }
private static async Task ProcessNotificationAsync(McmaApiRequest request, McmaApiResponse response) { Logger.Debug(nameof(ProcessNotificationAsync)); Logger.Debug(request.ToMcmaJson().ToString()); var notification = request.JsonBody.ToMcmaObject <Notification>(); if (notification == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing notification in request body"; return; } if (notification.Content == null) { response.StatusCode = (int)HttpStatusCode.BadRequest; response.StatusMessage = "Missing notification content"; return; } var job = notification.Content.ToMcmaObject <Job>(); var taskToken = request.QueryStringParameters["taskToken"]; var stepFunctionClient = new AmazonStepFunctionsClient(); switch (job.Status) { case "COMPLETED": Logger.Debug($"Sending task success for task token {taskToken}"); await stepFunctionClient.SendTaskSuccessAsync(new SendTaskSuccessRequest { TaskToken = taskToken, Output = $"\"{notification.Source}\"" }); break; case "FAILED": Logger.Debug($"Sending task failure for task token {taskToken}"); var error = job.Type + " failed execution"; var cause = job.Type + " with id '" + job.Id + "' failed execution with statusMessage '" + job.StatusMessage + "'"; await stepFunctionClient.SendTaskFailureAsync(new SendTaskFailureRequest { TaskToken = taskToken, Error = error, Cause = cause }); break; } }
public Function() { this.config = new AmazonS3StorageConfig { AccessKey = "AKIAJTXMJ7UJ47CT27GA", //Environment.GetEnvironmentVariable("accessKey"), SecretKey = "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" //Environment.GetEnvironmentVariable("secretKey"), }; this.credentials = new BasicAWSCredentials(this.config.AccessKey, this.config.SecretKey); functionsClient = new AmazonStepFunctionsClient(this.credentials, RegionEndpoint.USEast1); }
static async System.Threading.Tasks.Task Main(string[] args) { var s3Client = new AmazonS3Client(); var stepFunctionclient = new AmazonStepFunctionsClient(); var bucket = Environment.GetEnvironmentVariable("STEP_FUNCTION_DEMO_BUCKET"); var requestObjects = await s3Client.ListObjectsAsync(new ListObjectsRequest { BucketName = bucket, Prefix = "TaskRequest" }); foreach (var obj in requestObjects.S3Objects) { var s3object = await s3Client.GetObjectAsync(new GetObjectRequest { BucketName = bucket, Key = obj.Key }); using var sr = new StreamReader(s3object.ResponseStream); var data = JsonSerializer.Deserialize <JobWithTaskToken>(await sr.ReadToEndAsync()); if (data.State.Data == 10) { data.State.Resolved = true; } await s3Client.PutObjectAsync(new PutObjectRequest { BucketName = bucket, Key = $"TaskResponse/Response-{System.DateTime.Now.ToString("MMddyyyyhhmmss")}.json", ContentBody = JsonSerializer.Serialize(data) }); if (!string.IsNullOrEmpty(data.TaskToken)) { await stepFunctionclient.SendTaskSuccessAsync(new SendTaskSuccessRequest { TaskToken = data.TaskToken, Output = JsonSerializer.Serialize(data.State) }); } await s3Client.CopyObjectAsync(new CopyObjectRequest { SourceBucket = obj.BucketName, DestinationBucket = bucket, SourceKey = obj.Key, DestinationKey = obj.Key.Replace("TaskRequest", "Completed") }); await s3Client.DeleteObjectAsync(bucket, obj.Key); } }
private async Task <StartExecutionResponse> StartWorkflowInstanceAsync() { StartExecutionResponse status = null; try { LambdaLogger.Log("1. Lambda start1"); List <Quote> quotes = GetQuotesAsPerStage(2, 13);//2 RSM, 13 Submitted, LambdaLogger.Log("1. Lambda start2"); foreach (var item in quotes) { //Check if any Promotion is submitted //LambdaLogger.Log("1. Lambda start3"+ Directory.GetCurrentDirectory() + @"\iam.json"); //_CredentialProfileStoreChain = new CredentialProfileStoreChain( );//@"/iam.json" //LambdaLogger.Log("Profile Location="+_CredentialProfileStoreChain.ProfilesLocation); //AWSCredentials awsCredentials; //if (_CredentialProfileStoreChain.TryGetAWSCredentials("default", out awsCredentials))//local-test-profile //{ // if (awsCredentials != null) // { string inputToStepFunc = "{ \"quoteid\": \"" + item.QuoteID + "\", \"promodesc\" : \"" + item.TPBackground + "\" }"; var aws_access_key_id = Environment.GetEnvironmentVariable("aws_access_key_id"); var aws_secret_access_key = Environment.GetEnvironmentVariable("aws_secret_access_key"); using (AmazonStepFunctionsClient stepfunctions = new AmazonStepFunctionsClient(aws_access_key_id, aws_secret_access_key, Amazon.RegionEndpoint.USEast1)) { LambdaLogger.Log("Gocha credentials going to invoke Step function"); string nameunique = item.QuoteID + "_WF";//+"_" + DateTime.Now.ToString("ddMMyyyyHHmmssfff"); StartExecutionRequest newRequest = new StartExecutionRequest { StateMachineArn = "arn:aws:states:us-east-1:494875521123:stateMachine:ApprovalWorkflow", Input = inputToStepFunc, Name = nameunique }; LambdaLogger.Log("Step Func object is ready to be invoked."); status = await stepfunctions.StartExecutionAsync(newRequest); LambdaLogger.Log("Step Func invoke done" + status.ToString()); } //} //else // LambdaLogger.Log("Null AWS Credentilas found."); //} LambdaLogger.Log("Workflow is created for Quote id = " + item.QuoteID); } } catch (Exception ex) { LambdaLogger.Log("Error" + ex.ToString()); throw ex; } return(status); }
public async Task <NumberPlateTrigger> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; context.Logger.LogLine("EVENT Received: " + JsonConvert.SerializeObject(s3Event)); if (regExNumberPlate == null) { context.Logger.LogLine("regExNumberPlate is not yet populated. Calling getNumberPlateFromSecretsManager()..."); regExNumberPlate = await getNumberPlateFromSecretsManager(context); context.Logger.LogLine("regExNumberPlate is " + regExNumberPlate); } NumberPlateTrigger result = new NumberPlateTrigger { bucket = s3Event.Bucket.Name, key = s3Event.Object.Key, contentType = "", contentLength = s3Event.Object.Size, charge = int.Parse(Environment.GetEnvironmentVariable("TollgateCharge")), numberPlate = new NumberPlate() { numberPlateRegEx = this.regExNumberPlate, detected = false } }; AWSXRayRecorder recorder = AWSXRayRecorder.Instance; recorder.BeginSubsegment("TollGantry::Detect Number Plate in Captured Image"); recorder.AddMetadata("bucket", s3Event.Bucket.Name); recorder.AddMetadata("key", s3Event.Object.Key); recorder.AddMetadata("regex", this.regExNumberPlate); // // TODO: Call Rekognition to detect text in the captured image // recorder.EndSubsegment(); // // Kick off the step function // context.Logger.LogLine("Starting the state machine"); IAmazonStepFunctions stepFunctionsClient = new AmazonStepFunctionsClient(); await stepFunctionsClient.StartExecutionAsync(new StartExecutionRequest() { StateMachineArn = Environment.GetEnvironmentVariable("NumberPlateProcessStateMachine"), Input = JsonConvert.SerializeObject(result) }); context.Logger.LogLine("State machine started"); return(result); }
private static async Task StartAndMonitorExecution() { string executionName = Guid.NewGuid().ToString(); using (var client = new AmazonStepFunctionsClient(RegionEndpoint.SAEast1)) { // manda executar o Step Function var payload = new SimpleStepFunctionInput { Name = "pirilampo", Number = _rnd.Next() }; var request = new StartExecutionRequest { Input = JsonConvert.SerializeObject(payload, Formatting.None, _jsonSerializerSettings), Name = executionName, StateMachineArn = _stateMachineArn }; Console.WriteLine("Sending:"); Console.WriteLine(request.Input); var response = await client.StartExecutionAsync(request).ConfigureAwait(false); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine($"Err: {response.HttpStatusCode}"); return; } // verifica o estado da execução var requestStatus = new DescribeExecutionRequest { ExecutionArn = $"{_executionArn}:{executionName}", }; while (true) { var responseStatus = await client.DescribeExecutionAsync(requestStatus).ConfigureAwait(false); if (responseStatus.HttpStatusCode == System.Net.HttpStatusCode.OK && responseStatus.Status == ExecutionStatus.SUCCEEDED) { dynamic parsed = JsonConvert.DeserializeObject(responseStatus.Output); Console.WriteLine(JsonConvert.SerializeObject(parsed, Formatting.Indented, _jsonSerializerSettings)); break; } Console.WriteLine($"{responseStatus.Status}..."); await Task.Delay(1500).ConfigureAwait(false); } } }
protected IAmazonStepFunctions CreateClient(AWSCredentials credentials, RegionEndpoint region) { var config = new AmazonStepFunctionsConfig { RegionEndpoint = region }; Amazon.PowerShell.Utils.Common.PopulateConfig(this, config); this.CustomizeClientConfig(config); var client = new AmazonStepFunctionsClient(credentials, config); client.BeforeRequestEvent += RequestEventHandler; client.AfterResponseEvent += ResponseEventHandler; return(client); }
internal WorkerHostedService(IWorker worker, IHeartbeatManager heartbeatManager, StepFunctionsConfig config, AmazonStepFunctionsClient client, ILoggerFactory loggerFactory) { _heartbeatManager = heartbeatManager; _logger = loggerFactory.CreateLogger <WorkerHostedService>(); LoggerFactory = loggerFactory; Client = client; Worker = worker; Config = config; _logger.LogDebug($"Creating TaskWorkerHostedService: '{worker.ActivityName}'"); }
public async Task ExecuteAsync(WorkerJobHelper <WorkflowJob> job) { using (var stepFunctionClient = new AmazonStepFunctionsClient()) await stepFunctionClient.StartExecutionAsync( new StartExecutionRequest { Input = new { Input = job.JobInput, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = job.JobAssignmentId + "/notifications" } }.ToMcmaJson().ToString(), StateMachineArn = job.Request.GetRequiredContextVariable($"{job.Profile.Name}Id") }); }
private static async Task ProcessNotificationAsync(McmaApiRequestContext requestContext) { Logger.Debug(nameof(ProcessNotificationAsync)); Logger.Debug(requestContext.Request.ToMcmaJson().ToString()); if (requestContext.IsBadRequestDueToMissingBody(out Notification notification)) { return; } if (notification.Content == null) { requestContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; requestContext.Response.StatusMessage = "Missing notification content"; return; } var job = notification.Content.ToMcmaObject <Job>(); var taskToken = requestContext.Request.QueryStringParameters["taskToken"]; if (job.Status == JobStatus.Completed) { using (var stepFunctionClient = new AmazonStepFunctionsClient()) await stepFunctionClient.SendTaskSuccessAsync(new SendTaskSuccessRequest { TaskToken = taskToken, Output = $"\"{notification.Source}\"" }); } else if (job.Status == JobStatus.Failed) { using (var stepFunctionClient = new AmazonStepFunctionsClient()) await stepFunctionClient.SendTaskFailureAsync(new SendTaskFailureRequest { TaskToken = taskToken, Error = job.Type + " failed execution", Cause = job.Type + " with id '" + job.Id + "' failed execution with statusMessage '" + job.StatusMessage + "'" }); } else { Logger.Debug($"Ignoring notification for updated status of '{job.Status}'"); } }
public DescribeExecutionResponse FunctionHandler(JObject input) { LambdaLogger.Log(input.ToString()); AmazonStepFunctionsClient client = new AmazonStepFunctionsClient(); string executionArn = string.Format("{0}:{1}", Environment.GetEnvironmentVariable("stepFunctionsArn").Replace("stateMachine", "execution"), input.SelectToken("path.id")); LambdaLogger.Log(executionArn); DescribeExecutionRequest request = new DescribeExecutionRequest() { ExecutionArn = executionArn }; DescribeExecutionResponse response = client.DescribeExecutionAsync(request).Result; return(response); }
private async Task SendFailureAsync(String failureCause, String failureError) { AmazonStepFunctionsClient client = new AmazonStepFunctionsClient(); SendTaskFailureRequest failureRequest = new SendTaskFailureRequest(); failureRequest.Cause = failureCause; failureRequest.Error = failureError; failureRequest.TaskToken = taskToken; try { await client.SendTaskFailureAsync(failureRequest); } catch (Exception error) { Console.WriteLine("ERROR : SendFailureAsync : " + error.Message); Console.WriteLine("ERROR : SendFailureAsync : " + error.StackTrace); } await Task.CompletedTask; }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.LogLine(request.Body); var input = JsonSerializer.Deserialize <Payload>(request.Body); context.Logger.LogLine(JsonSerializer.Serialize(input.OrderID)); //Get Order details List <Order> allDocs = await GetOrderDetails(input); var savedState = allDocs.FirstOrDefault(); //Callback step function SendTaskSuccessResponse sendTaskSuccessResponse; using (var amazonStepFunctionsClient = new AmazonStepFunctionsClient()) { //Set Stepfunction output data. var data = new { IsApproved = input.IsApproved }; var jsonData = JsonSerializer.Serialize(data); var startExecutionRequest = new StartExecutionRequest { Input = jsonData, }; var result = amazonStepFunctionsClient.SendTaskSuccessAsync(new SendTaskSuccessRequest() { TaskToken = savedState.Token, Output = jsonData }).Result; } APIGatewayProxyResponse response = new APIGatewayProxyResponse() { Body = "Successfully invoked stepfunction.", StatusCode = 200 }; return(response); }
public async Task <JToken> Handler(JToken @event, ILambdaContext context) { if (@event == null) { throw new Exception("Missing workflow input"); } var resourceManager = AwsEnvironment.GetAwsV4ResourceManager(); try { var jobData = new JobBase { Status = "RUNNING", ParallelProgress = { ["speech-text-translate"] = 20 } }; await resourceManager.SendNotificationAsync(jobData, @event["notificationEndpoint"].ToMcmaObject <NotificationEndpoint>()); } catch (Exception error) { Logger.Error("Failed to send notification: {0}", error); } var stepFunction = new AmazonStepFunctionsClient(); var data = await stepFunction.GetActivityTaskAsync(new GetActivityTaskRequest { ActivityArn = ACTIVITY_ARN }); var taskToken = data.TaskToken; if (taskToken == null) { throw new Exception("Failed to obtain activity task"); } @event = JToken.Parse(data.Input); var jobProfiles = await resourceManager.GetAsync <JobProfile>(("name", JOB_PROFILE_NAME)); var jobProfileId = jobProfiles?.FirstOrDefault()?.Id; if (jobProfileId == null) { throw new Exception($"JobProfile '{JOB_PROFILE_NAME}' not found"); } var job = new AIJob { JobProfile = jobProfileId, JobInput = new JobParameterBag { ["inputFile"] = @event["data"]["mediaFileLocator"], ["outputLocation"] = new S3Locator { AwsS3Bucket = TEMP_BUCKET, AwsS3KeyPrefix = JOB_RESULTS_PREFIX } }, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = ACTIVITY_CALLBACK_URL + "?taskToken=" + Uri.EscapeDataString(taskToken) } }; job = await resourceManager.CreateAsync(job); return(job.Id); }
/// <summary> /// Constructor used for testing purposes /// </summary> /// <param name="ddbClient"></param> /// <param name="stepFunctions"></param> /// <param name="tablename"></param> public Function(IAmazonDynamoDB ddbClient, IAmazonStepFunctions stepFunctions, string tablename) { AWSSDKHandler.RegisterXRayForAllServices(); _incidentRepository = new IncidentRepository(ddbClient, tablename); _amazonStepFunctionsClient = (AmazonStepFunctionsClient)stepFunctions; }
/// <summary> /// Default constructor /// </summary> public Function() { AWSSDKHandler.RegisterXRayForAllServices(); _incidentRepository = new IncidentRepository(Environment.GetEnvironmentVariable("TABLE_NAME")); _amazonStepFunctionsClient = new AmazonStepFunctionsClient(); }
private StepFunctionSource InitializeSource() { var amazonStepFunctions = new AmazonStepFunctionsClient(CredentialsReader.GetCredentials(), RegionEndpoint.EUWest1); return(new StepFunctionSource(amazonStepFunctions)); }
public async Task <string> Handler(JToken @event, ILambdaContext context) { var resourceManager = AwsEnvironment.GetAwsV4ResourceManager(); try { var jobData = new JobBase { Status = "RUNNING", Progress = 27 }; await resourceManager.SendNotificationAsync(jobData, @event["notificationEndpoint"].ToMcmaObject <NotificationEndpoint>()); } catch (Exception error) { Logger.Error("Failed to send notification: {0}", error); } var stepFunction = new AmazonStepFunctionsClient(); Logger.Debug($"Getting Activity Task with ARN {ACTIVITY_ARN}..."); var data = await stepFunction.GetActivityTaskAsync(new GetActivityTaskRequest { ActivityArn = ACTIVITY_ARN }); var taskToken = data.TaskToken; if (taskToken == null) { throw new Exception("Failed to obtain activity task"); } Logger.Debug($"Activity Task token is {taskToken}"); @event = JToken.Parse(data.Input); Logger.Debug($"Getting job profile 'ExtractTechnicalMetadata'..."); var jobProfiles = await resourceManager.GetAsync <JobProfile>(("name", "ExtractTechnicalMetadata")); var jobProfileId = jobProfiles?.FirstOrDefault()?.Id; if (jobProfileId == null) { throw new Exception("JobProfile 'ExtractTechnicalMetadata' not found"); } var ameJob = new AmeJob { JobProfile = jobProfileId, JobInput = new JobParameterBag { ["inputFile"] = @event["data"]["repositoryFile"], ["outputLocation"] = new S3Locator { AwsS3Bucket = TEMP_BUCKET, AwsS3KeyPrefix = "AmeJobResults/" } }, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = ACTIVITY_CALLBACK_URL + "?taskToken=" + Uri.EscapeDataString(taskToken) } }; Logger.Debug($"Submitting AME job..."); ameJob = await resourceManager.CreateAsync(ameJob); Logger.Debug($"Successfully created AME job {ameJob.Id}."); return(ameJob.Id); }
//const aws = require('aws-sdk'); /// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public string FunctionHandler(ILambdaContext context) { //try //{ // var bucketName = System.Environment.GetEnvironmentVariable("mysqlconnection"); // var quotenumber = GetQuote(58); // string input = quotenumber.QuoteNumber; // return input?.ToUpper(); //} //catch (Exception e) //{ } LambdaLogger.Log("1. Lambda start"); //Starts workflow instance with parameter var stste = StartWorkflowInstanceAsync(); LambdaLogger.Log("2. Workflow started." + stste ?? "".ToString()); string emailstatus = "Problem occured in email approval."; string token = string.Empty; try { //AmazonStepFunctionsClient stepfunctions = new AmazonStepFunctionsClient(Amazon.RegionEndpoint.USEast1); AmazonSimpleEmailServiceClient ses = new AmazonSimpleEmailServiceClient(Amazon.RegionEndpoint.USEast1); Task <GetActivityTaskResponse> responsetask = null; GetActivityTaskResponse response = null; //return "Success"; LambdaLogger.Log("Task ARN"); var aws_access_key_id = Environment.GetEnvironmentVariable("aws_access_key_id"); var aws_secret_access_key = Environment.GetEnvironmentVariable("aws_secret_access_key"); using (AmazonStepFunctionsClient stepfunctions = new AmazonStepFunctionsClient(aws_access_key_id, aws_secret_access_key, Amazon.RegionEndpoint.USEast1)) { GetActivityTaskRequest request = new GetActivityTaskRequest { ActivityArn = "arn:aws:states:us-east-1:494875521123:activity:PMApprovalStep", WorkerName = "PMApprovingTaskActivity" }; LambdaLogger.Log("Response Output req sent "); responsetask = stepfunctions.GetActivityTaskAsync(request); responsetask.GetAwaiter(); response = responsetask.Result; //if(responsetask.) if (response != null) { LambdaLogger.Log("Response Output = " + response.Input); } else { LambdaLogger.Log("Response Output = null"); } token = response.TaskToken; LambdaLogger.Log("Token = " + token); } if (!string.IsNullOrEmpty(response.Input)) { List <string> tolst = GetUsersAsPerRole("PM"); var input = JObject.Parse(response.Input); //var email = "*****@*****.**"; //get to mail from salesOrg as per approval level var quoteid = (int)input.SelectToken("quoteid"); //var tolst = new List<string>(); //tolst.Add(email); Body body = new Body { Html = new Content { Charset = "UTF-8", Data = "<div>Hi!<br /><br /> " + "Promotion '" + (string)input.SelectToken("promodesc") + "' needs your approval!<br />" + "Can you please approve:<br /> <h3><a href='https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/approve?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "'>Click To Approve</a></h3><br />" + //"https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/approve?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "<br />" + "Or reject:<br />" + "<h3><a href='https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/reject?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "'>Click To Reject</a></h3></div><br /><br />Thank You,<br />Application Team" //"https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/reject?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "</div>" } }; Content subjectContent = new Content { Charset = "UTF-8", Data = "Your Approval Needed as Pricing Manager for Promotion Id " + quoteid }; LambdaLogger.Log("Starting Mail sending.. "); Message msg = new Message { Body = body, Subject = subjectContent }; SendEmailRequest s = new SendEmailRequest { Source = "*****@*****.**", Destination = new Destination { ToAddresses = tolst }, Message = msg, ConfigurationSetName = "sesconfigset", SourceArn = "arn:aws:ses:us-east-1:494875521123:identity/[email protected]" }; var status = ses.SendEmailAsync(s); status.GetAwaiter(); emailstatus = status.Result.HttpStatusCode.ToString(); LambdaLogger.Log("Going to update Db"); if (!string.IsNullOrEmpty(token)) { UpdateApprovalStatusWithToken(quoteid, Uri.EscapeDataString(token)); //Moving quote status to 3(PM) and updating token } //string input, //var bucketName = System.Environment.GetEnvironmentVariable("mysqlconnection"); //var quotenumber = GetQuote(58); //input = quotenumber.QuoteNumber; //return input?.ToUpper(); } } catch (Exception e) { LambdaLogger.Log("Response Output = " + e); } return(emailstatus); }
internal static async Task ProcessJobAssignmentAsync(WorkflowServiceWorkerRequest @event) { var resourceManager = @event.Request.GetAwsV4ResourceManager(); var table = new DynamoDbTable(@event.Request.StageVariables["TableName"]); var jobAssignmentId = @event.JobAssignmentId; try { // 1. Setting job assignment status to RUNNING await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "RUNNING", null); // 2. Retrieving WorkflowJob var workflowJob = await RetrieveWorkflowJobAsync(resourceManager, table, jobAssignmentId); // 3. Retrieve JobProfile var jobProfile = await RetrieveJobProfileAsync(resourceManager, workflowJob); // 4. Retrieve job inputParameters var jobInput = workflowJob.JobInput; // 5. Check if we support jobProfile and if we have required parameters in jobInput ValidateJobProfile(jobProfile, jobInput); // 6. Launch the appropriate workflow var workflowInput = new { Input = jobInput, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = jobAssignmentId + "/notifications" } }; var startExecutionRequest = new StartExecutionRequest { Input = workflowInput.ToMcmaJson().ToString() }; switch (jobProfile.Name) { case JOB_PROFILE_CONFORM_WORKFLOW: startExecutionRequest.StateMachineArn = @event.Request.StageVariables["ConformWorkflowId"]; break; case JOB_PROFILE_AI_WORKFLOW: startExecutionRequest.StateMachineArn = @event.Request.StageVariables["AiWorkflowId"]; break; } var stepFunctionClient = new AmazonStepFunctionsClient(); var startExecutionResponse = await stepFunctionClient.StartExecutionAsync(startExecutionRequest); // 7. saving the executionArn on the jobAssignment var jobAssignment = await GetJobAssignmentAsync(table, jobAssignmentId); //TODO: Additional properties on JobAssignment? How to handle this? //jobAssignment.WorkflowExecutionId = startExecutionResponse.ExecutionArn; await PutJobAssignmentAsync(resourceManager, table, jobAssignmentId, jobAssignment); } catch (Exception error) { Logger.Exception(error); try { await UpdateJobAssignmentStatusAsync(resourceManager, table, jobAssignmentId, "FAILED", error.ToString()); } catch (Exception innerError) { Logger.Exception(innerError); } } }
public async Task Handler(JToken @event, ILambdaContext context) { var resourceManager = AwsEnvironment.GetAwsV4ResourceManager(); try { var jobData = new JobBase { Status = "RUNNING", Progress = 54 }; await resourceManager.SendNotificationAsync(jobData, @event["notificationEndpoint"].ToMcmaObject <NotificationEndpoint>()); } catch (Exception error) { Logger.Error("Failed to send notification: {0}", error); } var stepFunction = new AmazonStepFunctionsClient(); var data = await stepFunction.GetActivityTaskAsync(new GetActivityTaskRequest { ActivityArn = ACTIVITY_ARN }); var taskToken = data.TaskToken; if (taskToken == null) { throw new Exception("Failed to obtain activity task"); } @event = JToken.Parse(data.Input); var jobProfiles = await resourceManager.GetAsync <JobProfile>(("name", JOB_PROFILE_NAME)); var jobProfileId = jobProfiles?.FirstOrDefault()?.Id; if (jobProfileId == null) { throw new Exception($"JobProfile '{JOB_PROFILE_NAME}' not found"); } var createProxyJob = new TransformJob { JobProfile = jobProfileId, JobInput = new JobParameterBag { ["inputFile"] = @event["data"]["repositoryFile"], ["outputLocation"] = new S3Locator { AwsS3Bucket = REPOSITORY_BUCKET, AwsS3KeyPrefix = "TransformJobResults/" } }, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = ACTIVITY_CALLBACK_URL + "?taskToken=" + Uri.EscapeDataString(taskToken) } }; createProxyJob = await resourceManager.CreateAsync(createProxyJob); }
public async Task Can_be_invoked_from_step_functions_local() { // 1. Create the StepFunctions Client var credentials = new BasicAWSCredentials("not", "used"); var config = new AmazonStepFunctionsConfig { ServiceURL = _stepFunctionsServiceUrl.ToString() }; var client = new AmazonStepFunctionsClient(credentials, config); // 2. Create step machine with a single task the invokes a lambda function // The FunctionName contains the lambda to be invoked. var request = new CreateStateMachineRequest { Name = "Foo", Type = StateMachineType.STANDARD, Definition = @"{ ""Comment"": ""A Hello World example demonstrating various state types of the Amazon States Language"", ""StartAt"": ""Invoke Lambda function"", ""States"": { ""Invoke Lambda function"": { ""Type"": ""Task"", ""Resource"": ""arn:aws:states:::lambda:invoke"", ""Parameters"": { ""FunctionName"": ""arn:aws:lambda:us-east-1:123456789012:function:SimpleLambdaFunction:$LATEST"", ""Payload"": { ""Input.$"": ""$.Payload"" } }, ""End"": true } } }" }; var createStateMachineResponse = await client.CreateStateMachineAsync(request); // 3. Create a StepFunction execution. var startExecutionRequest = new StartExecutionRequest { Name = Guid.NewGuid().ToString(), StateMachineArn = createStateMachineResponse.StateMachineArn, Input = @"{ ""Payload"": { ""Foo"": ""Bar"" } }" }; var startExecutionResponse = await client.StartExecutionAsync(startExecutionRequest); var getExecutionHistoryRequest = new GetExecutionHistoryRequest { ExecutionArn = startExecutionResponse.ExecutionArn, IncludeExecutionData = true, }; // 4. Poll and wait for the while (true) { var getExecutionHistoryResponse = await client.GetExecutionHistoryAsync(getExecutionHistoryRequest); var historyEvent = getExecutionHistoryResponse.Events.Last(); if (historyEvent.ExecutionSucceededEventDetails != null) { _outputHelper.WriteLine("Execution succeeded"); _outputHelper.WriteLine(historyEvent.ExecutionSucceededEventDetails.Output); break; } if (historyEvent.ExecutionFailedEventDetails != null) { _outputHelper.WriteLine("Execution failed"); _outputHelper.WriteLine(historyEvent.ExecutionFailedEventDetails.Cause); break; } } }
public async Task <JToken> Handler(JToken @event, ILambdaContext context) { if (@event == null) { throw new Exception("Missing workflow input"); } var resourceManager = AwsEnvironment.GetAwsV4ResourceManager(); try { var jobData = new JobBase { Status = "RUNNING", ParallelProgress = { ["speech-text-translate"] = 60 } }; await resourceManager.SendNotificationAsync(jobData, @event["notificationEndpoint"].ToMcmaObject <NotificationEndpoint>()); } catch (Exception error) { Logger.Error("Failed to send notification: {0}", error); } var stepFunction = new AmazonStepFunctionsClient(); var data = await stepFunction.GetActivityTaskAsync(new GetActivityTaskRequest { ActivityArn = ACTIVITY_ARN }); var taskToken = data.TaskToken; if (taskToken == null) { throw new Exception("Failed to obtain activity task"); } @event = JToken.Parse(data.Input); var jobProfiles = await resourceManager.GetAsync <JobProfile>(("name", JOB_PROFILE_NAME)); var jobProfileId = jobProfiles?.FirstOrDefault()?.Id; if (jobProfileId == null) { throw new Exception($"JobProfile '{JOB_PROFILE_NAME}' not found"); } // writing speech transcription to a textfile in temp bucket var bmContent = await resourceManager.ResolveAsync <BMContent>(@event["input"]["bmContent"].Value <string>()); // get the transcript from the BMContent var transcript = bmContent.Get <McmaExpandoObject>("awsAiMetadata") ?.Get <McmaExpandoObject>("transcription") ?.Get <string>("original"); if (transcript == null) { throw new Exception("Missing transcription on BMContent"); } var s3Params = new PutObjectRequest { BucketName = TEMP_BUCKET, Key = "AiInput/" + Guid.NewGuid() + ".txt", ContentBody = transcript }; var s3Client = new AmazonS3Client(); await s3Client.PutObjectAsync(s3Params); var job = new AIJob { JobProfile = jobProfileId, JobInput = new JobParameterBag { ["inputFile"] = new S3Locator { AwsS3Bucket = s3Params.BucketName, AwsS3Key = s3Params.Key }, ["targetLanguageCode"] = "ja", ["outputLocation"] = new S3Locator { AwsS3Bucket = TEMP_BUCKET, AwsS3Key = JOB_RESULTS_PREFIX } }, NotificationEndpoint = new NotificationEndpoint { HttpEndpoint = ACTIVITY_CALLBACK_URL + "?taskToken=" + Uri.EscapeDataString(taskToken) } }; job = await resourceManager.CreateAsync(job); return(job.Id); }