Ejemplo n.º 1
0
        public async Task FunctionHandler(ILambdaContext context)
        {
            var sw = Stopwatch.StartNew();

            context.Logger.Log($"{context?.FunctionName} => {nameof(FunctionHandler)} => Started");

            try
            {
                var instances = await _EC2.ListInstances();

                if (instances.Length <= 0)
                {
                    context.Logger.Log($"AWSTerminator can't process tags, not a single EC2 Instance was found.");
                }

                await ParallelEx.ForEachAsync(instances, async instance => await Process(instance, context.Logger));
            }
            finally
            {
                context.Logger.Log($"{context?.FunctionName} => {nameof(FunctionHandler)} => Stopped, Eveluated within: {sw.ElapsedMilliseconds} [ms]");
            }
        }
Ejemplo n.º 2
0
        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]");
            }
        }