public string Remove(Notification input, ILambdaContext context) { var region = RegionEndpoint.APSoutheast1; var topicId = input.TopicId; //Get item from DynamoDB var key = new Dictionary <string, AttributeValue>(); key.Add("id", new AttributeValue() { S = topicId }); var dynamoDBClient = new AmazonDynamoDBClient(region); var getItemResponse = dynamoDBClient.GetItemAsync("Topics", key).Result; if (getItemResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't find item"); } var item = getItemResponse.Item; var topicArn = item.GetValueOrDefault("arn").S; //Remove permission to accept CloudWatch Events trigger for Lambda var lambdaClient = new AmazonLambdaClient(region); var removePermissionRequest = new Amazon.Lambda.Model.RemovePermissionRequest(); removePermissionRequest.FunctionName = "TestDonkeyLambda"; removePermissionRequest.StatementId = $"testdonkey-lambda-{ topicId }"; var removePermissionResponse = lambdaClient.RemovePermissionAsync(removePermissionRequest).Result; if (removePermissionResponse.HttpStatusCode != HttpStatusCode.NoContent) { throw new TestDonkeyException("Can't remove permission"); } //Remove target for CloudWatch Events var cloudWatchEventClient = new AmazonCloudWatchEventsClient(region); var removeTargetsRequest = new RemoveTargetsRequest(); removeTargetsRequest.Ids = new List <string> { $"testdonkey-target-{ topicId }" }; removeTargetsRequest.Rule = $"testdonkey-rule-{ topicId }"; var removeTargetsResponse = cloudWatchEventClient.RemoveTargetsAsync(removeTargetsRequest).Result; if (removeTargetsResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't remove target"); } //Delete rule in CloudWatch Events var deleteRuleRequest = new DeleteRuleRequest(); deleteRuleRequest.Name = removeTargetsRequest.Rule; var deleteRuleResponse = cloudWatchEventClient.DeleteRuleAsync(deleteRuleRequest).Result; if (deleteRuleResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't delete rule"); } //Remove subscribers from SNS var snsClient = new AmazonSimpleNotificationServiceClient(region); var listSubscriptionsByTopicRequest = new ListSubscriptionsByTopicRequest(); listSubscriptionsByTopicRequest.TopicArn = topicArn; ListSubscriptionsByTopicResponse listSubscriptionsByTopicResponse = null; do { listSubscriptionsByTopicResponse = snsClient.ListSubscriptionsByTopicAsync(listSubscriptionsByTopicRequest).Result; if (listSubscriptionsByTopicResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't list subscriptions"); } if (listSubscriptionsByTopicResponse.Subscriptions != null && listSubscriptionsByTopicResponse.Subscriptions.Count > 0) { foreach (var subscription in listSubscriptionsByTopicResponse.Subscriptions) { if (!subscription.SubscriptionArn.Equals("pendingconfirmation", StringComparison.OrdinalIgnoreCase)) { snsClient.UnsubscribeAsync(subscription.SubscriptionArn).GetAwaiter().GetResult(); } } } listSubscriptionsByTopicRequest.NextToken = listSubscriptionsByTopicResponse.NextToken; Thread.Sleep(1_000); //Wait for 1 second. Throttle: 100 transactions per second (TPS) } while (!string.IsNullOrWhiteSpace(listSubscriptionsByTopicResponse.NextToken)); //Delete topic from SNS var deleteTopicResponse = snsClient.DeleteTopicAsync(topicArn).Result; if (deleteTopicResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't delete topic"); } //Delete item from DynamoDB var dynamoDBDeleteItemResponse = dynamoDBClient.DeleteItemAsync("Topics", key).Result; if (dynamoDBDeleteItemResponse.HttpStatusCode != HttpStatusCode.OK) { throw new TestDonkeyException("Can't delete item"); } return("success"); }