private async Task <Application> CreateTopics(Application app) { string queueName = $"queue-{app.Name.Replace(" ", "")}-{app.Id}-subs"; AmazonSimpleNotificationServiceClient clientSNS = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>(); //create topics and subscribe them await app.Events.ToList().ForEachAsync(e => { string topic_name = $"topic-{app.Name.Replace(" ", "")}-event-{e.EventName}"; CreateTopicResponse topicResponse = clientSNS.CreateTopicAsync(new CreateTopicRequest(topic_name)).Result; if (topicResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new Exception($"Error creating topic {topic_name}"); } e.TopicArn = topicResponse.TopicArn; }); return(app); }
async Task ConfigureLambdaWithQueueAsync(string queueName) { string queueArn = null; AmazonSQSClient sqsClient = AwsFactory.CreateClient <AmazonSQSClient>(); GetQueueUrlRequest queueUrlReq = new GetQueueUrlRequest(); queueUrlReq.QueueName = queueName; GetQueueUrlResponse getQueueUrlResp = await sqsClient.GetQueueUrlAsync(queueUrlReq); GetQueueAttributesRequest queueAttribReq = new GetQueueAttributesRequest(); queueAttribReq.AttributeNames.Add(QueueAttributeName.QueueArn); queueAttribReq.QueueUrl = getQueueUrlResp.QueueUrl; var queueAttribResp = await sqsClient.GetQueueAttributesAsync(queueAttribReq); queueArn = queueAttribResp.QueueARN; AmazonLambdaClient lambdaClient = AwsFactory.CreateClient <AmazonLambdaClient>(); CreateEventSourceMappingRequest eventMappingReq = new CreateEventSourceMappingRequest(); eventMappingReq.FunctionName = "WebhookDispatcher"; eventMappingReq.BatchSize = 10; eventMappingReq.Enabled = true; eventMappingReq.EventSourceArn = queueArn; await lambdaClient.CreateEventSourceMappingAsync(eventMappingReq); }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.Log("Starting Minimum Get call"); using (AwsFactory factory = new AwsFactory(context.Logger)) { string userId = request.PathParameters["userId"]; string firstName = request.PathParameters["firstName"]; context.Logger.LogLine($"userId=\"{userId}\""); context.Logger.LogLine($"firstName=\"{firstName}\""); using (GetMinimum getMinimum = new GetMinimum(factory)) { string jsonResponse = getMinimum.Retrieve(userId, firstName); context.Logger.LogLine($"Response: {jsonResponse}"); APIGatewayProxyResponse response = new APIGatewayProxyResponse() { Body = jsonResponse, StatusCode = 200 }; return(response); } } }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.Log("Starting Weight Get call"); using (AwsFactory factory = new AwsFactory(context.Logger)) { string userId = request.PathParameters["userId"]; string firstName = request.PathParameters["firstName"]; string weightDateString = request.PathParameters["weightDate"]; context.Logger.LogLine($"userId=\"{userId}\""); context.Logger.LogLine($"firstName=\"{firstName}\""); context.Logger.LogLine($"weightDate=\"{weightDateString}\""); DateTime weightDate = Convert.ToDateTime(weightDateString); using (GetWeight getWeight = new GetWeight(factory)) { string jsonResponse = getWeight.Retrieve(userId, firstName, weightDate); context.Logger.LogLine($"Response: {jsonResponse}"); APIGatewayProxyResponse response = new APIGatewayProxyResponse() { Body = jsonResponse, StatusCode = 200 }; return(response); } } }
public async Task <string> NotifyAsync(string appId, string eventName, string message) { ValidateNotification(appId, eventName, message); string notifyRequestId = Guid.NewGuid().ToString(); AmazonSimpleNotificationServiceClient clientSNS = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>(); var app = await Repository.GetByIdAsync(appId); Event targetEvent = app.Events.ToList().Where(e => e.EventName == eventName).FirstOrDefault(); var response = await clientSNS.PublishAsync(new PublishRequest { Message = JsonSerializer.Serialize <NotifyMessage>(new NotifyMessage { EventName = eventName, ApplicationId = appId, Payload = message, NotifyRequestId = notifyRequestId }), TargetArn = targetEvent.TopicArn }); await NotificationRequestRepository.CreateAsync(new NotificationRequest { ApplicationId = appId, EventName = eventName, Id = notifyRequestId, Payload = message, TopicArn = targetEvent.TopicArn, Timestamp = DateTime.Now.ToUniversalTime() }); return(response.MessageId); }
async Task <CreateQueueResponse> CreateQueueAsync(string queueName, string SubscriptionId) { CreateQueueRequest deadLetterRequest = new CreateQueueRequest(string.Concat(queueName, "-deadletter")); deadLetterRequest.Attributes = new Dictionary <string, string>(); deadLetterRequest.Attributes.Add(QueueAttributeName.ReceiveMessageWaitTimeSeconds, "20"); deadLetterRequest.Attributes.Add(QueueAttributeName.MessageRetentionPeriod, "864000"); string deadLetterArn = null; AmazonSQSClient sqsClient = AwsFactory.CreateClient <AmazonSQSClient>(); var createResponse = await sqsClient.CreateQueueAsync(deadLetterRequest); GetQueueAttributesRequest queueReq = new GetQueueAttributesRequest(); queueReq.QueueUrl = createResponse.QueueUrl; queueReq.AttributeNames.Add(QueueAttributeName.All); var queueAttribs = await sqsClient.GetQueueAttributesAsync(queueReq); deadLetterArn = queueAttribs.QueueARN; string redrivePolicy = $"{{\"deadLetterTargetArn\":\"{deadLetterArn}\",\"maxReceiveCount\":5}}"; CreateQueueRequest createQueueRequest = new CreateQueueRequest(); createQueueRequest.QueueName = queueName; createQueueRequest.Attributes = new Dictionary <string, string>(); createQueueRequest.Attributes.Add(QueueAttributeName.RedrivePolicy, redrivePolicy); createQueueRequest.Attributes.Add(QueueAttributeName.ReceiveMessageWaitTimeSeconds, "20"); //createQueueRequest.Attributes.Add("trigger-id", SubscriptionId); CreateQueueResponse queueResponse = await sqsClient.CreateQueueAsync(createQueueRequest); return(queueResponse); }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { context.Logger.Log("Starting User Post call"); using (AwsFactory factory = new AwsFactory(context.Logger)) { using (PostUser postUser = new PostUser(factory)) { context.Logger.Log($"Request body: {request.Body}"); string jsonResponse = postUser.Save(request.Body); context.Logger.LogLine($"Response: {jsonResponse}"); APIGatewayProxyResponse response = new APIGatewayProxyResponse() { Body = jsonResponse, StatusCode = 200 }; return(response); } } }
public void DeleteAllTopics() { AmazonSimpleNotificationServiceClient clientSNS = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>(); AmazonSQSClient clientSQS = AwsFactory.CreateClient <AmazonSQSClient>(); AmazonLambdaClient lambdaClient = AwsFactory.CreateClient <AmazonLambdaClient>(); var topics = clientSNS.ListTopicsAsync(); // var subs = clientSNS.ListSubscriptionsAsync(new ListSubscriptionsRequest()); var filas = clientSQS.ListQueuesAsync("subs"); filas.Result.QueueUrls.ForEach(i => { var deleted = clientSQS.DeleteQueueAsync(i); if (deleted.Result.HttpStatusCode != HttpStatusCode.OK) { int x = 0; } }); string nextToken = ""; do { var subs = clientSNS.ListSubscriptionsAsync(new ListSubscriptionsRequest(nextToken)); subs.Result.Subscriptions.ForEach(i => { var deleted = clientSNS.UnsubscribeAsync(i.SubscriptionArn); }); nextToken = subs.Result.NextToken; } while (!String.IsNullOrEmpty(nextToken)); var mapper = lambdaClient.ListEventSourceMappingsAsync(new Amazon.Lambda.Model.ListEventSourceMappingsRequest { FunctionName = "WebhookDispatcher" }); mapper.Result.EventSourceMappings.ToList().ForEach(i => { var result = lambdaClient.DeleteEventSourceMappingAsync(new Amazon.Lambda.Model.DeleteEventSourceMappingRequest() { UUID = i.UUID }); if (result.Result.HttpStatusCode != HttpStatusCode.OK) { int x = 0; } }); topics.Result.Topics.ForEach(i => { var deleted = clientSNS.DeleteTopicAsync(new DeleteTopicRequest() { TopicArn = i.TopicArn }); }); }
public async Task <string> Create(Model.Subscription subscription) { subscription.Id = Guid.NewGuid().ToString(); string queueName = $"subs-{subscription.Id}"; //create queue var queueResponse = await CreateQueueAsync(queueName, subscription.Id); subscription.QueueUrl = queueResponse.QueueUrl; //set lambda subs await ConfigureLambdaWithQueueAsync(queueName); //Get topicsARN var app = await ApplicationRepository.GetByIdAsync(subscription.ApplicationId); if (app != null) { //getting ARN var events = from subEvent in subscription.Events join appEvent in app.Events on subEvent.EventName equals appEvent.EventName select new Event { EventName = appEvent.EventName, TopicArn = appEvent.TopicArn }; await events.ToList().ForEachAsync(e => { subscription.Events.RemoveAll(y => y.EventName == e.EventName); subscription.Events.Add(e); var sub = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>() .SubscribeQueueToTopicsAsync( new List <string>() { e.TopicArn }, AwsFactory.CreateClient <AmazonSQSClient>(), queueResponse.QueueUrl); sub.Result.ToList().ForEach(s => { try { var setting = AwsFactory.CreateClient <AmazonSimpleNotificationServiceClient>().SetSubscriptionAttributesAsync(new SetSubscriptionAttributesRequest { AttributeName = "RawMessageDelivery", AttributeValue = "true", SubscriptionArn = sub.Result.FirstOrDefault().Value }); } catch (Exception) { throw new WebhookManagerException("Error setting Subscription Attribute"); } } ); }); await Repository.CreateAsync(subscription); } return(subscription.Id); }