public async Task <long> Execute() { var sw = Stopwatch.StartNew(); Log($"{_context?.FunctionName} => {nameof(FunctionHandler)} => Execute"); try { //Select Instances with Route Tag Key Only var instances = (await _EC2.ListInstances()).Where(instance => instance.Tags.Any(x => x.Key.Contains("Route53 Name"))).ToArray(); var running = instances.Where(instance => instance.State.Code == 16); //running var not_running = instances.Where(instance => instance.State.Code != 16); //not running var blacklist = new List <string>(); //only process running instances if there are stopped ones with the same 'Route53 Name' if (!running.IsNullOrEmpty() && !not_running.IsNullOrEmpty()) { foreach (var live in running) { var names_live = live.Tags.Where(x => x.Key.Contains("Route53 Name") && !x.Value.IsNullOrEmpty()).Select(x => x.Value); var zones_live = live.Tags.Where(x => x.Key.Contains("Route53 Zone") && !x.Value.IsNullOrEmpty()).Select(x => x.Value); foreach (var stopped in not_running) { if (blacklist.Contains(stopped.InstanceId) || live.InstanceId == stopped.InstanceId) { continue; //dont process already blacklisted instances or the same instances } var names_stopped = stopped.Tags.Where(x => x.Key.Contains("Route53 Name") && !x.Value.IsNullOrEmpty()).Select(x => x.Value); var zones_stopped = stopped.Tags.Where(x => x.Key.Contains("Route53 Zone") && !x.Value.IsNullOrEmpty()).Select(x => x.Value); if (names_live.IntersectAny(names_stopped) && zones_live.IntersectAny(zones_stopped)) { Console.WriteLine($"Blacklisting instance '{stopped.InstanceId}' from processing, found overlapping zones and names with already running instance '{live.InstanceId}'."); blacklist.Add(stopped.InstanceId); } } } } //Select Non blacklisted instances instances = instances.Where(instance => !blacklist.Contains(instance.InstanceId)).ToArray(); var zones = await _R53.GetRecordSets(); if (zones.Count <= 0) { Log($"AWSRouter53 can't process any tags, not a single Route53 Zone was found."); } else { Log($"Processing validating routes for {zones.Count} zones and {instances.Length} instances..."); } await ParallelEx.ForEachAsync(instances, async instance => await Process(zones, instance)); return(sw.ElapsedMilliseconds); } finally { Log($"{_context?.FunctionName} => {nameof(FunctionHandler)} => Stopped, Eveluated within: {sw.ElapsedMilliseconds} [ms]"); } }
private static async Task executeR53(string[] args) { var nArgs = CLIHelper.GetNamedArguments(args); var helper = new Route53Helper(); switch (args[1]) { case "destroy-record": await helper.DestroyRecord( zoneId : nArgs["zone"], recordName : nArgs["name"], recordType : nArgs["type"], throwIfNotFound : nArgs.GetValueOrDefault("throw-if-not-foud").ToBoolOrDefault(true)); ; break; case "upsert-cname-record": { var result = await helper.UpsertCNameRecordAsync( zoneId : nArgs["zone"], name : nArgs["name"], value : nArgs["value"], ttl : nArgs.GetValueOrDefault("ttl").ToIntOrDefault(60), failover : nArgs.GetValueOrDefault("failover"), healthCheckId : nArgs.GetValueOrDefault("health-check-id"), setIdentifier : nArgs.GetValueOrDefault("set-identifier")); WriteLine($"SUCCESS, Result: '{result}'"); } ; break; case "upsert-a-record": { var result = await helper.UpsertARecordAsync( zoneId : nArgs["zone"], name : nArgs["name"], value : nArgs["value"], ttl : nArgs.GetValueOrDefault("ttl").ToIntOrDefault(60), failover : nArgs.GetValueOrDefault("failover"), healthCheckId : nArgs.GetValueOrDefault("health-check-id"), setIdentifier : nArgs.GetValueOrDefault("set-identifier")); WriteLine($"SUCCESS, Result: '{result}'"); } ; break; case "get-record-sets": { WriteLine("Loading Route53 Record Sets..."); var result = await helper.GetRecordSets(); WriteLine("SUCCESS, Result:"); Console.WriteLine(result.Select(x => (x.Key.Name, x.Value.Select(y => y))).JsonSerialize(Newtonsoft.Json.Formatting.Indented)); } ; break; case "list-resource-record-sets": { WriteLine("Loading Route53 Resource Record Sets..."); var result = await helper.ListResourceRecordSetsAsync(nArgs["zone"]); WriteLine("SUCCESS, Result:"); Console.WriteLine(result.JsonSerialize(Newtonsoft.Json.Formatting.Indented)); } ; break; case "help": case "--help": case "-help": case "-h": case "h": HelpPrinter($"{args[0]}", "Amazon Route53", ("destroy-record", "Accepts params: zone, name, type, throw-if-not-foud (optional)"), ("get-record-sets", "Accepts params: no params"), ("list-resource-record-sets", "Accepts params: zone"), ("upsert-cname-record", "Accepts: zone, name, value, ttl (optional:60), failover (optional), health-check-id (optional), set-identifier (optional)"), ("upsert-a-record", "Accepts: zone, name, value, ttl (optional:60), failover (optional), health-check-id (optional), set-identifier (optional)")); break; default: { Console.WriteLine($"Try '{args[0]} help' to find out list of available commands."); throw new Exception($"Unknown Route53 command: '{args[0]} {args[1]}'"); } } }